explain plan获取sql的执行计划

本文深入探讨SQL执行计划的工作实质、前提条件及操作方法,包括如何通过`explainplan`命令预估执行计划并将其加载到`plan_table`中,以及如何通过设置标记位来跟踪特定SQL语句的执行计划。并通过创建测试表来演示获取执行计划的过程。

explain plan工作实质、前提及操作方法
     1. 工作实质
      将SQL语句预估的执行计划加载到表plan_table,是对表plan_table 执行了DML操作,故不会执行隐式提交
      可以对select,insert,update,merge,delete,create table, create index,alter index等加载执行计划到plan_table

     2. 前提条件
      需要先创建plan_table,创建方法:@?/rdbms/admin/utlxplan
      对当前的SQL语句有执行权限以及对依赖的对象有相应操作的权限

     3. 使用方法:
       explain plan for select * from scott.emp where ename='SCOTT';    --未设置标记位
       explain plan set statement_id='TEST' for select * from scott.emp where ename='SCOTT'  --设置标记位为TEST

练习

1.创建测试表演示获取执行计划

scott@ORCL> create table t as select * from all_objects where rownum<=1000;                                      
                                                                                                                
Table created.                                                                                                   
                                                                                                                
--加载创建表的执行计划(DDL 执行计划)                                                                              
scott@ORCL> explain plan set statement_id='T1' for create table t1 as select * from t;                           
                                                                                                                 
Explained.                                                                                                       
                                                                                                                
--对plan_table执行一个层次查询获取执行计划                                                                         
col OPERATION format a25                                                                                         
col OPTIONS format a25                                                                                           
col OBJECT_NAME format a25                                                                                       
SELECT lpad(' ', 2 * (LEVEL - 1)) || operation operation                                                         
    ,options                                                                                                     
    ,object_name                                                                                                 
    ,position pos                                                                                                
    ,bytes                                                                                                       
    ,cost                                                                                                        
FROM plan_table                                                                                                  
START WITH id = 0                                                                                                
AND statement_id =upper( '&input_statement_id')                                                                  
CONNECT BY PRIOR id = parent_id;                                                                                 
                                                                                                                
Enter value for input_statement_id: T1                                                                           
old   9: AND statement_id =upper( '&input_statement_id')                                                         
new   9: AND statement_id =upper( 'T1')                                                                          
                                                                                                                 
OPERATION                 OPTIONS                   OBJECT_NAME                      POS      BYTES       COST   
------------------------- ------------------------- ------------------------- ---------- ---------- ----------    
CREATE TABLE STATEMENT                                                                 8      79000          8   
  LOAD AS SELECT                                    T1                                 1                         
    TABLE ACCESS          FULL                      T                                  1      79000          5   
                                                                                                                 
---为了更好的查看执行计划可以使用dbms_xplan包如下
select * from table(dbms_xplan.display());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值