Oracle索引扫描的五种类型
之前在讨论CBO和RBO的时候提到了索引扫描的⼏种类型。
Oracle Optimizer CBO RBO
smokingOracle 索引详解
缮写Oracle Explain Plan
根据索引的类型与where限制条件的不同,有4种类型的Oracle索引扫描:
(1)索引唯⼀扫描(index unique scan)
(2)索引范围扫描(index range scan)
(3)索引全扫描(index full scan)
(4)索引快速扫描(index fast full scan)
(5)索引跳跃扫描(INDEX SKIP SCAN)某人的英文
⼀. 索引唯⼀扫描(index unique scan)
softskills通过唯⼀索引查找⼀个数值经常返回单个ROWID。如果该唯⼀索引有多个列组成(即组合索引),则⾄少要有组合索引的引导列参与到该查询中,如创建⼀个索引:create index idx_test on emp(ename, deptno, loc)。则lect ename from emp where ename = ‘JACK’ and deptno =‘DEV’语句可以使⽤该索引。如果该语句只返回⼀⾏,则存取⽅法称为索引唯⼀扫描。⽽lect ename from emp where deptno = ‘DEV’语句则不会使⽤该索引,因为where⼦句种没有引导列。如果存在UNIQUE 或PRIMARY KEY 约束(它保证了语句只存取单⾏)的话,Oracle经常实现唯⼀性扫描。
如:
SQL> t autot traceonly exp; -- 只显⽰执⾏计划
SQL> lect * p t pno=10;
执⾏计划
----------------------------------------------------------
Plan hash value: 2949544139
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
--------------------------------------------------------------------------------
领事馆英语| 0 | SELECT STATEMENT | | 1 | 38 | 1 (0)| 00:0
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 38 | 1 (0)| 00:0
|* 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | | 0 (0)| 00:0
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("T"."EMPNO"=10)
⼆.索引范围扫描(index range scan)
使⽤⼀个索引存取多⾏数据,同上⾯⼀样,如果索引是组合索引,⽽且lect ename from emp where ename = ‘JACK’ and deptno =
‘DEV’语句返回多⾏数据,虽然该语句还是使⽤该组合索引进⾏查询,可此时的存取⽅法称为索引范围扫描。
在唯⼀索引上使⽤索引范围扫描的典型情况下是在谓词(where限制条件)中使⽤了范围操作符(如>、<、<>、>=、<=、between)
使⽤索引范围扫描的例⼦:
SQL> lect empno,ename p where empno > 7876 order by empno;
执⾏计划
----------------------------------------------------------
Plan hash value: 169057108
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 10 | 2 (0)| 00:0
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 10 | 2 (0)| 00:0
|* 2 | INDEX RANGE SCAN | PK_EMP | 1 | | 1 (0)| 00:0
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
投诉信英文
2 - access("EMPNO">7876)
在⾮唯⼀索引上,谓词可能返回多⾏数据,所以在⾮唯⼀索引上都使⽤索引范围扫描。
使⽤index rang scan的3种情况:
(a) 在唯⼀索引列上使⽤了range操作符(> < <> >= <= between)。 (b) 在组合索引上,只使⽤部分列进⾏查询,导致查询出多⾏。 (c) 对⾮唯⼀索引列上进⾏的任何查询。
三.索引全扫描(index full scan)
与全表扫描对应,也有相应的全Oracle索引扫描。在某些情况下,可能进⾏全Oracle索引扫描⽽不是范围扫描,需要注意的是全Oracle索引扫描只在CBO模式下才有效。 CBO根据统计数值得知进⾏全Oracle索引扫描⽐进⾏全表扫描更有效时,才进⾏全Oracle索引扫描,⽽且此时查询出的数据都必须从索引中可以直接得到。
全Oracle索引扫描的例⼦:
SQL> create index big_emp p(empno,ename);
索引已创建。
SQL> lect empno, ename p order by empno,ename;
执⾏计划
----------------------------------------------------------
Plan hash value: 322359667
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 140 | 1 (0)| 00:00:01 |
| 1 | INDEX FULL SCAN | BIG_EMP | 14 | 140 | 1 (0)| 00:00:01 |
----------------------------------------------------------------------------
四.索引快速扫描(index fast full scan)
扫描索引中的所有的数据块,与 index full scan很类似,但是⼀个显著的区别就是它不对查询出的数据进⾏排序,即数据不是以排序顺序被返回。在这种存取⽅法中,可以使⽤多块读功能,也可以使⽤并⾏读⼊,以便获得最⼤吞吐量与缩短执⾏时间。
索引快速扫描的例⼦:
japane massage
SQL> lect /*+ index_ffs(dave index_dave) */ id from dave where id>0;
执⾏计划
----------------------------------------------------------
Plan hash value: 674200218
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 8 | 24 | 2 (0)| 00:00:0
|* 1 | INDEX FAST FULL SCAN| INDEX_DAVE | 8 | 24 | 2 (0)| 00:00:0
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
-
有趣的英文--------------------------------------------------
1 - filter("ID">0)
为了实现这个效果,折腾了半天,最终还是⽤hint来了.
Oracle Hint
五. 索引跳跃扫描(INDEX SKIP SCAN)
INDEX SKIP SCAN,发⽣在多个列建⽴的复合索引上,如果SQL中谓词条件只包含索引中的部分列,并且这些列不是建⽴索引时的第⼀列时,就可能发⽣INDEX SKIP SCAN。这⾥SKIP的意思是因为查询条件没有第⼀列或前⾯⼏列,被忽略了。
Oracle 10g的⽂档如下:
Index skip scans improve index scans by nonprefix columns. Often, scanning index blocks is faster than scanning table data blocks. Skip scanning lets a composite index be split logically into smaller subindexes. In skip scanning, the initial column of the composite index is not specified in the query. In other words, it is skipped.
--skip scan 让组合索引(composite index)逻辑的split 成⼏个⼦索引。如果在在查询时,第⼀个列没有指定,就跳过它。
The number of logical subindexes is determined by the number of distinct values in the initial column. Skip scanning is advantageous if there are few distinct values in the leading column of the composite index and many distinct values in the nonleading key of the index.
-- 建议将distinct 值⼩的列作为组合索引的引导列,即第⼀列。
Example 13-5 Index Skip Scan
Consider, for example, a table employees (x, employee_id, address) with a composite index on (x, employee_id). Splitting this composite index would result in two logical subindexes, one for M and one for F.
For this example, suppo you have the following index data:
客房服务员英文
('F',98)
('F',100)
('F',102)
('F',104)
('M',101)
('M',103)
('M',105)
The index is split logically into the following two subindexes:
(1)The first subindex has the keys with the value F.
(2)The cond subindex has the keys with the value M.
Figure 13-2 Index Skip Scan Illustration
The column x is skipped in the following query:
SELECT *
FROM employees
WHERE employee_id = 101;
A complete scan of the index is not performed, but the subindex with the value F is arched first, followed by a arch of the subindex with the value M.
测试:
创建表:
SQL> create table dave_test as lect owner,object_id,object_type,created from dba_objects; Table created.
创建组合索引
SQL> create index idx_dave_test_com on dave_test(owner,object_id,object_type);
Index created.
--收集表的统计信息
SQL> exec dbms_stats.gather_table_stats('SYS','DAVE_TEST');
PL/SQL procedure successfully completed.
SQL> t autot traceonly exp;
指定组合索引的所有字段时,使⽤Index range scan:
SQL> lect * from dave_test where owner='SYS' and object_id=20 and object_type='TABLE';
Execution Plan
----------------------------------------------------------
Plan hash value: 418973243
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 27 | 2
we just laugh about it| 1 | TABLE ACCESS BY INDEX ROWID| DAVE_TEST | 1 | 27 | 2
|* 2 | INDEX RANGE SCAN | IDX_DAVE_TEST_COM | 1 | | 1
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OWNER"='SYS' AND "OBJECT_ID"=20 AND "OBJECT_TYPE"='TABLE')
指定组合索引的2个字段时,使⽤的还是index range scan:
SQL> lect * from dave_test where owner='SYS' and object_id=20;
Execution Plan
----------------------------------------------------------