标签:
-- 创建两张相同结构的表,内部结构及数据均引用scott用户下的emp表
SQL> select count(*) from test01; COUNT(*) ---------- 14
SQL> select count(*) from test02; COUNT(*) ---------- 14 --针对表TEST01的empno列,添加B-tree索引 SQL> create index PK_TEST01 on TEST01(EMPNO); Index created.
--针对表TEST02的empno列,添加反向索引 SQL> create index PK_REV_TEST02 on TEST02(EMPNO) REVERSE; Index created.
--验证上面的索引,NORMAL/REV表明为反向索引 SQL> select TABLE_NAME,INDEX_NAME,INDEX_TYPE from user_indexes where INDEX_NAME like ‘%TEST%‘; TABLE_NAME INDEX_NAME INDEX_TYPE -------------------- -------------------- -------------------- TEST01 PK_TEST01 NORMAL TEST02 PK_REV_TEST02 NORMAL/REV
--打开会话追踪 SQL> set autotrace traceonly
--相同条件查询,观察两表的执行计划 SQL> select * from TEST01 where empno=7369;
Execution Plan ---------------------------------------------------------- Plan hash value: 515586510 ----------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 87 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| TEST01 | 1 | 87 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | PK_TEST01 | 1 | | 1 (0)| 00:00:01 | ----------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("EMPNO"=7369) Note ----- - dynamic sampling used for this statement (level=2) Statistics ---------------------------------------------------------- 152 recursive calls 0 db block gets 23 consistent gets 0 physical reads 0 redo size 1025 bytes sent via SQL*Net to client 523 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed SQL> select * from TEST02 where empno=7369; Execution Plan ---------------------------------------------------------- Plan hash value: 1053012716 --------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 87 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| TEST02 | 1 | 87 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | PK_REV_TEST02 | 1 | | 1 (0)| 00:00:01 | --------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("EMPNO"=7369) Note ----- - dynamic sampling used for this statement (level=2) Statistics ---------------------------------------------------------- 148 recursive calls 0 db block gets 21 consistent gets 0 physical reads 0 redo size 1025 bytes sent via SQL*Net to client 523 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed -- 相同范围条件查询,观察两表的执行计划 SQL> select * from TEST01 where empno between 7350 and 7500; Execution Plan ---------------------------------------------------------- Plan hash value: 515586510 ----------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 2 | 174 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| TEST01 | 2 | 174 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | PK_TEST01 | 2 | | 1 (0)| 00:00:01 | ----------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("EMPNO">=7350 AND "EMPNO"<=7500) Note ----- - dynamic sampling used for this statement (level=2) Statistics ---------------------------------------------------------- 9 recursive calls 0 db block gets 10 consistent gets 0 physical reads 0 redo size 1120 bytes sent via SQL*Net to client 523 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 2 rows processed SQL> select * from TEST02 where empno between 7350 and 7500; Execution Plan ---------------------------------------------------------- Plan hash value: 3294238222 ---------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 2 | 174 | 3 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| TEST02 | 2 | 174 | 3 (0)| 00:00:01 | ---------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("EMPNO">=7350 AND "EMPNO"<=7500) Note ----- - dynamic sampling used for this statement (level=2) Statistics ---------------------------------------------------------- 5 recursive calls 0 db block gets 8 consistent gets0 redo size 1112 bytes sent via SQL*Net to client 523 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 2 rows processed
通过上面的示例可以看到,当使用between条件进行范围查询时,采用反向索引的表,并没有使用索引,而是采用了全表扫面的方式进行检索。
Oracle索引总结(三)- Oracle索引种类之反向索引
标签:
原文地址:http://www.cnblogs.com/yumiko/p/5916347.html