执行计划中常见index访问方式(转)
近期有朋友对于单个表上的index各种情况比较模糊,这里对于单个表上,单个index出现的大多数情况进行了总结性测试,给出了测试结果,至于为什么出现这样的试验结果未做过多解释,给读者留下思考的空间.本篇文章仅仅是为了测试hint对index的影响,而不是说明走各种index方式的好坏.参考: INDEX FULL SCAN vs INDEX FAST FULL SCAN
创建表模拟测试
SQL> create table t_xifenfei as select object_id,object_name from dba_objects;Table created.SQL> create index i_t_object_id on t_xifenfei(object_id); Index created.SQL> exec dbms_stats.gather_table_stats(USER,'T_XIFENFEI',cascade=>true); PL/SQL procedure successfully completed.SQL> desc t_xifenfei Name Null? Type ----------------------------------------- -------- ---------------------------- OBJECT_ID NUMBER OBJECT_NAME VARCHAR2(128) |
TABLE ACCESS FULL
SQL> SET AUTOT TRACE EXP STATSQL> SELECT OBJECT_ID FROM T_XIFENFEI;49838 rows selected.Execution Plan----------------------------------------------------------Plan hash value: 548923532--------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 49838 | 243K| 57 (2)| 00:00:01 || 1 | TABLE ACCESS FULL| T_XIFENFEI | 49838 | 243K| 57 (2)| 00:00:01 |--------------------------------------------------------------------------------Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 3544 consistent gets 0 physical reads 0 redo size 721203 bytes sent via SQL*Net to client 36927 bytes received via SQL*Net from client 3324 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 49838 rows processedSQL> SELECT /*+ INDEX(T i_t_object_id) */ OBJECT_ID FROM T_XIFENFEI;49838 rows selected.Execution Plan----------------------------------------------------------Plan hash value: 548923532--------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 49838 | 243K| 57 (2)| 00:00:01 || 1 | TABLE ACCESS FULL| T_XIFENFEI | 49838 | 243K| 57 (2)| 00:00:01 |--------------------------------------------------------------------------------Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 3544 consistent gets 0 physical reads 0 redo size 721203 bytes sent via SQL*Net to client 36927 bytes received via SQL*Net from client 3324 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 49838 rows processed |
从上面的执行计划中可知,此时走了全表扫描. 由于我们需要查询的列为object_id,因此理论上只需要读取索引就应该可以返回所有数据,而此时为什么是全表扫描呢? 这是因为NULL值与索引的特性所决定的.即null值不会被存储到B树索引.因此应该为表 t_xifenfei 的列 object_id 添加 not null 约束.
INDEX FAST FULL SCAN
SQL> alter table t_xifenfei modify(object_id not null); Table altered.SQL> SELECT object_id from t_xifenfei;49838 rows selected.Execution Plan----------------------------------------------------------Plan hash value: 2036340805--------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 49838 | 243K| 27 (4)| 00:00:01 || 1 | INDEX FAST FULL SCAN| I_T_OBJECT_ID | 49838 | 243K| 27 (4)| 00:00:01 |--------------------------------------------------------------------------------------Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 3432 consistent gets 0 physical reads 0 redo size 721203 bytes sent via SQL*Net to client 36927 bytes received via SQL*Net from client 3324 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 49838 rows processed |
INDEX FAST FULL SCAN:当在高速缓存中没有找到所需的索引块时,则根据db_file_multiblock_read_count的值进行多块读操作.对于索引的分支结构只是简单的获取,然后扫描所有的叶结点.其结果是导致索引结构没有访问,获取的数据没有根据索引键的顺序排序.INDEX FAST FULL SCAN使用multiblock_read,故产生db file scattered reads 事件.
INDEX RANGE SCAN
SQL> select object_id from t_xifenfei where object_id<10;8 rows selected.Execution Plan----------------------------------------------------------Plan hash value: 2197008162----------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 2 | 10 | 2 (0)| 00:00:01 ||* 1 | INDEX RANGE SCAN| I_T_OBJECT_ID | 2 | 10 | 2 (0)| 00:00:01 |----------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 1 - access("OBJECT_ID"<10)Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 3 consistent gets 0 physical reads 0 redo size 499 bytes sent via SQL*Net to client 385 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 8 rows processedSQL> select /*+ index_ffs(t i_t_object_id) */ object_id from t_xifenfei t where object_id<10;8 rows selected.Execution Plan----------------------------------------------------------Plan hash value: 2036340805--------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 2 | 10 | 27 (4)| 00:00:01 ||* 1 | INDEX FAST FULL SCAN| I_T_OBJECT_ID | 2 | 10 | 27 (4)| 00:00:01 |--------------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 1 - filter("OBJECT_ID"<10)Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 118 consistent gets 0 physical reads 0 redo size 499 bytes sent via SQL*Net to client 385 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 8 rows processed1这里可以看出index_ffs已经生效,但是对于这样的情况hint index_ffs效率一般来说不会太高.<br><strong>INDEX FULL SCAN</strong>1SQL> SELECT /*+ INDEX(T i_t_object_id) */ object_id from t_xifenfei t;49838 rows selected.Execution Plan----------------------------------------------------------Plan hash value: 431110666----------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 49838 | 243K| 113 (2)| 00:00:02 || 1 | INDEX FULL SCAN | I_T_OBJECT_ID | 49838 | 243K| 113 (2)| 00:00:02 |----------------------------------------------------------------------------------Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 3426 consistent gets 0 physical reads 0 redo size 721203 bytes sent via SQL*Net to client 36927 bytes received via SQL*Net from client 3324 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 49838 rows processed |
INDEX FULL SCAN:完全按照索引存储的顺序依次访问整个索引树.当访问到叶结点之后,按照双向链表方式读取相连节点的值.换言之,对于索引上所有的数据是按照有序的方式来读取的.如果索引块没有在高速缓存中被找到时,则需要从数据文件中单块进行读取.对于需要读取大量数据的全索引扫描而言,这将使其变得低效.INDEX FULL SCAN使用single read,故产生db file sequential reads事件.新版的Oracle支持db file parallel reads方式.
HINT INDEX不会使用INDEX FAST FULL SCAN功能.
INDEX列ORDER BY
SQL> SELECT OBJECT_ID FROM T_XIFENFEI order by object_id ;49838 rows selected.Execution Plan----------------------------------------------------------Plan hash value: 431110666----------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 49838 | 243K| 113 (2)| 00:00:02 || 1 | INDEX FULL SCAN | I_T_OBJECT_ID | 49838 | 243K| 113 (2)| 00:00:02 |----------------------------------------------------------------------------------Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 3426 consistent gets 0 physical reads 0 redo size 721203 bytes sent via SQL*Net to client 36927 bytes received via SQL*Net from client 3324 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 49838 rows processedSQL> SELECT OBJECT_ID FROM T_XIFENFEI order by object_id desc;49838 rows selected.Execution Plan----------------------------------------------------------Plan hash value: 2808014233--------------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 49838 | 243K| 113 (2)| 00:00:02 || 1 | INDEX FULL SCAN DESCENDING| I_T_OBJECT_ID | 49838 | 243K| 113 (2)| 00:00:02 |--------------------------------------------------------------------------------------------Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 3427 consistent gets 0 physical reads 0 redo size 721203 bytes sent via SQL*Net to client 36927 bytes received via SQL*Net from client 3324 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 49838 rows processedSQL> SELECT /*+ index_ffs(t i_t_object_id) */ object_id from t_xifenfei t order by object_id;49838 rows selected.Execution Plan----------------------------------------------------------Plan hash value: 2527678987-----------------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 49838 | 243K| | 185 (4)| 00:00:03 || 1 | SORT ORDER BY | | 49838 | 243K| 1192K| 185 (4)| 00:00:03 || 2 | INDEX FAST FULL SCAN| I_T_OBJECT_ID | 49838 | 243K| | 27 (4)| 00:00:01 |-----------------------------------------------------------------------------------------------Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 117 consistent gets 0 physical reads 0 redo size 721203 bytes sent via SQL*Net to client 36927 bytes received via SQL*Net from client 3324 SQL*Net roundtrips to/from client 1 sorts (memory) 0 sorts (disk) 49838 rows processed |
对于index 列排序,默认情况下会使用INDEX FULL SCAN/INDEX FULL SCAN DESCENDING而不选择使用INDEX FAST FULL SCAN,因为INDEX FAST FULL SCAN获得数据后,还需要做一次SORT ORDER BY操作
INDEX FAST FULL SCAN+SORT AGGREGATE
SQL> SELECT count(object_id) FROM T_XIFENFEI;Execution Plan----------------------------------------------------------Plan hash value: 3095383276-------------------------------------------------------------------------------| Id | Operation | Name | Rows | Cost (%CPU)| Time |-------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 27 (4)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | | || 2 | INDEX FAST FULL SCAN| I_T_OBJECT_ID | 49838 | 27 (4)| 00:00:01 |-------------------------------------------------------------------------------Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 117 consistent gets 0 physical reads 0 redo size 421 bytes sent via SQL*Net to client 385 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processedSQL> SELECT /*+ INDEX(T i_t_object_id) */ count(object_id) FROM T_XIFENFEI t;Execution Plan----------------------------------------------------------Plan hash value: 3079973526--------------------------------------------------------------------------| Id | Operation | Name | Rows | Cost (%CPU)| Time |--------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 113 (2)| 00:00:02 || 1 | SORT AGGREGATE | | 1 | | || 2 | INDEX FULL SCAN| I_T_OBJECT_ID | 49838 | 113 (2)| 00:00:02 |--------------------------------------------------------------------------Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 111 consistent gets 0 physical reads 0 redo size 421 bytes sent via SQL*Net to client 385 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed |
sort aggregate通常发生在使用一些聚合函数的时候,sum(),avg(),min(),max(),count()等等,实际上sort aggregate不做真正的sort,并不会用到排序空间,而是通过一个全局变量+全表或全索引扫描来实现.这样的操作在默认情况下使用INDEX FAST FULL SCAN
INDEX FULL SCAN (MIN/MAX)
SQL> SELECT max(object_id) FROM T_XIFENFEI;Execution Plan----------------------------------------------------------Plan hash value: 2939893782--------------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 5 | 2 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 5 | | || 2 | INDEX FULL SCAN (MIN/MAX)| I_T_OBJECT_ID | 49838 | 243K| 2 (0)| 00:00:01 |--------------------------------------------------------------------------------------------Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 2 consistent gets 0 physical reads 0 redo size 419 bytes sent via SQL*Net to client 385 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processedSQL> SELECT /*+ index_ffs(t i_t_object_id) */ max(object_id) FROM T_XIFENFEI t;Execution Plan----------------------------------------------------------Plan hash value: 2939893782--------------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 5 | 27 (4)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 5 | | || 2 | INDEX FULL SCAN (MIN/MAX)| I_T_OBJECT_ID | 49838 | 243K| 27 (4)| 00:00:01 |--------------------------------------------------------------------------------------------Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 2 consistent gets 0 physical reads 0 redo size 419 bytes sent via SQL*Net to client 385 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed |
对于这样的查询INDEX FULL SCAN (MIN/MAX)明显是最优,但是此处奇怪的是使用了index_ffs提示无效,如果有知道的朋友,麻烦告知原因.
http://www.xifenfei.com/2968.html
执行计划中常见index访问方式(转)的更多相关文章
- MySQL 执行计划中Extra(Using where,Using index,Using index condition,Using index,Using where)的浅析
关于如何理解MySQL执行计划中Extra列的Using where.Using Index.Using index condition,Using index,Using where这四者的区别 ...
- Sql Server中的表访问方式Table Scan, Index Scan, Index Seek
1.oracle中的表访问方式 在oracle中有表访问方式的说法,访问表中的数据主要通过三种方式进行访问: 全表扫描(full table scan),直接访问数据页,查找满足条件的数据 通过row ...
- 转:Sql Server中的表访问方式Table Scan, Index Scan, Index Seek
0.参考文献 Table Scan, Index Scan, Index Seek SQL SERVER – Index Seek vs. Index Scan – Diffefence and Us ...
- SQL Server 执行计划中的扫描方式举例说明
SQL Server 执行计划中的扫描方式举例说明 原文地址:http://www.cnblogs.com/zihunqingxin/p/3201155.html 1.执行计划使用方式 选中需要执行的 ...
- [转载] EXPLAIN执行计划中要重点关注哪些要素
原文: https://mp.weixin.qq.com/s?__biz=MjM5NzAzMTY4NQ==&mid=400738936&idx=1&sn=2910b4119b9 ...
- EXPLAIN执行计划中要重点关注哪些要素(叶金荣)
原文:http://mp.weixin.qq.com/s/CDKN_nPcIjzA_U5-xwAE5w 导读 EXPLAIN的结果中,有哪些关键信息值得注意呢? MySQL的EXPLAIN当然和ORA ...
- EXPLAIN执行计划中要重点关注哪些要素
MySQL的EXPLAIN当然和ORACLE的没法比,不过我们从它输出的结果中,也可以得到很多有用的信息. 总的来说,我们只需要关注结果中的几列: 列名 备注 type 本次查询表联接类型,从这里可以 ...
- EXPLAIN执行计划中要重点关注哪些要素(转)
EXPLAIN的结果中,有哪些关键信息值得注意呢? MySQL的EXPLAIN当然和ORACLE的没法比,不过我们从它输出的结果中,也可以得到很多有用的信息. 总的来说,我们只需要关注结果中的几列: ...
- SQL Server 2000中的并行处理和执行计划中的位图运算符
SQL Server 2000中的并行处理和执行计划中的位图运算符 摘抄自:SQLServer 2000并行处理和位图简介 刘志斌 并行查询介绍Degree of Parallelism(并行度) 一 ...
随机推荐
- 理解 Linux 网络栈(1):Linux 网络协议栈简单总结 图
http://www.cnblogs.com/sammyliu/p/5225623.html
- hdu 5073 Galaxy
题意是给定n个点,让求找到一个点p使得sigma( (a[i] - p) ^ 2 ) 最小,其中a[i]表示第i个点的位置.其中有k个点不用算. 思路:发现这道题其实就是求n-k个点方差. 那么推一下 ...
- partial局部类
局部类型允许我们将一个类.接口或结构分成好几个部分,分别实现在几个不同的.cs文件中. 局部类型适用于以下情况: (1)类型特别大,不宜放在一个文件中实现. (2)一个类型中的一部分代码为自动化工具生 ...
- sqlserver 时间 格式化
0 或 100 (*) 默认值 mon dd yyyy hh:miAM(或 PM) 1 101 美国 mm/dd/yyyy ...
- jquery ui 插件------------------------->sortable
<!doctype html><html lang="en"><head> <meta charset="utf-8" ...
- $(document).ready(function(){});不执行
这里可能会有以下几种原因,请你挨个排查: 1.jqury的文件一定要引入1.js文件的引用路径不正确,特别是使用了命名空间,容易造成路径错误,使用绝对路径看是否成功 2.某一些函数使用错误,举个例子, ...
- sid超过8个字符处理步骤
服务端配置如下: [oracle@p3 admin]$ cat listener.ora # listener.ora Network Configuration File: /home/oracle ...
- 为什么选择Typescript
上一节,我简单介绍了Typescript,并将Typescript和JavaScript进行了对比,有些网友提出了一些疑问,可能有些网友对于这个Typescript还不是特别的熟悉,这节,我做一些演示 ...
- jQuery 删除元素
通过 jQuery,可以很容易地删除已有的 HTML 元素. 删除元素/内容 如需删除元素和内容,一般可使用以下两个 jQuery 方法: remove() - 删除被选元素(及其子元素) empty ...
- 从XML文件中获取格式化的文本信息
在FMW的运维过程中,时常需要将中间传输的XML信息转换为excel格式化的问题提交给关联系统人员,现总结三种格式化问题提供方式 一.使用Excel转换 因为从系统中取到的xml文档为中间信息文档,需 ...