概述
今天主要介绍下Oracle索引的常见执行计划:
- INDEX FULL SCAN:索引的全扫描,单块读,有序
- INDEX RANGE SCAN:索引的范围扫描
- INDEX FAST FULL SCAN:索引的快速全扫描,多块读,无序
- INDEX FULL SCAN(MIN/MAX):针对MAX(),MIN()函数的查询
- INDEX SKIP SCAN:查询条件没有用到组合索引的第一列,而组合索引的第一列重复度较高时,可能用到。
这里用简单的测试案例,体会下索引使用这些执行计划的场景。
1. 准备测试环境
创建测试表和索引:
droptabletest_objects; createtabletest_objectsasselect*fromall_objects; createindexidx_test_objects_1ontest_objects(owner,object_name,subobject_name); createindexidx_test_objects_2ontest_objects(object_id); desctest_objects;
查看测试表上的索引信息:
selectindex_name,column_name,column_positionfromuser_ind_columnswheretable_name='TEST_OBJECTS';
分析表并清空测试环境的shared_pool和buffer_cache:
analyzetabletest_objectscomputestatistics; altersystemflushshared_pool; altersystemflushbuffer_cache;
2. 准备SQL语句
根据不同执行计划的场景,编写SQL语句:
--INDEXRANGESCAN(索引的范围扫描) SELECTowner,object_nameFROMtest_objectsWHEREowner='SYS'ANDobject_name='DBMS_OUTPUT'; --INDEXSKIPSCAN(针对MAX(),MIN()函数的查询) SELECTowner,object_nameFROMtest_objectsWHEREobject_name='DBMS_OUTPUT'; --INDEXFASTFULLSCAN(索引的快速全扫描,多块读,无序) SELECTowner,object_nameFROMtest_objects; --INDEXFULLSCAN(索引的全扫描,单块读,有序) SELECTowner,object_nameFROMtest_objectsorderby1,2; --INDEXFULLSCAN(MIN/MAX)(针对MAX(),MIN()函数的查询) SELECTmax(object_id)FROMtest_objects;
3. 实验论证
结果如下:
(1) INDEX RANGE SCAN
setautotracetraceonly SELECTowner,object_nameFROMtest_objectsWHEREowner='SYS'ANDobject_name='DBMS_OUTPUT';
(2) INDEX SKIP SCAN
SELECTowner,object_nameFROMtest_objectsWHEREobject_name='DBMS_OUTPUT';
(3) INDEX FAST FULL SCAN
SELECTowner,object_nameFROMtest_objects;
(4) INDEX FULL SCAN
SELECTowner,object_nameFROMtest_objectsorderby1,2;
(5) INDEX FULL SCAN (MIN/MAX)
SELECTmax(object_id)FROMtest_objects;
相关文章
标签:Oracle