Is it safe to put an index on an Oracle Temporary Table?

前端 未结 6 2308
闹比i
闹比i 2021-02-19 22:41

I have read that one should not analyze a temp table, as it screws up the table statistics for others. What about an index? If I put an index on the table for the duration of

6条回答
  •  广开言路
    2021-02-19 23:18

    You cannot create an index on a temporary table while it is used by another session, so answer is: No, it cannot affect any other process, because it is not possible.

    An existing Index affects only your current session, because for any other session the temporary table appears empty, so it cannot access any index values.

    Session 1:

    SQL> create global temporary table index_test (val number(15)) on commit preserve rows;
    Table created.
    SQL> insert into index_test values (1);
    1 row created.
    SQL> commit;
    Commit complete.
    SQL>
    

    Session 2 (while session 1 is still connected):

    SQL> create unique index idx_val on index_test(val);
    create unique index idx_val on index_test(val)
                                   *
    ERROR at line 1:
    ORA-14452: attempt to create, alter or drop an index on temporary table already in use
    SQL>
    

    Back to session 1:

    SQL> delete from index_test;
    1 row deleted.
    SQL> commit;
    Commit complete.
    SQL>
    

    Session 2:

    SQL> create unique index idx_val on index_test(val);
    create unique index idx_val on index_test(val)
                                   *
    ERROR at line 1:
    ORA-14452: attempt to create, alter or drop an index on temporary table already in use
    SQL>
    

    still failing, you first have to disconnect session 1 or table has to be truncated.

    Session 1:

    SQL> truncate table index_test;
    Table truncated.
    SQL>
    

    Now you can create the index in Session 2:

    SQL> create unique index idx_val on index_test(val);
    Index created.
    SQL>
    

    This index of course will be used by any session.

提交回复
热议问题