6.4.3比较执行计划:教程
要比较计划,请使用DBMS_XPLAN.COMPARE_PLANS
功能。
在本教程中,您将比较两个不同的查询。比较计划报告显示,优化程序能够在一个查询中使用联接消除转换,但在另一个查询中不能使用。
假设条件
本教程假定用户sh
发出了以下查询:
select count(*) from products p, sales s where p.prod_id = s.prod_id and p.prod_min_price > 200; select count(*) from products p, sales s where p.prod_id = s.prod_id and s.quantity_sold = 43;
复制
比较执行计划:
- 启动SQL * Plus,然后以管理特权登录数据库。
- 查询
V$SQL
以确定两个查询的SQL ID。以下查询查询
V$SQL
包含字符串的查询products
:SET LINESIZE 120 COL SQL_ID FORMAT a20 COL SQL_TEXT FORMAT a60 SELECT SQL_ID, SQL_TEXT FROM V$SQL WHERE SQL_TEXT LIKE '%products%' AND SQL_TEXT NOT LIKE '%SQL_TEXT%' ORDER BY SQL_ID; SQL_ID SQL_TEXT -------------------- ------------------------------------------------------------ 0hxmvnfkasg6q select count(*) from products p, sales s where p.prod_id = s.prod_id and s.quantity_sold = 43 10dqxjph6bwum select count(*) from products p, sales s where p.prod_id = s.prod_id and p.prod_min_price > 200
复制 - 以user身份登录数据库
sh
。 - 执行
DBMS_XPLAN.COMPARE_PLANS
函数,指定在上一步中获得的SQL ID。 (进行比较两SQL的执行计划)例如,执行以下程序:
VARIABLE v_rep CLOB BEGIN :v_rep := DBMS_XPLAN.COMPARE_PLANS( reference_plan => cursor_cache_object('0hxmvnfkasg6q', NULL), compare_plan_list => plan_object_list(cursor_cache_object('10dqxjph6bwum', NULL)), type => 'TEXT', level => 'TYPICAL', section => 'ALL'); END; /
复制 - 打印报告。
例如,运行以下查询:
SET PAGESIZE 50000 SET LONG 100000 SET LINESIZE 210 COLUMN report FORMAT a200 SELECT :v_rep REPORT FROM DUAL;
复制Comparison Results
以下示例报告的这一节显示只有第一个查询使用了联接消除转换:REPORT --------------------------------------------------------------------------------------------- COMPARE PLANS REPORT --------------------------------------------------------------------------------------------- Current user : SH Total number of plans : 2 Number of findings : 1 --------------------------------------------------------------------------------------------- COMPARISON DETAILS --------------------------------------------------------------------------------------------- Plan Number : 1 (Reference Plan) Plan Found : Yes Plan Source : Cursor Cache SQL ID : 0hxmvnfkasg6q Child Number : 0 Plan Database Version : 19.0.0.0 Parsing Schema : "SH" SQL Text : select count(*) from products p, sales s where p.prod_id = s.prod_id and s.quantity_sold = 43 Plan ----------------------------- Plan Hash Value : 3519235612 ------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost | Time | ------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 469 | | <--- 从查询块中删除转换JOIN(结果查询块:SEL $ A43D1678) | 1 | SORT AGGREGATE | | 1 | 3 | | | 查当于把 select count(*) from products p, sales s where p.prod_id = s.prod_id and s.quantity_sold = 43 | 2 | PARTITION RANGE ALL | | 1 | 3 | 469 | 00:00:01 | 改写成了
select count(*)
sales s where s.quantity_sold = 43 | * 3 | TABLE ACCESS FULL | SALES | 1 | 3 | 469 | 00:00:01 | ------------------------------------------------------------------------- Predicate Information (identified by operation id): ------------------------------------------ * 3 - filter("S"."QUANTITY_SOLD"=43) --------------------------------------------------------------------------------------------- Plan Number : 2 Plan Found : Yes Plan Source : Cursor Cache SQL ID : 10dqxjph6bwum Child Number : 0 Plan Database Version : 19.0.0.0 Parsing Schema : "SH" SQL Text : select count(*) from products p, sales s where p.prod_id = s.prod_id and p.prod_min_price > 200 Plan ----------------------------- Plan Hash Value : 3037679890 -------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost | Time | -------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 34 | | | 1 | SORT AGGREGATE | | 1 | 13 | | | | * 2 | HASH JOIN | | 781685 | 10161905 | 34 | 00:00:01 | | * 3 | TABLE ACCESS FULL | PRODUCTS | 61 | 549 | 2 | 00:00:01 | | 4 | PARTITION RANGE ALL | | 918843 | 3675372 | 29 | 00:00:01 | | 5 | BITMAP CONVERSION TO ROWIDS | | 918843 | 3675372 | 29 | 00:00:01 | | 6 | BITMAP INDEX FAST FULL SCAN | SALES_PROD_BIX | | | | | -------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): ------------------------------------------ * 2 - access("P"."PROD_ID"="S"."PROD_ID") * 3 - filter("P"."PROD_MIN_PRICE">200) Notes ----- - This is an adaptive plan Comparison Results (1): ----------------------------- 1. Query block SEL$1: Transformation JOIN REMOVED FROM QUERY BLOCK occurred only in the reference plan (result query block: SEL$A43D1678). 查询块1: 仅在参考计划中发生了从查询块中删除转换JOIN(结果查询块:SEL $ A43D1678)。复制
也可以看看:
《 Oracle数据库PL / SQL软件包和类型参考》以获取有关该DBMS_XPLAN
软件包的 更多信息
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
Oracle Concepts(Oracle 19c):07 SQL
Ryan Bai
1009次阅读
2025-04-09 10:57:11
Oracle RAC 一键安装翻车?手把手教你如何排错!
Lucifer三思而后行
548次阅读
2025-04-15 17:24:06
【纯干货】Oracle 19C RU 19.27 发布,如何快速升级和安装?
Lucifer三思而后行
469次阅读
2025-04-18 14:18:38
Oracle SQL 执行计划分析与优化指南
Digital Observer
446次阅读
2025-04-01 11:08:44
XTTS跨版本迁移升级方案(11g to 19c RAC for Linux)
zwtian
445次阅读
2025-04-08 09:12:48
墨天轮个人数说知识点合集
JiekeXu
443次阅读
2025-04-01 15:56:03
【ORACLE】记录一些ORACLE的merge into语句的BUG
DarkAthena
438次阅读
2025-04-22 00:20:37
【ORACLE】你以为的真的是你以为的么?--ORA-38104: Columns referenced in the ON Clause cannot be updated
DarkAthena
413次阅读
2025-04-22 00:13:51
Oracle数据库一键巡检并生成HTML结果,免费脚本速来下载!
陈举超
411次阅读
2025-04-20 10:07:02
Oracle 19c RAC更换IP实战,运维必看!
szrsu
393次阅读
2025-04-08 23:57:08