Oracle访问索引的执行计划(一)
Oracle访问索引的执行计划(二)
当需要获取某个字段的全部数据时,如果该字段非空且有索引,或者通过where条件将空值排除掉,则可能会使用索引全扫描(INDEX FULL SCAN)。
查看HR用户下EMPLOYEES表的LAST_NAME字段的索引,为非唯一索引。
SQL> select a.table_name,column_name,a.index_name,index_type,uniqueness from user_indexes a,user_ind_columns b where a.index_name=b.index_name and a.table_name='EMPLOYEES' and column_name='LAST_NAME';
TABLE_NAME COLUMN_NAME INDEX_NAME INDEX_TYPE UNIQUENES
--------------- --------------- --------------- --------------- ---------
EMPLOYEES LAST_NAME EMP_NAME_IX NORMAL NONUNIQUE
查看HR用户下EMPLOYEES表的LAST_NAME字段为非空。
SQL> select table_name,column_name,nullable from user_tab_columns where table_name='EMPLOYEES' and column_name='LAST_NAME';
TABLE_NAME COLUMN_NAME N
------------------------------ ------------------------------ -
EMPLOYEES LAST_NAME N
查看获取last_name这个字段的所有记录的执行计划:
SQL> select last_name from employees;
107 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2228653197
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 107 | 856 | 1 (0)| 00:00:01 |
| 1 | INDEX FULL SCAN | EMP_NAME_IX | 107 | 856 | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------
查看HR用户下EMPLOYEES表的DEPARTMENT_ID字段的索引,为非唯一索引。
SQL> select a.table_name,column_name,a.index_name,index_type,uniqueness from user_indexes a,user_ind_columns b where a.index_name=b.index_name and a.table_name='EMPLOYEES' and column_name='DEPARTMENT_ID';
TABLE_NAME COLUMN_NAME INDEX_NAME INDEX_TYPE UNIQUENES
--------------- --------------- -------------------- --------------- ---------
EMPLOYEES DEPARTMENT_ID EMP_DEPARTMENT_IX NORMAL NONUNIQUE
查看HR用户下EMPLOYEES表的DEPARTMENT_ID字段,可以为空。
SQL> select table_name,column_name,nullable from user_tab_columns where table_name='EMPLOYEES' and column_name='DEPARTMENT_ID';
TABLE_NAME COLUMN_NAME N
--------------- --------------- -
EMPLOYEES DEPARTMENT_ID Y
查看获取department_id这个字段的所有非空记录的执行计划:
SQL> select department_id from employees where department_id is not null;
106 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3420648541
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 106 | 318 | 1 (0)| 00:00:01 |
|* 1 | INDEX FULL SCAN | EMP_DEPARTMENT_IX | 106 | 318 | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
从上面的执行计划可以看出,由于索引键值就是查询结果,所有就不需要回表了。
索引全扫描会对目标索引的所有叶子块从左到右依次顺序扫描,所以它的结果是有序的。也就是说索引全扫描是不能够并行执行的,并且通常情况下使用的是单块读,产生db file sequential reads事件。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28536251/viewspace-2140022/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28536251/viewspace-2140022/