码迷,mamicode.com
首页 > 数据库 > 详细

Oracle中如何判断SQL是否真正做了排序

时间:2014-12-26 11:14:06      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:

   在Oracle中,有时候在执行计划中看到sort关键字,就有人认为是做了排序,其实不一定。可以用下面的方法实验:

drop table test purge;

create table test as select * from dba_objects;
exec dbms_stats.gather_table_stats(user,‘test‘);

select /*+gg_test*/* from test order by object_id;
select /*+gg_test*/* from test order by object_id,object_name;
select /*+gg_test*/* from test order by object_id,object_name,object_type;

--#keys表示返回的结果中排序的字段数量
SQL>select projection
  from v$sql_plan_statistics_all
 where sql_id in(select sql_id from v$sql where 
   sql_text like‘select /*+gg_test*/* from test order by object_id ‘)
and operation=‘SORT‘ and options=‘ORDER BY‘;
PROJECTION
-----------------------------------------------------------------------
(#keys=1) "TEST"."OBJECT_ID"[NUMBER,22], "TEST"."OWNER"[VARCHAR2,30], "TEST"."OBJECT_NAME"[VARCHAR2,128], 
"TEST"."SUBOBJECT_NAME"[VARCHAR2,30], "TEST"."EDITION_NAME"[VARCHAR2,30], 
"TEST"."DATA_OBJECT_ID"[NUMBER,22], "TEST"."OBJECT_TYPE"[VARCHAR2,19], "TEST"."CREATED"[DATE,7],
"TEST"."LAST_DDL_TIME"[DATE,7], "TEST"."TIMESTAMP"[VARCHAR2,19], "TEST"."STATUS"[VARCHAR2,7],
"TEST"."TEMPORARY"[VARCHAR2,1], "TEST"."GENERATED"[VARCHAR2,1], "TEST"."SECONDARY"[VARCHAR2,1],
"TEST"."NAMESPACE"[NUMBER,22]

SQL>select projection
  from v$sql_plan_statistics_all
 where sql_id in(select sql_id from v$sql where 
   sql_text=‘select /*+gg_test*/* from test order by object_id,object_name ‘)
and operation=‘SORT‘ and options=‘ORDER BY‘;
PROJECTION
-----------------------------------------------------------------------
(#keys=2) "TEST"."OBJECT_ID"[NUMBER,22], "TEST"."OBJECT_NAME"[VARCHAR2,128], "TEST"."OWNER"[VARCHAR2,30],
"TEST"."NAMESPACE"[NUMBER,22], "TEST"."SUBOBJECT_NAME"[VARCHAR2,30], "TEST"."EDITION_NAME"[VARCHAR2,30], 
"TEST"."DATA_OBJECT_ID"[NUMBER,22], "TEST"."OBJECT_TYPE"[VARCHAR2,19], "TEST"."CREATED"[DATE,7], 
"TEST"."LAST_DDL_TIME"[DATE,7], "TEST"."TIMESTAMP"[VARCHAR2,19], "TEST"."STATUS"[VARCHAR2,7], 
"TEST"."TEMPORARY"[VARCHAR2,1], "TEST"."GENERATED"[VARCHAR2,1], "TEST"."SECONDARY"[VARCHAR2,1]

SQL>select projection
  from v$sql_plan_statistics_all
 where sql_id in(select sql_id from v$sql where 
   sql_text=‘select /*+gg_test*/* from test order by object_id,object_name,object_type ‘)
and operation=‘SORT‘ and options=‘ORDER BY‘;
PROJECTION
-----------------------------------------------------------------------
(#keys=3) "TEST"."OBJECT_ID"[NUMBER,22], "TEST"."OBJECT_NAME"[VARCHAR2,128], 
"TEST"."OBJECT_TYPE"[VARCHAR2,19], "TEST"."OWNER"[VARCHAR2,30], "TEST"."NAMESPACE"[NUMBER,22], 
"TEST"."SUBOBJECT_NAME"[VARCHAR2,30], "TEST"."EDITION_NAME"[VARCHAR2,30], 
"TEST"."DATA_OBJECT_ID"[NUMBER,22], "TEST"."SECONDARY"[VARCHAR2,1], "TEST"."CREATED"[DATE,7], 
"TEST"."LAST_DDL_TIME"[DATE,7], "TEST"."TIMESTAMP"[VARCHAR2,19], "TEST"."STATUS"[VARCHAR2,7], 
"TEST"."TEMPORARY"[VARCHAR2,1], "TEST"."GENERATED"[VARCHAR2,1]

Oracle中如何判断SQL是否真正做了排序

标签:

原文地址:http://blog.csdn.net/stevendbaguo/article/details/42168261

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!