/* * vplesnila 2025-05 - creation * Based on Vishal Gupta script * and other documents: * - https://www.linkedin.com/pulse/efficiently-converting-sql-statements-sqlids-oracle-database-pandey-fwmff * - https://franckpachot.medium.com/oracle-dba-sql-plan-baseline-sql-id-and-plan-hash-value-8ffa811a7c68 * * * Usage: @sql_plan_baselines.sql * Examples: * @sql_plan_baselines.sql "1=1" * @sql_plan_baselines.sql "accepted='YES' and Last_Modified >(sysdate-1)" * @sql_plan_baselines.sql "plan_hash_value=237994299" * @sql_plan_baselines.sql "sql_id='g3gwhhx6r00qc'" */ COLUMN sql_id HEADING "SQL_ID" FORMAT a14 COLUMN sql_handle HEADING "SQL Handle" FORMAT a20 COLUMN plan_hash_value HEADING 'Plan|Hash Value' FORMAT 9999999999 COLUMN plan_name HEADING "Plan Name" FORMAT a30 COLUMN enabled HEADING "Ena|bled" FORMAT a4 COLUMN accepted HEADING "Accep|ted" FORMAT a5 COLUMN fixed HEADING "Fixed" FORMAT a5 COLUMN reproduced HEADING "Repro|duced" FORMAT a5 COLUMN autopurge HEADING "Auto|Purge" FORMAT a5 COLUMN optimizer_cost HEADING "Optimizer|Cost" FORMAT 99999999 COLUMN creator HEADING "Creator" FORMAT a13 COLUMN created HEADING "Created" FORMAT a19 COLUMN last_modified HEADING "LastModified" FORMAT a19 COLUMN last_executed HEADING "LastExecuted" FORMAT a19 COLUMN last_verified HEADING "LastVerified" FORMAT a19 COLUMN force_matching HEADING "Force|Match" FORMAT a5 COLUMN signature HEADING "Signature" FORMAT 999999999999999999999 COLUMN description HEADING "Description" FORMAT a50 BREAK ON name ON CATEGORY ON created ON last_modified ON type ON status ON force_matching ON signature ON description select * from ( SELECT dbms_sql_translator.sql_id(sql_text) sql_id ,(select case when instr(plan_table_output, ':') > 0 then to_number(substr(plan_table_output, instr(plan_table_output, ':', -1) + 1)) else to_number(plan_table_output) end as last_value from ( select * from table(dbms_xplan.DISPLAY_SQL_PLAN_BASELINE(sql_handle,plan_name)) where plan_table_output like 'Plan hash value: %' ) ) plan_hash_value , spb.sql_handle , spb.plan_name , spb.origin , spb.enabled , spb.accepted , spb.fixed , spb.reproduced , spb.autopurge , spb.optimizer_cost , spb.creator , to_char(spb.created,'YYYY-MM-DD HH24:MI:SS') created , to_char(spb.last_modified,'YYYY-MM-DD HH24:MI:SS') last_modified , to_char(spb.last_executed,'YYYY-MM-DD HH24:MI:SS') last_executed , to_char(spb.last_verified,'YYYY-MM-DD HH24:MI:SS') last_verified , spb.description FROM dba_sql_plan_baselines spb ) WHERE &1 ORDER BY last_modified DESC ;