forked from tanelpoder/tpt-oracle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_sql_baseline.sql
56 lines (45 loc) · 2.06 KB
/
create_sql_baseline.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-- Copyright 2018 Tanel Poder. All rights reserved. More info at http://tanelpoder.com
-- Licensed under the Apache License, Version 2.0. See LICENSE.txt for terms & conditions.
--------------------------------------------------------------------------------------------------------
-- this script allows you to transfer a plan from an existing (good) cursor to the problematic (bad) one.
-- usage:
--
-- @create_sql_baseline <from_good_sqlid> <good_sql_plan_hash_value> <to_bad_sqlid>
--
--
-- Query baseline info from: DBA_SQL_PLAN_BASELINES
-- Drop baselines: exec DBMS_SPM.DROP_SQL_PLAN_BASELINE('&sql_handle')
--
-- More info:
-- SQL Plan Baselines - DBMS_SPM in EE licenses - see http://jonathanlewis.wordpress.com/2011/01/12/fake-baselines/
--------------------------------------------------------------------------------------------------------
DEF good_sql_id = &1
DEF good_sql_phv = &2
DEF bad_sql_id = &3
DECLARE
ret NUMBER;
v_signature NUMBER; -- hash value of normalized SQL text
v_sql_text CLOB;
v_sql_handle VARCHAR2(100);
BEGIN
ret := DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE('&bad_sql_id', enabled=>'NO');
DBMS_OUTPUT.PUT_LINE(q'[Looking up SQL_ID &bad_sql_id]');
-- rownum = 1 because there may be multiple children with this SQL_ID
SELECT sql_fulltext, exact_matching_signature INTO v_sql_text, v_signature
FROM v$sql
WHERE
sql_id = '&bad_sql_id'
AND rownum = 1;
DBMS_OUTPUT.PUT_LINE('Found: '||SUBSTR(v_sql_text,1,80)||'...');
DBMS_OUTPUT.PUT_LINE(q'[Signature = ]'||v_signature);
SELECT sql_handle INTO v_sql_handle FROM dba_sql_plan_baselines WHERE signature = v_signature;
DBMS_OUTPUT.PUT_LINE(q'[Handle = ]'||v_sql_handle);
-- associate good SQL_IDs plan outline with the bad SQL_ID
ret := DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE(
sql_id => '&good_sql_id'
, plan_hash_value => &good_sql_phv
, sql_handle => v_sql_handle
);
DBMS_OUTPUT.PUT_LINE(q'[SQL Baseline Name = SQL_BASELINE_&1] return=]'||ret);
END;
/