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


       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         18
    00007F64CC0825A0          0          1          1        233         10
    00007F64CC0825A0          0          1          1      95203          4
    00007F64CC0825A0          0          1          1       4571          3
    00007F64CC0825A0          0          1          1      95436          2
    00007F64CC0825A0          0          1          1      77851          2
    00007F64CC0825A0          0          1          1      52289          1
    00007F64CC0825A0          0          1          1      65536          1
    00007F64CC0825A0          1          2          2      42914          1
    00007F64CC0825A0          0          1          1      96368          1
    00007F64CC0825A0          0          1          1      57093          1
    
    ADDR                    TS#      FILE#    DBARFIL     DBABLK        TCH
    ---------------- ---------- ---------- ---------- ---------- ----------
    00007F64CC0825A0          0          1          1      22156          1
    00007F64CC0825A0          0          1          1      34704          1
    00007F64CC0825A0          0          1          1      17119          1
    00007F64CC0825A0          0          1          1      30133          1
    00007F64CC0825A0          0          1          1      38809          1
    00007F64CC0825A0          0          1          1      21224          1
    00007F64CC0825A0          0          1          1      17818          1
    00007F64CC0825A0          0          1          1      55928          1
    
    19 rows selected.
    
    SQL> set autotrace trace explain
    SQL> 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语句执行计划的区别。
  • 相关阅读:
    Linux下状态查看相关命令
    Linux免密登录理解
    Linux下远程访问&拷贝&下载
    初识NAT、桥接和Host-Only
    Linux文件和文件夹权限管理
    Linux用户和用户组的操作
    Mac+Parallels+iTerm安装配置Linux Centos7
    SSM整合事务失效
    Linux用户和用户组管理命令
    Linux中{ }的用法
  • 原文地址:https://www.cnblogs.com/pangblog/p/3362345.html
Copyright © 2020-2023  润新知