我读到过不应该分析临时表,因为这会破坏其他表的统计信息。那索引呢?如果在我的程序运行期间在表上放置一个索引,是否会影响使用该表的其他程序?
索引会影响我的进程和所有使用该表的其他进程吗? 还是仅影响我的进程?
没有权威的回复,所以我提供了贿赂。
我读到过不应该分析临时表,因为这会破坏其他表的统计信息。那索引呢?如果在我的程序运行期间在表上放置一个索引,是否会影响使用该表的其他程序?
索引会影响我的进程和所有使用该表的其他进程吗? 还是仅影响我的进程?
没有权威的回复,所以我提供了贿赂。
[编辑] 我看到您已经完善了问题,这里是一个稍微完善的答案:
来自:
Oracle® Database Administrator's Guide
10g Release 2 (10.2)
Part Number B14231-02
"可以在临时表上创建索引。这些索引也是临时的,索引中的数据与基础表中的数据具有相同的会话或事务范围。
如果您需要在事务范围内对索引进行有效处理,则我想您将不得不在查询中明确提示它,因为统计信息将显示表中没有行。"
exec dbms_stats.set_table_stats(user, 'my_temp_table', numrows=>10, numblks=>4)
另一个提示是,如果临时表的大小差异很大,并且在您的事务中,您知道临时表中有多少行,则可以通过提供该信息来帮助优化器。如果您从临时表连接到常规表,则我发现这非常有帮助。SELECT /*+ CARDINALITY(my_temp_table 100) */ * FROM my_temp_table
我试过了,第二个会话可以看到并使用索引。如果你真的需要一个索引,为你的数据创建一个新的全局临时表会更安全。
同时,当其他会话正在访问该表时,你也无法创建索引。
以下是我运行的测试用例:
--first session
create global temporary table index_test (val number(15))
on commit preserve rows;
create unique index idx_val on index_test(val);
--second session
insert into index_test select rownum from all_tables;
select * from index_test where val=1;
当临时表正在被另一个会话使用时,您无法在其上创建索引,因此答案是:不行,它不能影响任何其他进程,因为这是不可能的。
现有的索引仅影响您当前的会话,因为对于任何其他会话,临时表都为空,因此无法访问任何索引值。
会话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 (当第一次连接仍然保持时):
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>
返回第1个会话:
SQL> delete from index_test;
1 row deleted.
SQL> commit;
Commit complete.
SQL>
第二节:
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>
仍然无法通过,您首先必须断开会话1或清空表格。
会话1:
SQL> truncate table index_test;
Table truncated.
SQL>
现在你可以在第二个会话中创建索引:
SQL> create unique index idx_val on index_test(val);
Index created.
SQL>
这个索引当然会被任何会话使用。