如何读懂执行计划

1、执行计划概念
一个执行计划描述了一段SQL语句建议的执行方法,该 计划显示ORACLE数据库的执行sql时步骤的组合。 每一步都得到数据库中的物理数据行戒准备为他们的 用户发布的声明

2、生成执行计划
使用语句生成:

explain plan for SELECT * from ods_employe; 

显示执行计划:

select * from table(dbms_xplan.display); 

PLSQL窗口显示

  • 在PLSQL工具中,按F5戒者新建Explain Plan Window
  • 在窗口中输入SQL,执行后则输出该SQL的执行计划
select * from ods_employe; 

3、看懂执行计划

select 
 t2.month_name,        
 t1.depart_name,     
 t.employee_name,       
 sum(t2.sale_amount) as sale_amount   
from ods_employee t, ods_depart t1, ods_sales t2   
where 1=1   
and t.employee_id=t2.employee_id   
and t.depart_num=t1.depart_num   
and t.employee_id<10  
group by t2.month_name, t1.depart_name, t.employee_name 

在这里插入图片描述
该执行计划阅读步骤(从右到左,从上到下):

  • 首先使用索引扫描的方式访问ODS_EMPLOYEE_N1,再通过 索引对应的ROWID访问表取出9行记录
  • 然后使用全表扫描的方式访问ODS_DEPART表,返回19行记 录。
  • 把1和2的结果集用HASH JOIN连接起来,返回9行记录
  • 使用全表扫描的方式访问ODS_SALES表,使用过滤条件 EMPLOYEE_ID<10,返回7777行记录
  • 将两个结果集用HASH JOIN链接
  • 最后使用HASH GROUP BY

4、检查执行计划

  • The plan is such that the driving table has the best
    filter.(保证驱劢表有好的筛选条件)
  • The join order in each step means that the fewest number of rows are
    being returned to the next step (that is, the join order should
    reflect, where possible, going to the best not-yet-used
    filters).(每一步的连接顺序返回最小的行数给下一步)
  • The join method is appropriate for the number of rows being returned.
    For example, nested loop joins through indexes may not be optimal
    when many rows are being returned.(使用合适的连接方法,举个例子,nested
    loop连接是通过索引,不适合用于结果集返回多行的情况)
  • Views are used efficiently. Look at the SELECT list to see whether
    access to the view is necessary.(视图使用要有效,避免使用复杂视图)
  • There are any unintentional Cartesian products (even with small
    tables).(不要使用笛卡尔积,尽管是小表)
  • Each table is being accessed efficiently.(每一张表都是被有效的访问)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值