手机版
你好,游客 登录 注册
背景:
阅读新闻

Oracle创建基于事务和基于会话的临时表及临时表建索引的实验

[日期:2013-10-27] 来源:Linux社区  作者:还不算晕 [字体:大 中 小]

3.临时表上建立索引

会话1:环境及错误提示
14:30:11 SQL> create global temporary table temp3 on commit preserve rows as select * from dba_objects;
Table created
14:31:22 SQL> select count(*) from temp3;
COUNT(*)
----------
72009
14:32:25 SQL> create index idx_temp3 on temp3(object_id);
create index idx_temp3 on temp3(object_id)

ORA-14452: attempt to create, alter or drop an index on temporary table already in use

对以上错误的解释:create index idx_temp3 on temp3(object_id);用于会话相关,也就在事务结束后truncate data in the temporary table,但如果在会话未结束时要修改temporary table就会出现错误:

ORA-14452: attempt to create, alter or drop an index on temporary table already in use

此时可以从其它会话中来建索引。


会话2:建索引
SQL> create index idx_temp3 on temp3(object_id);
Index created
SQL> select index_name,index_type,table_owner,table_name from user_indexes where table_name='TEMP3';
INDEX_NAME INDEX_TYPE TABLE_OWNER TABLE_NAME
------------------------------ --------------------------- ------------------------------ ------------------------------
IDX_TEMP3 NORMAL BYS TEMP3

回到会话1:查询新建索引的相关信息
14:36:21 SQL> select index_name,index_type,table_owner,table_name from user_indexes where table_name='TEMP3';
INDEX_NAME INDEX_TYPE TABLE_OWNER TABLE_NAME
------------------------------ --------------------------- ------------------------------ ------------------------------
IDX_TEMP3 NORMAL BYS TEMP3
14:44:02 SQL> select count(*) from temp3;
COUNT(*)
----------
72009
14:44:35 SQL> select * from temp3 where object_id=50;
OWNER OBJECT_NAME SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE CREATED LAST_DDL_TIME TIMESTAMP STATUS TEMPORARY GENERATED SECONDARY NAMESPACE EDITION_NAME
------------------------------ -------------------------------------------------------------------------------- ------------------------------ ---------- -------------- ------------------- ----------- ------------- ------------------- ------- --------- --------- --------- ---------- ------------------------------

14:44:58 SQL> select table_name,tablespace_name from user_tables where table_name='TEMP3';
TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------
TEMP3
14:46:19 SQL> select index_name,tablespace_name from user_indexes where table_name='TEMP3';
INDEX_NAME TABLESPACE_NAME
------------------------------ ------------------------------
IDX_TEMP3
14:47:05 SQL> select segment_name,tablespace_name from user_segments where segment_name in('TEMP3','IDX_TEMP3');
SEGMENT_NAME TABLESPACE_NAME
-------------------------------------------------------------------------------- ------------------------------

会话3:在新建的会话上,测试查询是否能用到索引。第一次查询未用到索引,因为表内无数据。第二次查询从执行计划中可以看到使用了索引
SQL> set autotrace on explain;
SQL> select * from temp3 where object_id=111;
未选定行
执行计划
----------------------------------------------------------
Plan hash value: 2448592476
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 207 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEMP3 | 1 | 207 | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------
SQL>
SQL> set autotrace off;
SQL> select index_name,index_type,table_owner,table_name from user_indexes where table_name='TEMP3';
INDEX_NAME INDEX_TYPE
------------------------------ ---------------------------
TABLE_OWNER TABLE_NAME
------------------------------ ------------------------------
IDX_TEMP3 NORMAL
BYS TEMP3

SQL> select count(*) from temp3;
COUNT(*)
----------
0
SQL> insert into temp3 select * from dba_objects;
已创建72010行。
SQL> commit;
提交完成。
SQL> set autotrace on explain;
SQL> select * from temp3 where object_id=111;
未选定行
执行计划
----------------------------------------------------------
Plan hash value: 3995552559
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 207 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TEMP3 | 1 | 207 | 1 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_TEMP3 | 1 | | 1 (0)| 00:00:01 |

更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

linux
相关资讯       Oracle索引  Oracle临时表 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款