如何读懂执行计划

it2026-03-15  2

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.(每一张表都是被有效的访问)
最新回复(0)