oracle sql 执行计划分析
转自http://itindex.net/detail/45962-oracle-sql-%E8%AE%A1%E5%88%92
一、首先创建表
SQL> show userUSER is "RHYS"SQL> create table A(col1 number(4,0),col2 number(4,0), col4 char(30));create table B(col1 number(4,0),col3 number(4,0), name_b char(30));create table C(col2 number(4,0),col3 number(4,0), name_c char(30));Table created.SQL>Table created.SQL>Table created.
第二、查看一下执行计划。
1、
SQL> select a.col4 from c,a,b2 where c.col3=5 and a.col1=b.col1 and a.col2=c.col2 and b.col3=10;Execution Plan----------------------------------------------------------Plan hash value: 1485247927------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 110 | 6 (0)| 00:00:01 ||* 1 | HASH JOIN | | 1 | 110 | 6 (0)| 00:00:01 || 2 | MERGE JOIN CARTESIAN| | 1 | 52 | 4 (0)| 00:00:01 ||* 3 | TABLE ACCESS FULL | C | 1 | 26 | 2 (0)| 00:00:01 || 4 | BUFFER SORT | | 1 | 26 | 2 (0)| 00:00:01 ||* 5 | TABLE ACCESS FULL | B | 1 | 26 | 2 (0)| 00:00:01 || 6 | TABLE ACCESS FULL | A | 1 | 58 | 2 (0)| 00:00:01 |------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------1 - access("A"."COL1"="B"."COL1" AND "A"."COL2"="C"."COL2")3 - filter("C"."COL3"=5)5 - filter("B"."COL3"=10)Note------ dynamic sampling used for this statement (level=2)
执行计划主要查看:访问路径,连接顺序,连接方法
执行计划顺序为上内原则,同层次上边先执行,内层先执行。
plan hash value:当sql第一次在shared pool中进行执行的是硬解析并生产该hash值
id,只是一个标号,并不是实际执行顺序
operation:从字面意思也看出来就是操作的类型
name:对象的名字
rows:oracle估计该操作返回的行数
bytes:产生的数据量
cost:表示该sql执行 到此步骤的时候sql执行代价。
该sql的执行步骤如下:
首先执行id 3-》id5-》id4—》id2-》id6-》id1-》id0
首先对id3进行全表扫描过滤条件为filter("C"."COL3"=5),然后对表b进行全表扫描,条件为 filter("B"."COL3"=10),完了之后再进行buffer sort排序,最后把3和4的row source 进行merge join 笛卡尔积操作,并把所有的结果作为row source1 ,也就是驱动表,然后把表A作为被探测表,两者进行hash join。这就是这一个过程信息。
注意此处在id5和id3没有关联的条件,就采用了笛卡尔积,这是不好的现象。
2、
SQL> select /*+ordered*/ a.col4 from c,a,b2 where c.col3=5 and a.col1=b.col1 and a.col2=c.col2 and b.col3=10;Execution Plan----------------------------------------------------------Plan hash value: 531790806----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 110 | 6 (0)| 00:00:01 ||* 1 | HASH JOIN | | 1 | 110 | 6 (0)| 00:00:01 ||* 2 | HASH JOIN | | 1 | 84 | 4 (0)| 00:00:01 ||* 3 | TABLE ACCESS FULL| C | 1 | 26 | 2 (0)| 00:00:01 || 4 | TABLE ACCESS FULL| A | 1 | 58 | 2 (0)| 00:00:01 ||* 5 | TABLE ACCESS FULL | B | 1 | 26 | 2 (0)| 00:00:01 |----------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------1 - access("A"."COL1"="B"."COL1")2 - access("A"."COL2"="C"."COL2")3 - filter("C"."COL3"=5)5 - filter("B"."COL3"=10)Note------ dynamic sampling used for this statement (level=2)
使用hints可以调整optimizer的执行连接方法,在此例中我们指定了ordered使得采用hash join选取from 之后从左到有第一个表c作为驱动表。
执行顺序为:id3全表扫描过滤条件为filter("C"."COL3"=5)-》id4 全表扫描,然后表c为驱动表,a为探测表以此来进行hashjoin-》id5 全表扫描过滤条件为filter("B"."COL3"=10),此后执行id2为外部表,id5为被探测表进行hash join,从access访问路径可以看出首先是id2为("A"."COL2"="C"."COL2")此后为 id1access("A"."COL1"="B"."COL1")。
这是整个sql执行的整个过程。
为了便于理解分析一下数据,
首先我要取到在表c中col3=5的所有数据,然后再内存进行hash,作为hash table,然后我在去使用该hash table去探测A表进行匹配,取出的数据为access("A"."COL2"="C"."COL2"),把最后的匹配结果作为row source,再次建立hash table表,然后再去探测b表,方式为:access("A"."COL1"="B"."COL1")。最终获得了0执行的结果信息。
对于note动态采样信息请参考:
http://www.oracle.com/technetwork/issue-archive/2009/09-jan/o19asktom-086775.html
由于本次没有对表进行analyze所有存有动态取样。
SQL> select /*+ordered use_nl(a c)*/ a.col4 from c,a,b2 where c.col3=5 and a.col1=b.col1 and a.col2=c.col2 and b.col3=10;Execution Plan----------------------------------------------------------Plan hash value: 1446226736----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 110 | 6 (0)| 00:00:01 ||* 1 | HASH JOIN | | 1 | 110 | 6 (0)| 00:00:01 || 2 | NESTED LOOPS | | 1 | 84 | 4 (0)| 00:00:01 ||* 3 | TABLE ACCESS FULL| C | 1 | 26 | 2 (0)| 00:00:01 ||* 4 | TABLE ACCESS FULL| A | 1 | 58 | 2 (0)| 00:00:01 ||* 5 | TABLE ACCESS FULL | B | 1 | 26 | 2 (0)| 00:00:01 |----------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------1 - access("A"."COL1"="B"."COL1")3 - filter("C"."COL3"=5)4 - filter("A"."COL2"="C"."COL2")5 - filter("B"."COL3"=10)Note------ dynamic sampling used for this statement (level=2)
在这个语句中,表c和a进行了nested loops然后把结果惊醒hash table在与表b做jash join。
另外对于表有索引的情况进行如下分析。
首先创建表a的组合索引,索引列为(col1,col2)
eg:
SQL> create index inx_col12A on a(col1,col2);Index created.SQL> select A.col42 from C , A , B3 where C.col3 = 5 and A.col1 = B.col1 and A.col2 = C.col24 and B.col3 = 10;Execution Plan----------------------------------------------------------Plan hash value: 2122808611-------------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |-------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 110 | 4 (0)| 00:00:01 || 1 | NESTED LOOPS | | 1 | 110 | 4 (0)| 00:00:01 || 2 | NESTED LOOPS | | 1 | 110 | 4 (0)| 00:00:01 || 3 | MERGE JOIN CARTESIAN | | 1 | 52 | 4 (0)| 00:00:01 ||* 4 | TABLE ACCESS FULL | C | 1 | 26 | 2 (0)| 00:00:01 || 5 | BUFFER SORT | | 1 | 26 | 2 (0)| 00:00:01 ||* 6 | TABLE ACCESS FULL | B | 1 | 26 | 2 (0)| 00:00:01 ||* 7 | INDEX RANGE SCAN | INX_COL12A | 1 | | 0 (0)| 00:00:01 || 8 | TABLE ACCESS BY INDEX ROWID| A | 1 | 58 | 0 (0)| 00:00:01 |-------------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------4 - filter("C"."COL3"=5)6 - filter("B"."COL3"=10)7 - access("A"."COL1"="B"."COL1" AND "A"."COL2"="C"."COL2")Note------ dynamic sampling used for this statement (level=2)
这个比较有意思了。首先看一下执行顺序,首先对表c进行全表扫描过滤条件为col3=5取出数据作为row source1,然后再对b进行全表扫描过滤条件为col3=10,因为走的是merge join 笛卡尔积的排序连接,然后再buffer 进行sort作为row sources2 ,完了之后row source1和row source2作合并连接,完了之后作为row source1 是驱动表,然后再进行index range scan(索引范围扫描)访问路径为: access("A"."COL1"="B"."COL1" AND "A"."COL2"="C"."COL2"),完了之后把结果作为row source1 然后再去与表A进行嵌套循环操作,不过A也就是id8 走的是index rowid。完了之后再进行0获得数据。太繁琐了。呵呵。
SQL> select /*+ ORDERED USE_NL (A C)*/ A.col42 from C , A , B3 where C.col3 = 5 and A.col1 = B.col1 and A.col2 = C.col24 and B.col3 = 10;Execution Plan----------------------------------------------------------Plan hash value: 1446226736----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 110 | 6 (0)| 00:00:01 ||* 1 | HASH JOIN | | 1 | 110 | 6 (0)| 00:00:01 || 2 | NESTED LOOPS | | 1 | 84 | 4 (0)| 00:00:01 ||* 3 | TABLE ACCESS FULL| C | 1 | 26 | 2 (0)| 00:00:01 ||* 4 | TABLE ACCESS FULL| A | 1 | 58 | 2 (0)| 00:00:01 ||* 5 | TABLE ACCESS FULL | B | 1 | 26 | 2 (0)| 00:00:01 |----------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------1 - access("A"."COL1"="B"."COL1")3 - filter("C"."COL3"=5)4 - filter("A"."COL2"="C"."COL2")5 - filter("B"."COL3"=10)Note------ dynamic sampling used for this statement (level=2)
当改变optimizer选择的执行计划时候,添加了hints,然后我们使用嵌套循环,驱动表为c,被驱动表为A,完了之后再作为row source1做为hash table, 然后与表B进行hash join。
SQL> select /*+ USE_NL (A C)*/ A.col42 from C , A , B3 where C.col3 = 5 and A.col1 = B.col1 and A.col2 = C.col24 and B.col3 = 10;Execution Plan----------------------------------------------------------Plan hash value: 2122808611-------------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |-------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 110 | 4 (0)| 00:00:01 || 1 | NESTED LOOPS | | 1 | 110 | 4 (0)| 00:00:01 || 2 | NESTED LOOPS | | 1 | 110 | 4 (0)| 00:00:01 || 3 | MERGE JOIN CARTESIAN | | 1 | 52 | 4 (0)| 00:00:01 ||* 4 | TABLE ACCESS FULL | C | 1 | 26 | 2 (0)| 00:00:01 || 5 | BUFFER SORT | | 1 | 26 | 2 (0)| 00:00:01 ||* 6 | TABLE ACCESS FULL | B | 1 | 26 | 2 (0)| 00:00:01 ||* 7 | INDEX RANGE SCAN | INX_COL12A | 1 | | 0 (0)| 00:00:01 || 8 | TABLE ACCESS BY INDEX ROWID| A | 1 | 58 | 0 (0)| 00:00:01 |-------------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------4 - filter("C"."COL3"=5)6 - filter("B"."COL3"=10)7 - access("A"."COL1"="B"."COL1" AND "A"."COL2"="C"."COL2")Note------ dynamic sampling used for this statement (level=2)SQL>
注意当我们对表进行了分析之后,那么就不会有动态分析了,动态分析只是为了进行执行计划的选择。
对于分析表知识详解:
http://blog.csdn.net/xiaohai20102010/article/details/8777158
<pre>SQL> set autotrace offSQL> analyze table a compute statistics;Table analyzed.SQL> analyze table b compute statistics;Table analyzed.SQL> analyze table c compute statistics;Table analyzed.SQL> analyze index inx_col12A compute statistics;Index analyzed.SQL> select A.col42 from C , A , B3 where C.col3 = 5 and A.col1 = B.col1 and A.col2 = C.col24 and B.col3 = 10;no rows selectedSQL> set auotrace trace explainSP2-0158: unknown SET option "auotrace"SQL> set autotrace trace explainSQL> r1 select A.col42 from C , A , B3 where C.col3 = 5 and A.col1 = B.col1 and A.col2 = C.col24* and B.col3 = 10Execution Plan----------------------------------------------------------Plan hash value: 2122808611-------------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |-------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 110 | 4 (0)| 00:00:01 || 1 | NESTED LOOPS | | 1 | 110 | 4 (0)| 00:00:01 || 2 | NESTED LOOPS | | 1 | 110 | 4 (0)| 00:00:01 || 3 | MERGE JOIN CARTESIAN | | 1 | 52 | 4 (0)| 00:00:01 ||* 4 | TABLE ACCESS FULL | C | 1 | 26 | 2 (0)| 00:00:01 || 5 | BUFFER SORT | | 1 | 26 | 2 (0)| 00:00:01 ||* 6 | TABLE ACCESS FULL | B | 1 | 26 | 2 (0)| 00:00:01 ||* 7 | INDEX RANGE SCAN | INX_COL12A | 1 | | 0 (0)| 00:00:01 || 8 | TABLE ACCESS BY INDEX ROWID| A | 1 | 58 | 0 (0)| 00:00:01 |-------------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------4 - filter("C"."COL3"=5)6 - filter("B"."COL3"=10)7 - access("A"."COL1"="B"."COL1" AND "A"."COL2"="C"."COL2")</pre>
oracle sql 执行计划分析的更多相关文章
- Oracle sql执行计划解析
Oracle sql执行计划解析 https://blog.csdn.net/xybelieve1990/article/details/50562963 Oracle优化器 Oracle的优化器共有 ...
- 查看Oracle SQL执行计划的常用方式
在查看SQL执行计划的时候有很多方式 我常用的方式有三种 SQL> explain plan for 2 select * from scott.emp where ename='KING'; ...
- 分析 Oracle SQL 执行计划的关注点
本文内容摘自<剑破冰山--Oracle开发艺术>一书. 1.判定主要矛盾 在遇到复杂 SQL 语句时,执行计划也非常复杂,往往让人分析起来觉得无从下手,此时应避免顺序解决问题,而是快速定位 ...
- [转] 多种方法查看Oracle SQL执行计划
本文转自:http://falchion.iteye.com/blog/616234 一.在线查看执行计划表 如果PLAN_TABLE表不存在,执行$ORACLE_HOME/rdbms/admin/u ...
- SQL执行计划分析
explain执行计划中的字段以及含义在下面的博客中有详细讲述: https://blog.csdn.net/da_guo_li/article/details/79008016 执行计划能告诉我们什 ...
- Oracle sql执行计划
explain plan explain plan for sql_statement select * from table(dbms_xplan.display) DBMS_XPL ...
- SQL执行计划分析2
执行计划重点关注 type.key.key_len.rows.extra type:type如果为ALL,表示全盘扫描,也是效率最低的 key:表示使用了哪个索引,如果没有使用为null key_le ...
- Oracle查看SQL执行计划的方式
Oracle查看SQL执行计划的方式 获取Oracle sql执行计划并查看执行计划,是掌握和判断数据库性能的基本技巧.下面案例介绍了多种查看sql执行计划的方式: 基本有以下几种方式: ...
- sql执行计划变更和删除缓存中执行计划的方法
将指定SQL的执行计划从共享池删除的方法 http://www.2cto.com/database/201204/126388.html Oracle SQL执行计划变更的问题 http://www. ...
随机推荐
- cookie---session
//以下文字摘自慕课网教程..... 设置cookie PHP设置Cookie最常用的方法就是使用setcookie函数,setcookie具有7个可选参数,我们常用到的为前5个: name( Coo ...
- canvas刮刮乐效果(pc端&H5、zepto-touchmove)
一.html <div id="canvasArea" style="width:300px;height:200px;position:relative;&quo ...
- 利用JS判断是否手机或pad访问
<script type="text/javascript"> /* * 智能机浏览器版本信息: * */ var browser={ versions:functio ...
- Struts2返回json格式数据踩坑记录
事件起因 昨天提测修改冻结/解冻银行卡样式的功能,微姐测试过程中发现调用ajax请求耗时过长,今天来排查,发现浏览器请求/finance/ajax/freeze/ajaxGetShopLists时,对 ...
- jquery 杂记
返回指定属性名的属性值:getAttribute() 设置元素的属性值:attr('src',voiceurl) form表单: 序列化表单值: $('#formid').serialize() ...
- informatica读取FTP文件
以下为一个完整的informatica读取ftp文件,并导入到系统中. 第一步: 通过shell脚本下载压缩包文件 /server/infa_shared/crm_prod/shell/ftpFrom ...
- SQL Server中查询数据库及表的信息语句
/* -- 本文件主要是汇总了 Microsoft SQL Server 中有关数据库与表的相关信息查询语句. -- 下面的查询语句中一般给出两种查询方法, -- A方法访问系统表,适应于SQL 20 ...
- WebApp开发之--"rem"单位
随着web app的兴起,rem这是个低调的css单位,近一两年开始崭露头角,有许多朋友对于它的评价不一,有的在尝试使用,有的在使用过程中遇到坑就弃用了.但是我认为rem是用来做web app它绝对是 ...
- iOS中为什么block用copy属性
1. Block的声明和线程安全Block属性的声明,首先需要用copy修饰符,因为只有copy后的Block才会在堆中,栈中的Block的生命周期是和栈绑定的,可以参考之前的文章(iOS: 非ARC ...
- Javascript数组学习
记录下学习数组的过程 1.创建数组 var ary1 = new Array();//空数组 var ary2= [] ;//字面量 2.数组检测 //方法一 if(array instanceof ...