查看Oracle SQL语句执行计划
方法一:
1、explain plan for select * from emp where empno=7369; --解释执行计划,实际上未必执行。
2、select * from table(dbms_xplan.display);或者 select * from table(dbms_xplan.display());或者select * from table(dbms_xplan.display(null,null,'basic rows bytes'))
方法二:
查看V$SQL_PLAN --已经缓存的执行计划(实际执行)
方法三:AutoTrace
set autotrace on; --从plan_table中读取的
执行的语句。
set autotrace off;
方法四:SQL_TRACE 10046跟踪事件。
方法五:10053跟踪事件。
方法六:select * from table(dbms_xplan.display_cursor(null,null,'allstats last')); -- (实际执行)可用命令 desc dbms_xplan 查看参数 第一个参数 SQL_ID,第二个参数 CURSOR_CHILD_NO 第三个参数 FORMAT。可使用提示 gather_plan_statistics 提示Oracle收集信息。 select /*+ gather_plan_statistics*/* from AA t;
方法七:SQL监控报告:monitor 和 no_monitor 默认监视执行时间超过5秒的SQL语句,如果要监控执行时间小于5秒的SQL语句需要加monitor提示。
select /*+ monitor*/* from AA t;
select dbms_sqltune.report_sql_monitor() from dual;
影响SQL语句执行计划的因素--统计信息过期处理。
exec dbms_stats.gather_table_stats(ownname => 'owner',tabname => 'table_name' ,estimate_percent => 100 ,method_opt => 'for all columns size auto' ,cascade => true); --for all columns size auto 会收集列上的直方图信息。
analyze table table_name compute statistics for table for all indexes for all indexed columns;
例子如下:
exec dbms_stats.gather_table_stats(ownname => 'scott',tabname => 'A' ,estimate_percent => 100 ,method_opt => 'for all columns size auto' ,cascade => true);
analyze table scott.a compute statistics for table for all indexes for all indexed columns;
select * from dba_autotask_client; -- Oracle 11g 默认有自动收集统计信息的任务。