验证 explain plan命令 与 set autotrace命令 是否为真实执行计划
0 CONN /AS SYSDBA;
1 create table t1 as select * from dba_objects;
2 insert into t1 select * from t1;
3 commit;
4 select count(1) from t1;
5 create index idx_t1 on t1(object_id);
--收集统计信息
6 exec dbms_stats.gather_table_stats(ownname=>'SYS',tabname=>'T1',estimate_percent=> 100,cascade=> true);
7 select * from table(dbms_xplan.display);
8
VAR X NUMBER;
VAR Y NUMBER;
EXEC :X :=0;
EXEC:Y :=100000;
--explain命令
EXPLAIN PLAN FOR SELECT count(*) from t1 where object_id between :x and :y;
select * from table(dbms_xplan.display);
显示走idx_t1索引范围(range)扫描
select count(*) from t1 where object_id between :x and :y;
--dbms_xplan.display_cursor(null,null,'ADVANCED') 得到真实执行计划
select * from table(dbms_xplan.display_cursor(null,null,'ADVANCED'));
Index fast full 快速全扫描
---set autotrace traceonly 验证
Set autotrace traceonly
Select count(*) from t1 where object_id between :x and :y;
显示走idx_t1索引范围(range)扫描
结论:使用set autotrace,set autotrace 源于explain plan是不准确的,特别是绑定变量下查询是不准确的
-dbms_xplan.display_cursor(null,null,'ADVANCED')
和10046事件获取真实的执行计划