步骤:javascript
- 1:explain plan for 你的SQL;
- 2:select * from table (dbms_xplan. display()) ;
sqlplus登陆:php
用户名/密码@主机名称:1521/数据库名
步骤:css
- 1:set sutoatrace on
- 2:在这次执行你的sql;
步骤:java
- 1:alter session set statistics_level=all;
- 2:在此处执行你的SQL;
- 3:select * from table(dbms_xplan.display_cursor(null , null,'allstats last'));
假如使用了Hint语法: /*+ gather_plan_statistics */,就能够省略步骤1,直接执行步骤2和3,获取执行计划python
关键字解读:sql
优势:数据库
步骤
从共享池获取bash
//${SQL_ID}参数能够从共享池拿 select * from table(dbms_xplan.display_cursor(${SQL_ID}));
还能够从AWR性能视图里获取session
select * from table(dbms_xplan.display_awr(${SQL_ID}));
多个执行计划的状况,能够用相似方法查出函数
select * from table(dbms_xplan.display_cursor(${SQL_ID},0)); select * from table(dbms_xplan.display_cursor(${SQL_ID},1));
优势:
缺点:
步骤:
1:alter session set events '10046 trace name context forever,level 12';//开启跟踪 2:执行你的语句 3:alter session set events '10046 trace name context off';//关闭跟踪 4:找到跟踪产生的文件 5:tkprof trc文件 目标文件 sys=no sort=prsela,exeela,fchela(格式化命令)
优势:
步骤:
1:@?/rdbms/admin/awrsqrpt.sql 具体能够参考我以前的博客:https://smilenicky.blog.csdn.net/article/details/89429989
能够分为两种类型:单独型和联合型
联合型分为:关联的联合型和非关联的联合型
单独型比较好理解,执行顺序是按照id=1,id=2,id=3执行,由远及近
先scott登陆,而后执行sql,例子来自《收获,不止SQL优化》一书
select deptno, count(*)
from emp where job = 'CLERK' and sal < 3000 group by deptno
因此能够给出单独型的图例:
这里使用Hint的nl
select /*+ ordered use_nl(dept) index(dept) */ * from emp, dept where emp.deptno = dept.deptno and emp.comm is null and dept.dname != 'SALES'
这图来自《收获,不止SQL优化》,能够看出id为2的A-Rows实践返回行数为10,id为3的Starts为10,说明驱动表emp访问的结果集返回多少条记录,被驱动表就被访问多少次,这是关联型的显著特征
前面已经介绍了联合型关联型(nl)这种方法的,这种方法是驱动表返回多少条记录,被驱动表就被访问了多少次,不过这种状况对于FILTER模式下并不适用
执行SQL,这里使用Hint /*+ no_unnset */
select * from emp where not exists (select /*+ no_unnset */ 0 from dept where dept.dname='SALES' and dept.deptno = emp.deptno) and not exists(select /*+ no_unnset */ 0 from bonus where bonus.ename = emp.ename)
ps:图来自《收获,不止SQL优化》一书,这里能够看出id为2的地方,A-Rows实际返回行数为8,而id为3的地方,Starts为3,说明对应SQL执行3次,也即dept被驱动表被访问了3次,这和刚才介绍的nl方式不一样,为何不一样?
查询一下SQL,能够看出实际返回3条,其它的都是重复多的,
select dname, count(*) from emp, dept where emp.deptno = dept.deptno group by dname;
因此,就很明显了,被过滤了重复数据,也就是说FILTER模式的对数据进行过滤,驱动表执行结果集返回多少行不重复数据,被驱动表就被访问多少次,FILTER模式能够说是对nl模式的改善
update emp e1 set sal = (select avg(sal) from emp e2 where e2.deptno = e1.deptno),comm = (select avg(comm) from emp e3)
联合型的关联型(UPDATE)和FILTER模式相似,因此就不重复介绍
select /*+ connect_by_filtering */ level, ename ,prior ename as manager from emp start with mgr is null connect by prior empno = mgr
给出联合型关联型图例:
能够执行SQL
select ename from emp union all select dname from dept union all select '%' from dual
对于plsql可使用工具查看执行计划,sqlplus客户端的可使用statistics_level=all的方法获取执行计划,具体步骤
- 1:alter session set statistics_level=all;
- 2:在此处执行你的SQL;
- 3:select * from table(dbms_xplan.display_cursor(null , null,'allstats last'));
能够给出联合型非关联型的图例: