您好,欢迎访问一九零五行业门户网

执行计划补充

优化sql需要建立在对执行计划深入理解的基础上,关于大部分的执行计划大家都有过接触,下面借助资料和测试用例补充一些比较少见的执行计划。 sql create table tab001 as select * from dba_objects; sql create index ind_owner001 on tab001(owner); sql c
优化sql需要建立在对执行计划深入理解的基础上,关于大部分的执行计划大家都有过接触,下面借助资料和测试用例补充一些比较少见的执行计划。
sql> create table tab001 as select * from dba_objects;
sql> create index ind_owner001 on tab001(owner);
sql> create index ind_object_type001 on tab001(object_type);
sql> exec dbms_stats.gather_table_stats(ownname=>user,tabname=>'tab001',cascade=>true);
多个b tree索引转换为bitmap,然互bitmap and或者bitmap or后再转换为rowid回表:
sql> select * from tab001 where object_type='lob' and owner='mdsys';
215 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 1145989034
-------------------------------------------------------------------------------------------------------
| id  | operation                        | name               | rows  | bytes | cost (%cpu)| time     |
-------------------------------------------------------------------------------------------------------
|   0 | select statement                 |                    |    21 |  2058 |    13   (0)| 00:00:01 |
|   1 |  table access by index rowid     | tab001             |    21 |  2058 |    13   (0)| 00:00:01 |
|   2 |   bitmap conversion to rowids    |                    |       |       |            |          |
|   3 |    bitmap and                    |                    |       |       |            |          |
|   4 |     bitmap conversion from rowids|                    |       |       |            |          |
|*  5 |      index range scan            | ind_object_type001 |  1000 |       |     3   (0)| 00:00:01 |
|   6 |     bitmap conversion from rowids|                    |       |       |            |          |
|*  7 |      index range scan            | ind_owner001       |  1000 |       |     5   (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
5 - access(object_type='lob')
   7 - access(owner='mdsys')
sql> select * from tab001 where object_type='lob' or owner='mdsys';
2808 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 4013421176
-------------------------------------------------------------------------------------------------------
| id  | operation                        | name               | rows  | bytes | cost (%cpu)| time     |
-------------------------------------------------------------------------------------------------------
|   0 | select statement                 |                    |  2803 |   268k|   291   (0)| 00:00:04 |
|   1 |  table access by index rowid     | tab001             |  2803 |   268k|   291   (0)| 00:00:04 |
|   2 |   bitmap conversion to rowids    |                    |       |       |            |          |
|   3 |    bitmap or                     |                    |       |       |            |          |
|   4 |     bitmap conversion from rowids|                    |       |       |            |          |
|*  5 |      index range scan            | ind_owner001       |       |       |     5   (0)| 00:00:01 |
|   6 |     bitmap conversion from rowids|                    |       |       |            |          |
|*  7 |      index range scan            | ind_object_type001 |       |       |     3   (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
5 - access(owner='mdsys')
   7 - access(object_type='lob')
看下视图合并,视图合并可以将视图外面的谓词条件推入到视图内部。
sql> create view view_tab001 as select * from tab001 where object_name='opw';
view created.
sql> select * from view_tab001 where object_type='cluster';
no rows selected
execution plan
----------------------------------------------------------
plan hash value: 2567763011
--------------------------------------------------------------------------------------------------
| id  | operation                   | name               | rows  | bytes | cost (%cpu)| time     |
--------------------------------------------------------------------------------------------------
|   0 | select statement            |                    |     1 |    98 |     2   (0)| 00:00:01 |
|*  1 |  table access by index rowid| tab001             |     1 |    98 |     2   (0)| 00:00:01 |
|*  2 |   index range scan          | ind_object_type001 |    16 |       |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - filter(object_name='opw')
   2 - access(object_type='cluster')
这里视图由于rownum的限制,导致无法将谓词推进到视图内部
sql> create or replace view view_tab001 as select * from tab001 where object_name='opw' and rownum
view created.
sql> select * from view_tab001 where object_type='cluster';
no rows selected
execution plan
----------------------------------------------------------
plan hash value: 770467758
-----------------------------------------------------------------------------------
| id  | operation           | name        | rows  | bytes | cost (%cpu)| time     |
-----------------------------------------------------------------------------------
|   0 | select statement    |             |     2 |   414 |   344   (1)| 00:00:05 |
|*  1 |  view               | view_tab001 |     2 |   414 |   344   (1)| 00:00:05 |
|*  2 |   count stopkey     |             |       |       |            |          |
|*  3 |    table access full| tab001      |     2 |   196 |   344   (1)| 00:00:05 |
-----------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - filter(object_type='cluster')
   2 - filter(rownum   3 - filter(object_name='opw')
statistics
----------------------------------------------------------
         18  recursive calls
          0  db block gets
       1258  consistent gets
          0  physical reads
          0  redo size
       1343  bytes sent via sql*net to client
        512  bytes received via sql*net from client
          1  sql*net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
          0  rows processed
filter是过滤的意思,这里我们列出一个子查询中的filter来简单说明下:
sql> select * from table(dbms_xplan.display_cursor('8uyp666q4gvg6',null,'allstats last'));
plan_table_output
--------------------------------------------------------------------------------------------------------------------------------------------
sql_id  8uyp666q4gvg6, child number 0
-------------------------------------
select /*+gather_plan_statistics*/* from tab001 a where a.object_type
in (select /*+no_unnest*/object_type from tab002 b where b.object_id
plan hash value: 1420963810
-----------------------------------------------------------------------------------------------------------
| id  | operation                    | name             | starts | e-rows | a-rows |   a-time   | buffers |
-----------------------------------------------------------------------------------------------------------
|   0 | select statement             |                  |      1 |        |   7822 |00:00:00.01 |    1862 |
|*  1 |  filter                      |                  |      1 |        |   7822 |00:00:00.01 |    1862 |
|   2 |   table access full          | tab001           |      1 |  86311 |  86311 |00:00:00.01 |    1751 |
|*  3 |   table access by index rowid| tab002           |     53 |      1 |      3 |00:00:00.01 |     111 |
|*  4 |    index range scan          | ind_objid_tab002 |     53 |      8 |    406 |00:00:00.01 |      58 |
-----------------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - filter( is not null)
   3 - filter(object_type=:b1)
   4 - access(b.object_id
tab001表返回了86311行数据,但是tab002表的ind_objid_tab002只走了53次index range scan,这里的filter是一种改良的nested loops,会对驱动结果集的做一个去重的动作,详细的可以参考之前写的文章 http://www.dbaxiaoyu.com/archives/2360
union和union all执行计划的区别:
sql> select object_id,object_name from tab001 a union all select object_id,object_name from tab002 b;
execution plan
----------------------------------------------------------
plan hash value: 910399089
-----------------------------------------------------------------------------
| id  | operation          | name   | rows  | bytes | cost (%cpu)| time     |
-----------------------------------------------------------------------------
|   0 | select statement   |        |   172k|  5057k|   687   (1)| 00:00:09 |
|   1 |  union-all         |        |       |       |            |          |
|   2 |   table access full| tab001 | 86311 |  2528k|   344   (1)| 00:00:05 |
|   3 |   table access full| tab002 | 86315 |  2528k|   344   (1)| 00:00:05 |
-----------------------------------------------------------------------------
sql> select object_id,object_name from tab001 a union  select object_id,object_name from tab002 b;
execution plan
----------------------------------------------------------
plan hash value: 4025140749
--------------------------------------------------------------------------------------
| id  | operation           | name   | rows  | bytes |tempspc| cost (%cpu)| time     |
--------------------------------------------------------------------------------------
|   0 | select statement    |        |   172k|  5057k|       |  2099   (1)| 00:00:26 |
|   1 |  sort unique        |        |   172k|  5057k|  6800k|  2099   (1)| 00:00:26 |
|   2 |   union-all         |        |       |       |       |            |          |
|   3 |    table access full| tab001 | 86311 |  2528k|       |   344   (1)| 00:00:05 |
|   4 |    table access full| tab002 | 86315 |  2528k|       |   344   (1)| 00:00:05 |
--------------------------------------------------------------------------------------
union和union all相比会多一个排序的动作,union对应的执行计划也确实多出了sort unique的执行计划。
connect by是oracle数据库中的层次关键字,对于这类查询xiaoyu接触的并不多,简单先讲讲connect by递归查询的业务逻辑含义
比如start with object_id=3 connect by prior object_id=data_object_id的含义为:取数据从满足start with object_id=3 数据开始,当然表中可能有多行满足这个条件,然后再找只要表中的下一行数据的data_object_id等于上一行的object_id就满足,依次递归的查询直到表中没有满足条件的数据后返回数据。
sql> create table tab_conn01 as select object_id,lag(object_id,1)over(order by object_id) data_object_id,object_name from tab001 where object_idsql> exec dbms_stats.gather_table_stats(ownname=>user,tabname=>'tab_conn01’);
sql> select * from tab_conn01;
object_id data_object_id object_name
---------- -------------- ------------------------------
         2                c_obj#
         3              2 i_obj#
         4              3 tab$
         5              4 clu$
         6              5 c_ts#
         7              6 i_ts#
         8              7 c_file#_block#
         9              8 i_file#_block#
        10              9 c_user#
9 rows selected.
sql> select a.*,level from tab_conn01 a start with object_id=3 connect by prior object_id=data_object_id;
object_id data_object_id object_name               level
---------- -------------- -------------------- ----------
         3              2 i_obj#                        1
         4              3 tab$                          2
         5              4 clu$                          3
         6              5 c_ts#                         4
         7              6 i_ts#                         5
         8              7 c_file#_block#                6
         9              8 i_file#_block#                7
        10              9 c_user#                       8
8 rows selected.
sql> select a.* ,level from tab_conn01 a start with object_id=3 connect by object_id= prior data_object_id;
object_id data_object_id object_name               level
---------- -------------- -------------------- ----------
         3              2 i_obj#                        1
         2                c_obj#                        2
关于connect by层次查询的业务逻辑就不再叙述,下面我们关注下这个查询的执行计划:
sql> select * from tab_conn01 start with object_id=3 connect by prior object_id=data_object_id;
8 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 896851045
------------------------------------------------------------------------------------------------------
| id  | operation                               | name       | rows  | bytes | cost (%cpu)| time     |
------------------------------------------------------------------------------------------------------
|   0 | select statement                        |            |     2 |   184 |   135   (1)| 00:00:02 |
|*  1 |  connect by no filtering with start-with|            |       |       |            |          |
|   2 |   table access full                     | tab_conn01 |     9 |   126 |   134   (0)| 00:00:02 |
------------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - access(tab_conn01.data_object_id=prior tab_conn01.object_id)
       filter(object_id=3)
statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
        476  consistent gets
          0  physical reads
          0  redo size
        869  bytes sent via sql*net to client
        523  bytes received via sql*net from client
          2  sql*net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
          8  rows processed
如果表中的数据稍微多点,然后递归查询返回的结果集多点,cbo就可能选择另一种执行计划
sql> create table tab_conn02 as select object_id,lag(object_id,1)over(order by object_id) data_object_id,object_name from dba_objects;
table created.
sql> exec dbms_stats.gather_table_stats(ownname=>user,tabname=>'tab_conn02');
pl/sql procedure successfully completed.
sql> select * from tab_conn02 start with object_id=3 connect by prior object_id=data_object_id;
86318 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 3789322130
----------------------------------------------------------------------------------------
| id  | operation                 | name       | rows  | bytes | cost (%cpu)| time     |
----------------------------------------------------------------------------------------
|   0 | select statement          |            |     2 |   184 |   406   (1)| 00:00:05 |
|*  1 |  connect by with filtering|            |       |       |            |          |
|*  2 |   table access full       | tab_conn02 |     1 |    35 |   135   (1)| 00:00:02 |
|*  3 |   hash join               |            |     1 |    48 |   269   (1)| 00:00:04 |
|   4 |    connect by pump        |            |       |       |            |          |
|   5 |    table access full      | tab_conn02 | 86319 |  2950k|   135   (1)| 00:00:02 |
----------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - access(tab_conn02.data_object_id=prior tab_conn02.object_id)
   2 - filter(object_id=3)
   3 - access(connect$_by$_pump$_002.prior object_id=data_object_id)
statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
   41087844  consistent gets
          0  physical reads
          0  redo size
    4137925  bytes sent via sql*net to client
      63817  bytes received via sql*net from client
       5756  sql*net roundtrips to/from client
      86320  sorts (memory)
          0  sorts (disk)
      86318  rows processed
cob预估上述这种执行计划相对高效,但是加上no_connect_by_filtering的hint后我们观察实际表扫描后connect by no filtering with start-with往往更加高效
sql> select /*+no_connect_by_filtering*/ * from tab_conn02 a start with object_id=3 connect by prior object_id=data_object_id;
86318 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 969721844
------------------------------------------------------------------------------------------------------
| id  | operation                               | name       | rows  | bytes | cost (%cpu)| time     |
------------------------------------------------------------------------------------------------------
|   0 | select statement                        |            |     2 |   184 |   938  (86)| 00:00:12 |
|*  1 |  connect by no filtering with start-with|            |       |       |            |          |
|   2 |   table access full                     | tab_conn02 | 86319 |  2950k|   135   (1)| 00:00:02 |
------------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - access(a.data_object_id=prior a.object_id)
       filter(object_id=3)
statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
        476  consistent gets
          0  physical reads
          0  redo size
    4137925  bytes sent via sql*net to client
      63817  bytes received via sql*net from client
       5756  sql*net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
      86318  rows processed
逻辑读从之前的四千多万的降低到了476,对于优化器的评估cost成本为什么和实际的sql的资源消耗如此大的差别,这种糟糕的执行计划究竟是如何工作的也没有找到合适的资料来进行解释,一般而言碰见这种connect by的sql语句反复多次扫描表段的sql语句都可以尝试添加hint no_connect_by_filteringl来让表能否扫描一次完成查询。
跟sort有关的执行计划:
sql> select sum(object_id) from tab_conn01 ;
execution plan
----------------------------------------------------------
plan hash value: 1562061203
---------------------------------------------------------------------------------
| id  | operation          | name       | rows  | bytes | cost (%cpu)| time     |
---------------------------------------------------------------------------------
|   0 | select statement   |            |     1 |     3 |   134   (0)| 00:00:02 |
|   1 |  sort aggregate    |            |     1 |     3 |            |          |
|   2 |   table access full| tab_conn01 |     9 |    27 |   134   (0)| 00:00:02 |
---------------------------------------------------------------------------------
statistics
----------------------------------------------------------
         14  recursive calls
          0  db block gets
        504  consistent gets
         12  physical reads
          0  redo size
        532  bytes sent via sql*net to client
        523  bytes received via sql*net from client
          2  sql*net roundtrips to/from client
          5  sorts (memory)
          0  sorts (disk)
          1  rows processed
sql> select avg(object_id) from tab_conn01;
execution plan
----------------------------------------------------------
plan hash value: 1562061203
---------------------------------------------------------------------------------
| id  | operation          | name       | rows  | bytes | cost (%cpu)| time     |
---------------------------------------------------------------------------------
|   0 | select statement   |            |     1 |     3 |   134   (0)| 00:00:02 |
|   1 |  sort aggregate    |            |     1 |     3 |            |          |
|   2 |   table access full| tab_conn01 |     9 |    27 |   134   (0)| 00:00:02 |
---------------------------------------------------------------------------------
statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
        476  consistent gets
          0  physical reads
          0  redo size
        532  bytes sent via sql*net to client
        523  bytes received via sql*net from client
          2  sql*net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
sql> select min(object_id) from tab_conn01;
execution plan
----------------------------------------------------------
plan hash value: 1562061203
---------------------------------------------------------------------------------
| id  | operation          | name       | rows  | bytes | cost (%cpu)| time     |
---------------------------------------------------------------------------------
|   0 | select statement   |            |     1 |     3 |   134   (0)| 00:00:02 |
|   1 |  sort aggregate    |            |     1 |     3 |            |          |
|   2 |   table access full| tab_conn01 |     9 |    27 |   134   (0)| 00:00:02 |
---------------------------------------------------------------------------------
statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
        476  consistent gets
          0  physical reads
          0  redo size
        532  bytes sent via sql*net to client
        523  bytes received via sql*net from client
          2  sql*net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
sql> select max(object_id) from tab_conn01;
execution plan
----------------------------------------------------------
plan hash value: 1562061203
---------------------------------------------------------------------------------
| id  | operation          | name       | rows  | bytes | cost (%cpu)| time     |
---------------------------------------------------------------------------------
|   0 | select statement   |            |     1 |     3 |   134   (0)| 00:00:02 |
|   1 |  sort aggregate    |            |     1 |     3 |            |          |
|   2 |   table access full| tab_conn01 |     9 |    27 |   134   (0)| 00:00:02 |
---------------------------------------------------------------------------------
statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
        476  consistent gets
          0  physical reads
          0  redo size
        532  bytes sent via sql*net to client
        523  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一般对应于聚合函数,statistics部分sort非0的值,也有0值,其实根据statistics部分的sort来判断一个sql是否排序是不正确的。
sql> alter system flush shared_pool;
system altered.
sql> select sum(object_id) from tab_conn01 ;
execution plan
----------------------------------------------------------
plan hash value: 1562061203
---------------------------------------------------------------------------------
| id  | operation          | name       | rows  | bytes | cost (%cpu)| time     |
---------------------------------------------------------------------------------
|   0 | select statement   |            |     1 |     3 |   134   (0)| 00:00:02 |
|   1 |  sort aggregate    |            |     1 |     3 |            |          |
|   2 |   table access full| tab_conn01 |     9 |    27 |   134   (0)| 00:00:02 |
---------------------------------------------------------------------------------
statistics
----------------------------------------------------------
         14  recursive calls
          0  db block gets
        504  consistent gets
          0  physical reads
          0  redo size
        532  bytes sent via sql*net to client
        523  bytes received via sql*net from client
          2  sql*net roundtrips to/from client
          5  sorts (memory)
          0  sorts (disk)
          1  rows processed
这里的sort部分是5,貌似都是在内存中排序,其实了sum求和是完全不需要排序的,同样的min、max和avg都是不需要排序的,有兴趣的同学可以用上面的方法验证下。
sql> select sql_id from v$sql where sql_text like 'select sum(object_id) from tab_conn01%';
sql_id
-------------
3tnr9w2gb10t3
sql> select * from table(dbms_xplan.display_cursor('3tnr9w2gb10t3',null,'advanced'));
plan_table_output
--------------------------------------------------------------------------------------------------------------------------------------------
sql_id  3tnr9w2gb10t3, child number 0
-------------------------------------
select sum(object_id) from tab_conn01
plan hash value: 1562061203
---------------------------------------------------------------------------------
| id  | operation          | name       | rows  | bytes | cost (%cpu)| time     |
---------------------------------------------------------------------------------
|   0 | select statement   |            |       |       |   134 (100)|          |
|   1 |  sort aggregate    |            |     1 |     3 |            |          |
|   2 |   table access full| tab_conn01 |     9 |    27 |   134   (0)| 00:00:02 |
---------------------------------------------------------------------------------
query block name / object alias (identified by operation id):
-------------------------------------------------------------
1 - sel$1
   2 - sel$1 / tab_conn01@sel$1
outline data
-------------
/*+
      begin_outline_data
      ignore_optim_embedded_hints
      optimizer_features_enable('11.2.0.4')
      db_version('11.2.0.4')
      all_rows
      outline_leaf(@sel$1)
      full(@sel$1 tab_conn01@sel$1)
      end_outline_data
  */
column projection information (identified by operation id):
-----------------------------------------------------------
1 - (#keys=0) sum(object_id)[22]
   2 - object_id[number,22]
这里的column projection information (identified by operation id):的 1 – (#keys=0) sum(“object_id”)[22]的#keys是0,这个表示这个sql是不完全不需要排序的。
order by一般对应于执行计划sort order by,如果没有索引可以利用这个排序是无法避免的,同样在column projection information (identified by operation id): 1 – (#keys=1) “tab_conn01″.”object_id”[number,22]的#keys是1。
sql> select * from tab_conn01 order by object_id;
object_id data_object_id object_name
---------- -------------- ------------------------------
         2                c_obj#
         3              2 i_obj#
         4              3 tab$
         5              4 clu$
         6              5 c_ts#
         7              6 i_ts#
         8              7 c_file#_block#
         9              8 i_file#_block#
        10              9 c_user#
9 rows selected.
sql> select sql_id from v$sql where sql_text like 'select * from tab_conn01 order by object_id%';
sql_id
-------------
3qzxfg5ycpz6t
sql> select * from table(dbms_xplan.display_cursor('3qzxfg5ycpz6t',null,'advanced'));
plan_table_output
--------------------------------------------------------------------------------------------------------------------------------------------
sql_id  3qzxfg5ycpz6t, child number 0
-------------------------------------
select * from tab_conn01 order by object_id
plan hash value: 525840827
---------------------------------------------------------------------------------
| id  | operation          | name       | rows  | bytes | cost (%cpu)| time     |
---------------------------------------------------------------------------------
|   0 | select statement   |            |       |       |   135 (100)|          |
|   1 |  sort order by     |            |     9 |   126 |   135   (1)| 00:00:02 |
|   2 |   table access full| tab_conn01 |     9 |   126 |   134   (0)| 00:00:02 |
---------------------------------------------------------------------------------
query block name / object alias (identified by operation id):
-------------------------------------------------------------
1 - sel$1
   2 - sel$1 / tab_conn01@sel$1
outline data
-------------
/*+
      begin_outline_data
      ignore_optim_embedded_hints
      optimizer_features_enable('11.2.0.4')
      db_version('11.2.0.4')
      all_rows
      outline_leaf(@sel$1)
      full(@sel$1 tab_conn01@sel$1)
      end_outline_data
  */
column projection information (identified by operation id):
-----------------------------------------------------------
1 - (#keys=1) tab_conn01.object_id[number,22],
       tab_conn01.object_name[varchar2,128],
       tab_conn01.data_object_id[number,22]
   2 - tab_conn01.object_id[number,22],
       tab_conn01.data_object_id[number,22],
       tab_conn01.object_name[varchar2,128]
44 rows selected.
来看看排序连接中的排序:
sql> select /*+use_merge(a b)*/* from tab_conn01 a,tab_conn01 b where a.object_id=b.object_id;
9 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 2830733959
----------------------------------------------------------------------------------
| id  | operation           | name       | rows  | bytes | cost (%cpu)| time     |
----------------------------------------------------------------------------------
|   0 | select statement    |            |     9 |   252 |   270   (1)| 00:00:04 |
|   1 |  merge join         |            |     9 |   252 |   270   (1)| 00:00:04 |
|   2 |   sort join         |            |     9 |   126 |   135   (1)| 00:00:02 |
|   3 |    table access full| tab_conn01 |     9 |   126 |   134   (0)| 00:00:02 |
|*  4 |   sort join         |            |     9 |   126 |   135   (1)| 00:00:02 |
|   5 |    table access full| tab_conn01 |     9 |   126 |   134   (0)| 00:00:02 |
----------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
4 - access(a.object_id=b.object_id)
       filter(a.object_id=b.object_id)
statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
        952  consistent gets
          0  physical reads
          0  redo size
       1231  bytes sent via sql*net to client
        523  bytes received via sql*net from client
          2  sql*net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
          9  rows processed
再来看一个笛卡尔积中涉及的buffer sort,这里的buffer sort也不是代表的排序,而是将查询返回的数据load到pga中。
sql> select * from tab_conn01 a,tab_conn01 b;
81 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 1615069149
-----------------------------------------------------------------------------------
| id  | operation            | name       | rows  | bytes | cost (%cpu)| time     |
-----------------------------------------------------------------------------------
|   0 | select statement     |            |    81 |  2268 |  1329   (1)| 00:00:16 |
|   1 |  merge join cartesian|            |    81 |  2268 |  1329   (1)| 00:00:16 |
|   2 |   table access full  | tab_conn01 |     9 |   126 |   134   (0)| 00:00:02 |
|   3 |   buffer sort        |            |     9 |   126 |  1195   (1)| 00:00:15 |
|   4 |    table access full | tab_conn01 |     9 |   126 |   133   (0)| 00:00:02 |
-----------------------------------------------------------------------------------
statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
        958  consistent gets
          0  physical reads
          0  redo size
       3525  bytes sent via sql*net to client
        578  bytes received via sql*net from client
          7  sql*net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
         81  rows processed
sql> select sql_id from v$sql where sql_text like 'select * from tab_conn01 a,tab_conn01 b%';
sql_id
-------------
5t664aq72ydva
sql> select * from table(dbms_xplan.display_cursor('5t664aq72ydva',null,'advanced'));
plan_table_output
--------------------------------------------------------------------------------------------------------------------------------------------
sql_id  5t664aq72ydva, child number 0
-------------------------------------
select * from tab_conn01 a,tab_conn01 b
plan hash value: 1615069149
-----------------------------------------------------------------------------------
| id  | operation            | name       | rows  | bytes | cost (%cpu)| time     |
-----------------------------------------------------------------------------------
|   0 | select statement     |            |       |       |  1329 (100)|          |
|   1 |  merge join cartesian|            |    81 |  2268 |  1329   (1)| 00:00:16 |
|   2 |   table access full  | tab_conn01 |     9 |   126 |   134   (0)| 00:00:02 |
|   3 |   buffer sort        |            |     9 |   126 |  1195   (1)| 00:00:15 |
|   4 |    table access full | tab_conn01 |     9 |   126 |   133   (0)| 00:00:02 |
-----------------------------------------------------------------------------------
query block name / object alias (identified by operation id):
-------------------------------------------------------------
1 - sel$1
   2 - sel$1 / a@sel$1
   4 - sel$1 / b@sel$1
outline data
-------------
/*+
      begin_outline_data
      ignore_optim_embedded_hints
      optimizer_features_enable('11.2.0.4')
      db_version('11.2.0.4')
      all_rows
      outline_leaf(@sel$1)
      full(@sel$1 a@sel$1)
      full(@sel$1 b@sel$1)
      leading(@sel$1 a@sel$1 b@sel$1)
      use_merge_cartesian(@sel$1 b@sel$1)
      end_outline_data
  */
column projection information (identified by operation id):
-----------------------------------------------------------
1 - a.object_id[number,22], a.data_object_id[number,22],
       a.object_name[varchar2,128], b.object_id[number,22],
       b.data_object_id[number,22], b.object_name[varchar2,128]
   2 - a.object_id[number,22], a.data_object_id[number,22],
       a.object_name[varchar2,128]
   3 - (#keys=0) b.object_id[number,22],
       b.data_object_id[number,22], b.object_name[varchar2,128]
   4 - b.object_id[number,22], b.data_object_id[number,22],
       b.object_name[varchar2,128]
在oracle 10g后,group by和distinct可以用hash group by和hash unique执行计划来完成查询,取代了原来的sort unique和sort group by的执行计划,新的算法不需要排序就可以完成查询。
sql> select distinct object_id from tab_conn01;
object_id
----------
         6
         2
         4
         5
         8
         3
         7
         9
        10
9 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 3127877997
---------------------------------------------------------------------------------
| id  | operation          | name       | rows  | bytes | cost (%cpu)| time     |
---------------------------------------------------------------------------------
|   0 | select statement   |            |     9 |    27 |   135   (1)| 00:00:02 |
|   1 |  hash unique       |            |     9 |    27 |   135   (1)| 00:00:02 |
|   2 |   table access full| tab_conn01 |     9 |    27 |   134   (0)| 00:00:02 |
---------------------------------------------------------------------------------
statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
        476  consistent gets
          0  physical reads
          0  redo size
        637  bytes sent via sql*net to client
        523  bytes received via sql*net from client
          2  sql*net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          9  rows processed
sql> select object_id from tab_conn01 group by object_id;
object_id
----------
         6
         2
         4
         5
         8
         3
         7
         9
        10
9 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 1614676255
---------------------------------------------------------------------------------
| id  | operation          | name       | rows  | bytes | cost (%cpu)| time     |
---------------------------------------------------------------------------------
|   0 | select statement   |            |     9 |    27 |   135   (1)| 00:00:02 |
|   1 |  hash group by     |            |     9 |    27 |   135   (1)| 00:00:02 |
|   2 |   table access full| tab_conn01 |     9 |    27 |   134   (0)| 00:00:02 |
---------------------------------------------------------------------------------
statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
        476  consistent gets
          0  physical reads
          0  redo size
        637  bytes sent via sql*net to client
        523  bytes received via sql*net from client
          2  sql*net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          9  rows processed
sql> select object_id from tab_conn01 group by object_id order by object_id;
object_id
----------
  &nb
其它类似信息

推荐信息