博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql执行计划解析案例(二)
阅读量:7073 次
发布时间:2019-06-28

本文共 5657 字,大约阅读时间需要 18 分钟。

   sql执行计划解析案例(二)

 
今天是2013-10-09,本来以前自己在专注oracle sga中buffer cache 以及shared pool知识点的研究。但是在研究cache buffer chain的时候发现了一个语句:
select * from (select addr,ts#,file#,dbarfil,dbablk,tch from x$bh order by tch desc) where rownum<20,当时觉得很诧异,为什么要这么写呢?就是这一个语句让我开始接触了少许sql执行计划的知识,对于sql优化是一门艺术,走的路还 是很长,但是对于现在的我还是踏实一步一步走的好。现在分析如下执行计划。
SQL> select addr,ts#,file#,dbarfil,dbablk,tch from x$bh where rownum<20 order by tch desc  2  ;ADDR                    TS#      FILE#    DBARFIL     DBABLK        TCH---------------- ---------- ---------- ---------- ---------- ----------00007F64CC0825A0          0          1          1       8210         1800007F64CC0825A0          0          1          1        233         1000007F64CC0825A0          0          1          1      95203          400007F64CC0825A0          0          1          1       4571          300007F64CC0825A0          0          1          1      95436          200007F64CC0825A0          0          1          1      77851          200007F64CC0825A0          0          1          1      52289          100007F64CC0825A0          0          1          1      65536          100007F64CC0825A0          1          2          2      42914          100007F64CC0825A0          0          1          1      96368          100007F64CC0825A0          0          1          1      57093          1ADDR                    TS#      FILE#    DBARFIL     DBABLK        TCH---------------- ---------- ---------- ---------- ---------- ----------00007F64CC0825A0          0          1          1      22156          100007F64CC0825A0          0          1          1      34704          100007F64CC0825A0          0          1          1      17119          100007F64CC0825A0          0          1          1      30133          100007F64CC0825A0          0          1          1      38809          100007F64CC0825A0          0          1          1      21224          100007F64CC0825A0          0          1          1      17818          100007F64CC0825A0          0          1          1      55928          119 rows selected.SQL> set autotrace trace explainSQL> r  1  select addr,ts#,file#,dbarfil,dbablk,tch from x$bh where rownum<20 order by tch desc  2*Execution Plan----------------------------------------------------------Plan hash value: 2913638504---------------------------------------------------------------------------| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |---------------------------------------------------------------------------|   0 | SELECT STATEMENT   |      |    19 |  1349 |     1 (100)| 00:00:01 ||   1 |  SORT ORDER BY     |      |    19 |  1349 |     1 (100)| 00:00:01 ||*  2 |   COUNT STOPKEY    |      |       |       |            |          ||   3 |    FIXED TABLE FULL| X$BH |    19 |  1349 |     0   (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------   2 - filter(ROWNUM<20)SQL>
该sql的执行顺序为id3-》id2—》id1-》id0,首先对固定表x$bh进行全表扫描,完了之后进行统计所有数据(没有拍过续的),这时候的过滤条件为:filter(ROWNUM<20),然后执行到id1的时候在对这20条数据进行排序,但是这个时候安装tch排序的是在一个大的表中抽取的20条然后再进行排序,并不能找到对数据块操作最频繁的块。
eg:

SQL> select * from (select addr,ts#,file#,dbarfil,dbablk,tch from x$bh order by tch desc) where rownum<20   2  ;

Execution Plan ---------------------------------------------------------- Plan hash value: 2453498899

-------------------------------------------------------------------------------- | Id  | Operation               | Name | Rows  | Bytes | Cost (%CPU)| Time     | -------------------------------------------------------------------------------- |   0 | SELECT STATEMENT        |      |    19 |  1349 |     1 (100)| 00:00:01 | |*  1 |  COUNT STOPKEY          |      |       |       |            |          | |   2 |   VIEW                  |      |   100 |  7100 |     1 (100)| 00:00:01 | |*  3 |    SORT ORDER BY STOPKEY|      |   100 |  7100 |     1 (100)| 00:00:01 | |   4 |     FIXED TABLE FULL    | X$BH |   100 |  7100 |     0   (0)| 00:00:01 | --------------------------------------------------------------------------------

Predicate Information (identified by operation id): ---------------------------------------------------

   1 - filter(ROWNUM<20)    3 - filter(ROWNUM<20)

SQL> set autotrace off SQL> r   1  select * from (select addr,ts#,file#,dbarfil,dbablk,tch from x$bh order by tch desc) where rownum<20   2*

ADDR                    TS#      FILE#    DBARFIL     DBABLK        TCH ---------------- ---------- ---------- ---------- ---------- ---------- 00007F64CBFCD840          0          1          1       2017        162 00007F64CBFCD840          0          1          1       2016        161 00007F64CBFCD840          0          1          1       3025         54 00007F64CBFCD840          0          1          1       3073         50 00007F64CBFCD840          0          1          1        385         50 00007F64CBFCD840          0          1          1        169         50 00007F64CBFCD840          0          1          1        345         49 00007F64CBFCD840          0          1          1       3057         49 00007F64CBFCD840          0          1          1        337         49 00007F64CBFCD840          0          1          1        481         49 00007F64CBFCD840          0          1          1      46461         48

ADDR                    TS#      FILE#    DBARFIL     DBABLK        TCH ---------------- ---------- ---------- ---------- ---------- ---------- 00007F64CBFCD840          0          1          1       2945         46 00007F64CBFCD840          0          1          1        489         43 00007F64CBFCD840          0          1          1        170         42 00007F64CBFCD840          0          1          1        577         42 00007F64CBFCD840          0          1          1       1625         41 00007F64CBFCD840          0          1          1        490         41 00007F64CBFCD840          0          1          1       2946         41 00007F64CBFCD840          0          1          1        386         41

19 rows selected.

SQL>

对于该sql的执行计划就有意思了。执行顺序为:id4-》id3-》id2-》id1-》id0,首先也是对固定表x$bh进行全表扫描,然后通过对停止键进行排序过滤出排序过后的20条数据; 3 - filter(ROWNUM<20),完了之后把结果集作为一个view,然后再去统计所有的数据过滤条件为:1 - filter(ROWNUM<20)。其实对于order by在此是做了一个排序,看下面这句话:搜索引擎会简单的搜索这个表,然后缓存一部分数据到cache这个就是20,然后再去做比较,并取代较小的值,取出20条。
"the run-time engine simply scanned the table, keeping a cache of the top
10 values. It didn’t really sort 1,000,000 rows, it merely checked each row to see if it was larger
than the smallest item in the current cache and should replace it. At the end of the scan, it only
had 10 rows to sort."
这就是这两个sql语句执行计划的区别。

 

转载地址:http://jskml.baihongyu.com/

你可能感兴趣的文章
Java连接Oracle数据库简单实例
查看>>
Exchange2010 dag 的部署
查看>>
Linux/UNIX的scp命令用法详解
查看>>
Eclipse(MyEclipse)插件Jigloo的下载与安装
查看>>
软件设计的思想与哲学
查看>>
非常实用的linux系统命令
查看>>
NFS在Centos 6.3下的安装
查看>>
git pull 和本地文件冲突解决
查看>>
iOS音频AAC视频H264编码 推流最佳方案
查看>>
python基础教程(第2版)第五章读后总结;
查看>>
关于在eclipse中使用tomcat的笔记
查看>>
Android自定义控件实现简单的轮播图控件
查看>>
centos 6.4下的samba服务器的构建
查看>>
持续交付:价值主张
查看>>
二进制、八进制、十进制、十六进制之间转换
查看>>
sqlmap 本地安装
查看>>
[计算机术语]缺省
查看>>
JS --事件
查看>>
printStream 和printWriter区别
查看>>
Centos6.6搭建中文版本的Cacti监控
查看>>