首页 > 数据库 > Oracle > 查看SQL执行计划的方法(有待于进一步补充)
首先需要明确查看执行计划的目的:(摘自官方文档)
It is important to generate and review execution plans for all of the key SQL statements in your application. Doing so lets you compare the optimizer execution plans of a SQL statement when the statement performed well to the plan when that the statement is not performing well. Having the comparison, along with information such as changes in data volumes, can assist in identifying the cause of performance degradation.
答:第一种方法:explain plan for…,生成指定sql语句的执行计划,并把执行计划保存到用户指定的表中,供用户查询,但不作为真正运行时的执行计划,因为并没有保存在library cache中,得到的执行计划也不是library cache中的,所以不是真正的执行计划。
(1)SQL>explain plan for select * from emp
(2)SQL>select * from table(dbms_xplan.display);
第二种方法:SQL trace 和10046事件。查看trace文件,跟踪某个session的活动情况,跟踪的结果包括生成SQL语句的执行计划,可以用tkprof工具查看跟踪文件。执行计划有parse,bind,exec,fetch4个子过程,三个指标比较重要:disk(物理磁盘读取次数),query(一致性读),current(当前读)这3列,调整的目标也是降低这3个统计量。跟踪会话信息:
(1)exec dbms_support.start_trace_in_session(sid=>,serial=>,waits=>true,binds=>true),其中的sid和serial可以从v$session中获取。(2)从v$process中获取os的pid和oracle的pid.然后SQL>oradebug setospid …; SQL>oradebug setorapid…;
第三种方法:从library cache中获取,就是真正的执行计划。其实就是综合利用了v$sql_plan,v$sql_plan_statics(statics_level参数设为all),v$sql_workarea,v$sql_plan_statics_all这几张视图。可以对以上几张视图写查询,来获得特定sql语句真正的执行计划,也可以按照下面的步骤得到真正的执行计划。
(1) select /*+zhangxm*/ count(*) from table….
(2) Select sql_id from v$sql where sql_text like ‘% zhangxm %’; --得到这个语句的sql_id
(3) Select * from table(dbms_xplan.display_cursor(‘sql_id’,null,’basic’)); --以basic方式查
(4) Select * from table(dbms_xplan.display_cursor(‘sql_id’,null,’typical’));--以typical方式
(5) Select * from table(dbms_xplan.display_cursor(‘sql_id’,null,’all)); --以all方式查看
看其中的一些列,比如isstats,memstats,allstats:实际花费。A_time:语句执行时真正的花费。
还有一种:select * from table(dbms_xplan.display_awr(‘sql_id’));--调用display_awr,传入sql_id参数。
[@more@]来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24214296/viewspace-1035226/ ,如需转载,请注明出处,否则将追究法律责任。