您的位置:首页 > 数据库 > Oracle

什么决定了SKIP索引扫描的出现

2013-09-09 11:47 316 查看
建表:

SQL> create table test as select 1 v_num,emp.* from emp;

Table created.
SQL> update test set v_num = 2 where empno = 7369;

1 row updated.
建索引:
SQL> create index index_n_e on test(v_num,empno);

Index created.
索引所涉及到列的基数情况:

SQL> select count(distinct v_num),count(distinct empno) from test;

COUNT(DISTINCTV_NUM) COUNT(DISTINCTEMPNO)
-------------------- --------------------
2 14
查询语句和执行计划:
1.

SQL> select ename from test where empno = 7369;

Execution Plan
----------------------------------------------------------
Plan hash value: 1357081020

--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 20 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST | 1 | 20 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------2.
SQL> analyze table test compute statistics; Table analyzed.
SQL> select ename from test where empno = 7369;

Execution Plan
----------------------------------------------------------
Plan hash value: 3656150759

-----------------------------------------------------------------------------------------
| Id  | Operation		    | Name	| Rows	| Bytes | Cost (%CPU)| Time	|
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT	    |		|     1 |     8 |     3   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST	|     1 |     8 |     3   (0)| 00:00:01 |
|*  2 |   INDEX SKIP SCAN	    | INDEX_N_E |     1 |	|     2   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
3.
SQL> drop index index_n_e;

Index dropped.
SQL> create index index_e_n on test(empno,v_num);

Index created.

SQL> select ename from test where empno = 7369;

Execution Plan
----------------------------------------------------------
Plan hash value: 2333580441

-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 8 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TEST | 1 | 8 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | INDEX_E_N | 1 | | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------4.
SQL> select ename from test where v_num = 2;

Execution Plan
----------------------------------------------------------
Plan hash value: 1357081020

--------------------------------------------------------------------------
| Id  | Operation	  | Name | Rows  | Bytes | Cost (%CPU)| Time	 |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |	 |     7 |    49 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TEST |     7 |    49 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------
经过上面的两组对照(1,2;3,4)可以得出以下结论:
如果想要组合索引走跳跃索引扫描(INDEX SKIP SCAN)需要满足的条件有:

1.复合索引的引导列基数较低,而其他列基数高

2.对表进行分析

-------------------------------------------------------------------------

同时在做试验的时候也遇到了这样的情况,当表上有索引处于unusable的时候,是不能对这个表进行分析的。

SQL> alter index index_e_n unusable;

Index altered.
SQL> analyze table test compute statistics;
analyze table test compute statistics
*
ERROR at line 1:
ORA-01502: index 'SCOTT.INDEX_E_N' or partition of such index is in unusable state
SQL> alter index index_e_n rebuild;

Index altered.
SQL> analyze table test compute statistics;

Table analyzed.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Oracle 索引 SKIP
相关文章推荐