执行计划小试牛刀
前要:
写完sql需要找DBA进行sql review,看执行计划是必不可少的步骤,一直不太懂执行计划,特此学习下。
1.什么是执行计划,为什么需要看懂执行计划?
对于sql优化来说,读懂执行计划是非常重要的,也是最关键的一步,因为执行计划就是oracle根据业务sql来生成的一种规则,用来查询到想要得到的数据。
执行一个sql语句,oracle database 需要几个步骤来完成。执行计划包含:每个表的读取方法,表的连接顺序。
2.如何查看执行计划:
explain plan for sql;
select plan_table_output FROM TABLE(DBMS_XPLAN.DISPLAY('PLAN_TABLE'));
或者
select * from table(dbms_xplan.display);
PS:查看执行计划有很多种方式,只是列举了其中一种方法。
3.小试牛刀:
①第一个sql:
select * from bus_fund_sell_confirm t where t.batch_id in (
select distinct t2.id from bus_fund_sell_request t1,bus_fund_batch t2 where t1.batch_id = t2.req_batch_id);
②第二个sql:
select distinct t3.* from bus_fund_sell_request t1,bus_fund_batch t2,bus_fund_sell_confirm t3 where t1.batch_id = t2.req_batch_id and t3.batch_id = t2.id
分别查看各自的执行计划:
①:explain plan for
select * from bus_fund_sell_confirm t where t.batch_id in (
select distinct t2.id from bus_fund_sell_request t1,bus_fund_batch t2 where t1.batch_id = t2.req_batch_id);
得到:
②:explain plan for (
select distinct t3.* from bus_fund_sell_request t1,bus_fund_batch t2,bus_fund_sell_confirm t3 where t1.batch_id = t2.req_batch_id and t3.batch_id = t2.id);
得到
OK,下面就需要介绍一下里面列出的这写鬼东西到底是什么含义?
4.含义讲解:
字段解释:
ID: 执行序列,但不是执行的先后顺序。执行的先后根据Operation缩进来判断(采用最右最上最先执行的原则看层次关系,在同一级如果某个动作没有子ID就最先执行。 一般按缩进长度来判断,缩进最大的最先执行,如果有2行缩进一样,那么就先执行上面的。)
Operation: 当前操作的内容。
Rows: 当前操作的Cardinality,Oracle估计当前操作的返回结果集。
Cost(CPU):Oracle 计算出来的一个数值(代价),用于说明SQL执行的代价。
Time:Oracle 估计当前操作的时间。
------------------------------------------------------------------------------------------
谓词说明:
Access: 表示这个谓词条件的值将会影响数据的访问路劲(表还是索引)。
Filter:表示谓词条件的值不会影响数据的访问路劲,只起过滤的作用。
在谓词中主要注意access,要考虑谓词的条件,使用的访问路径是否正确。
-----------------------------------------------------------------------------------------
明显可以看出上面的第一种方式效率更高,只进行了两次全表扫描。
5.总结:
随手一记,不正之处,欢迎指正。因为对sql优化略感兴趣,后续学习更新,算知道了什么是执行计划。
转载于:https://my.oschina.net/dingaolin/blog/744235