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>
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>
"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语句执行计划的区别。