Skip to content

Commit c6e444e

Browse files
authored
SPM explain example (oracle-samples#154)
* security update * security update * security update * SPA helper example * Explain version script
1 parent aa596a1 commit c6e444e

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
This directory contains an example of deugging a SQL plan baseline in Oracle Database 19c.
22

3-
Note: these scripts use SQL performance analyzer.
4-
3+
Note: spb.sqlc and spb_noex.sql use SQL performance analyzer (SPA)
4+
55
The scripts make it easy to check SQL statements in the cursor cache.
66

77
You can set up an example by running test_setup.sql in a DBA account. Be aware it will drop and create SQL plan baselines. A SQL ID is displayed at the end of the script. You can enter this SQL ID when you run the "spb" scripts.
88

9-
In most cases, you can use spb_noex.sql - it explains the plan of the relevant SQL statement in the cursor cache.
9+
In most cases, you can use spb_explain.sql (EXPLAIN) or spb_noex.sql (SPA version) - which explain the plan of the relevant SQL statement in the cursor cache.
1010

11-
Alternatively, if you want to test execute the SQL statement, use spb.sql instead.
11+
Alternatively, if you want to parse and test execute the SQL statement, use spb.sql (uses SPA)
1212

1313
### DISCLAIMER
1414

@@ -20,6 +20,5 @@ Alternatively, if you want to test execute the SQL statement, use spb.sql instea
2020
### WARNING
2121

2222
* These scripts drop and create SQL plan baselines. For use on test databases
23-
* You need a license to use SQL performance analyzer to use these scripts
24-
* Check the license user manual for your database version
23+
* Check the license user manual for your database version if you want to use SPA versions
2524
* Oracle Database 19c: https://docs.oracle.com/en/database/oracle/oracle-database/19/dblic/
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
--
2+
-- This script explains a SQL statement in the cursor cache
3+
-- and produces a hint report - useful for diagnosing
4+
-- SQL plan baseline issues
5+
--
6+
set echo off
7+
set verify off
8+
set feedback off
9+
set long 10000000
10+
set pagesize 10000
11+
set linesize 250
12+
set trims on
13+
set tab off
14+
column report format a200
15+
16+
whenever sqlerror exit
17+
18+
var ccount number
19+
--
20+
-- Get the SQL ID to test
21+
--
22+
accept sqlid prompt 'Enter the SQL ID: '
23+
24+
--
25+
-- Check it's in cache
26+
--
27+
BEGIN
28+
select count(*) into :ccount from v$sqlarea where sql_id = '&sqlid';
29+
IF :ccount = 0
30+
THEN
31+
RAISE_APPLICATION_ERROR(-20002, 'SQL ID not found');
32+
END IF;
33+
END;
34+
/
35+
36+
--
37+
-- Spool the report
38+
--
39+
spool spm_report
40+
41+
alter session set "_sql_plan_management_control"=4;
42+
43+
--
44+
-- Explain plan
45+
--
46+
declare
47+
stmt clob;
48+
begin
49+
select sql_fulltext into stmt from v$sqlarea where sql_id = '&sqlid';
50+
execute immediate 'explain plan for '||stmt;
51+
end;
52+
/
53+
54+
alter session set "_sql_plan_management_control"=0;
55+
56+
select * from table(dbms_xplan.display(format=>'hint_report'));
57+
58+
spool off

0 commit comments

Comments
 (0)