4. Motivation
• Performance problem (regression) on a critical SQL
statement
• Users claim that this SQL has been in place for a long
period of time
• Developers claim they haven’t made any changes to the
SQL
• You are responsible of making that SQL run with
acceptable performance
• You have the tools but you don’t know
• Sometimes(?) the problem is reported
at 1 am in the morning or Friday 5 pm
5. Agenda
• Cost Based Optimizer
• SQL Profiles
- SQL Tuning Advisor
- COE script (custom SQL Profiles)
• Oracle SQL Plan Management
- SPM Framework
- Enable SQL plan baseline capture
- Evolve plans to stabilize performance
- Pack, Load and Unpack baselines
• Oracle Real Time Monitoring
- Usage
- OEM 12c
6. Cost Based Optimizer
• Cost Based Optimizer CBO (since Oracle 7)
– Determines the most efficient way to execute a SQL statement
after considering:
• Parameters
• Statistics
• Others (Hints, Outlines, Index Selectivity, etc.)
– Produces an optimal execution plan most of the times
– Setup CBO to where most SQL plans are optimal
– We want the CBO to be flexible enough to produce optimal plans
as needed
8. Cost Based Optimizer
• Plan Flexibility Vs Plan Stability
– Flexibility
• Goal is to allow the CBO to produce plans
(optimal/suboptimal)
• CBO Statistics and Histograms, Parameters, Bind Peeking,
Cardinality Feedback CFB, Adaptive Cursor Sharing, SQL
profiles STA
• Expect execution plan changes
http://bitkode.blogspot.com/2013/05/why-execution-plan-changed-case-study.html
– Stability
• Goal is to allow only optimal plans to be executed
• Outlines, Hints, SQL Plan Management, Custom SQL profiles
SQLT
10. Agenda
• Cost Based Optimizer
• SQL Profiles
- SQL Tuning Advisor
- COE script (custom SQL Profiles)
• Oracle SQL Plan Management
- SPM Framework
- Enable SQL plan baseline capture
- Evolve plans to stabilize performance
- Pack, Load and Unpack baselines
• Oracle Real Time Monitoring
- Usage
- OEM 12c
11. SQL Profiles
• Is a database object
• Contains auxiliary statistics specific to a SQL statement (not
to a plan)
• One time statistics
• If the SQL Profile changes, new plans are created
• Over time, profile content can be outdated
• It requires Diagnostics and Tuning license!
• Internally, a profile is implemented using hints
– OPT_ESTIMATE(@SEL$1, TABLE, TEST@SEL$1, SCALE_ROWS=30)
– TABLE_STATS(“SCOTT”, “TEST”, scale, blocks=3, rows=30)
– INDEX_STATS(“SCOTT”, “TEST”, “EMP_IDX”, scale, blocks=3,
rows=30)
12. SQL Profiles
• Can be created using:
– SQL Tuning Advisor
– Manually, to *lock a plan*
• The Oracle COE script
• Provided by SQLT. Support Note Id 215187.1
• coe_xfr_sql_profile.sql (Kerry’s site)
• SQL Profiles apply to the following statement types:
– SELECT statements
– UPDATE statements
– INSERT statements (only with a SELECT clause)
– DELETE statements
– CREATE TABLE statements (only with the AS SELECT clause)
– MERGE statements (the update or insert operations)
17. SQL Profiles
• SQL Tuning Advisor
• When the Automatic SQL Tuning task is configured to
implement SQL profiles automatically
• Behavior depends on the setting of the ACCEPT_SQL_PROFILE
19. SQL Profiles
• ACCEPT_SQL_PROFILE
– If set to TRUE, then the advisor implements SQL profiles automatically.
– If set to FALSE, then user intervention is required.
– If set to AUTO (default), then the setting is true when at least one SQL
statement exists with a SQL profile, and false when this condition is not
satisfied.
• Settings of Auto task from OEM 12c
20. SQL Profiles
• Settings of Auto task from command line
COL PARAMETER_NAME FORMAT a25
COL VALUE FORMAT a10
SELECT PARAMETER_NAME, PARAMETER_VALUE AS "VALUE"
FROM DBA_ADVISOR_PARAMETERS
WHERE ( (TASK_NAME = 'SYS_AUTO_SQL_TUNING_TASK') AND
( (PARAMETER_NAME LIKE '%PROFILE%') OR (PARAMETER_NAME =
'LOCAL_TIME_LIMIT') OR (PARAMETER_NAME = 'EXECUTION_DAYS_TO_EXPIRE')
) );
PARAMETER_NAME VALUE
------------------------- ----------
EXECUTION_DAYS_TO_EXPIRE 30
LOCAL_TIME_LIMIT 1000
ACCEPT_SQL_PROFILES FALSE
MAX_SQL_PROFILES_PER_EXEC 20
MAX_AUTO_SQL_PROFILES 10000
21. SQL Profiles
• Settings of Auto task from command line
BEGIN DBMS_SQLTUNE.SET_TUNING_TASK_PARAMETER
( task_name => 'SYS_AUTO_SQL_TUNING_TASK' ,
parameter => parameter_name ,
value => value ); END; /
BEGIN
DBMS_SQLTUNE.SET_TUNING_TASK_PARAMETER('SYS_AUTO_SQL_TUNING_TAS
K', 'LOCAL_TIME_LIMIT', 1200);
DBMS_SQLTUNE.SET_TUNING_TASK_PARAMETER('SYS_AUTO_SQL_TUNING_TAS
K', 'ACCEPT_SQL_PROFILES', 'true');
DBMS_SQLTUNE.SET_TUNING_TASK_PARAMETER('SYS_AUTO_SQL_TUNING_TAS
K', 'MAX_SQL_PROFILES_PER_EXEC', 50);
DBMS_SQLTUNE.SET_TUNING_TASK_PARAMETER('SYS_AUTO_SQL_TUNING_TAS
K', 'MAX_AUTO_SQL_PROFILES', 10002); END; /
22. SQL Profiles
• COE Script
– Identify a plan change in a SQL statement
SELECT SQL_ID, PLAN_HASH_VALUE, CHILD_NUMBER FROM V$SQL WHERE
SQL_ID=‘azcb6w2rc2fwk’;
SQL_ID PLAN_HASH_VALUE CHILD_NUMBER
------------- --------------- ------------
azcb6w2rc2fwk 1378164845 3
azcb6w2rc2fwk 648432385 4
azcb6w2rc2fwk 648432385 2
azcb6w2rc2fwk 1378164845 0
azcb6w2rc2fwk 1378164845 1
azcb6w2rc2fwk 1378164845 5
– Plan 648432385 is the “good” plan but is not longer executed!
23. SQL Profiles
• Download COE script
– Provided by SQLT. Support Note Id 215187.1
– coe_xfr_sql_profile.sql
$@coe_xfr_sql_profile
Parameter 1:
SQL_ID (required)
Enter value for 1: azcb6w2rc2fwk
PLAN_HASH_VALUE AVG_ET_SECS
--------------- -----------
648432385 .006
1378164845 2.03
Parameter 2:
PLAN_HASH_VALUE (required)
Enter value for 2: 648432385
Values passed:
~~~~~~~~~~~~~
SQL_ID : "azcb6w2rc2fwk"
PLAN_HASH_VALUE: "648432385"
Execute coe_xfr_sql_profile_azcb6w2rc2fwk_648432385.sql
on TARGET system in order to create a custom SQL Profile
with plan 648432385 linked to adjusted sql_text.
25. Agenda
• Cost Based Optimizer
• SQL Profiles
- SQL Tuning Advisor
- COE script (custom SQL Profiles)
• Oracle SQL Plan Management
- SPM Framework
- Enable SQL plan baseline capture
- Evolve plans to stabilize performance
- Pack, Load and Unpack baselines
• Oracle Real Time Monitoring
- Usage
- OEM 12c
26. SPM Framework
• SQL Plan Management
– New feature in 11g+ for plan stability and control
– Only selected plans (Accepted and Enabled) are executed
– New plans are captured and stored but not executed (default)
• SQL Plan Control Flags
– Enabled (YES/NO)
• Either alive or deleted
– Accepted (YES/NO)
• If Enabled and Accepted will be executed; if not Accepted can
be Evolved
– Fixed (YES/NO) “only if accepted and enabled”
• Plans have priority over Accepted plans and are not expected
to change
27. SPM Framework
• SQL Plan Baseline
– Group/Set of Accepted plans
• Plan History
– Group/Set of Accepted and not-Accepted plans
• Plan Evolution
– Process of making Enabled plans Accepted
• SQL Management Base (SMB)
– Set of database objects that store Plan History, SQL Plan Baseline
and SQL Profiles
29. SPM Framework
• Signature
– Unique SQL identifier (hash) from the normalized SQL text
(uncased and whitespaces removed)
• DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE
• SQL Plan Status Flags
– Reproduced (YES/NO)
• CBO can or cannot reproduce the plan
– Autopurge (YES/NO)
• If YES the plan will be purged if not used, NO never purged
– Rejected (YES/NO)
• Plan was Unaccepted (Accepted = NO) & has LAST_VERIFIED
date or ENABLED=NO
30. SPM Framework
• SQL Plan Management Initialization Parameters
– OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES
• FALSE by default. Controls the automatic creation of SQL plan
baselines
– OPTIMIZER_USE_SQL_PLAN_BASELINES
• TRUE by default. Controls the use of SQL plan baselines
31. SPM Framework
• SQL Plan Management has three main components
– Plan Capture
• Automatic Plan Capture
• Manual Plan Capture
– Plan Selection
– Plan Evolution
• Automatic Plan Evolution
• Manual Plan Evolution
32. Setup SPM – Plan Capture
• Automatic Plan Capture
– OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES = TRUE
– More than one SQL execution
33. Setup SPM – Plan Capture
• Manual Plan Capture
– Most common method to capture plans
– PL/SQL package DBMS_SPM or Enterprise Manager 12c
– Four different sources
• Cursor Cache
• SQL Tuning Set
• Stored Outlines
• Another DB System
– Loaded plans are automatically Accepted
34. Setup SPM – Plan Capture
• Manual Plan Capture
35. Setup SPM – Plan Capture
• Manual Plan Capture
36. Setup SPM – Plan Capture
• Manual Plan Capture
37. Setup SPM – Plan Selection
• OPTIMIZER_USE_SQL_PLAN_BASELINES=TRUE (Default)
38. Setup SPM – Plan Evolution
• Accept or reject plans
– Automatic Plan Evolution
• SQL Tuning Advisor task (11g)
• SPM Evolve Advisor (SYS_AUTO_SPM_EVOLVE_TASK) (12c)
– Manual Plan Evolution
• DBMS_SPM.ALTER_SQL_PLAN_BASELINE (11gR1)
• DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE function (11gR2)
• SPM Evolve Advisor API (12c)
• DBMS_SPM.CREATE_EVOLVE_TASK
• DBMS_SPM.EXECUTE_EVOLVE_TASK
• DBMS_SPM.REPORT_EVOLVE_TASK
• DBMS_SPM.ACCEPT_SQL_PLAN_BASELINE
39. Setup SPM – Plan Evolution
• Adaptive SQL Plan Management – Automatic Plan Evolution
(12c)
– Enabled by default
– When automatic SQL tuning is in COMPREHENSIVE mode, it runs a
verification or evolve process for all SQL statements that have non-
accepted plans during the nightly maintenance window.
– If the non-accepted plan performs better (1.5x default) than the
existing accepted plan (or plans) in the SQL plan baseline, then the
plan is automatically accepted and becomes usable by the optimizer.
– After the task is complete, a persistent report is generated detailing
how the non-accepted plan performs compared to the accepted
plan performance.
40. Setup SPM – Plan Evolution
• Manual Plan Evolution
– 11gR1
41. Setup SPM – Plan Evolution
• Manual Plan Evolution
– Display execution plans for a baseline
– Parameters:
• sql_handle =>
• plan_name =>
• format => ‘BASIC’, ‘TYPICAL’ & ‘ALL’
43. Setup SPM – Plan Evolution
• Manual Plan Evolution
– 11gR2
44. Setup SPM – Plan Evolution
• Manual Plan Evolution
– 11gR2
– VERIFY (YES/NO) compare the performance before changing non-
accepted plans into accepted plans
• When set to 'YES', the non-accepted plan(s) will be accepted if
verified to give better performance
• When set to 'NO', the non-accepted plan(s) will be changed to
accepted plan(s). (11gR1 like)
– COMMIT (YES/NO)
• When set to 'YES', baseline will be updated to ACCEPTED status
and a report will be generated
• When set to 'NO', a report will be generated without actually
changing the status of the baseline(s) to ACCEPTED.
45.
46. Setup SPM – Plan Evolution
• Manual Plan Evolution
– 12c
– Create evolve task
– Execute evolve task
47. Setup SPM – Plan Evolution
• Manual Plan Evolution
– Generate report
– Manual execution of SPM EVOLVE ADVISOR does NOT accept any
SQL plan baseline!!
48. Setup SPM – Plan Evolution
• Manual Plan Evolution
– Accept the plan baseline recommended by the advisor
– If you try to accept the plan baseline which is NOT recommended by
SPM EVOLVE ADVISOR, it will fail with ORA-38149.
– You can accept the plan baseline which is NOT recommended by SPM
EVOLVE ADVISOR, by setting force to TRUE.
– How to Manually Evolve and Accept SQL PLAN BASELINES in Oracle
12c (Doc ID 1939828.1)
51. Setup SPM
• Pack, Load and Unpack baselines
– 1. On the original system, create a staging table using the
DBMS_SPM.CREATE_STGTAB_BASELINE procedure
– 2. Pack the SQL plan baselines you want to export from the SQL
management base into the staging table using the
DBMS_SPM.PACK_STGTAB_BASELINE function.
– 3. Export the staging table into a flat file using the export
command or Oracle Data Pump.
– 4. Transfer this flat file to the target system.
– 5. Import the staging table from the flat file using the import
command or Oracle Data Pump.
– 6. Unpack the SQL plan baselines from the staging table into the
SQL management base on the target system using the
DBMS_SPM.UNPACK_STGTAB_BASELINE function.
53. SPM Administration
• SPM Plan Retention
– Weekly scheduled task that runs as an automated task in the
maintenance window
– Purges plans that have not been used for longer than the plan
retention period
– Default retention is 53 weeks and can be setup from 5 to 523
weeks
54. SPM Administration
• Manage SBM space limit
– Weekly background process measures the total space occupied by
the SMB in the SYSAUX tablespace, default 10%
– Process writes a warning message to the alert log
– Fix by increase SBM space limit, increase SYSAUX size or purge
baselines
55. SPM Administration
• Licensing
– SPM is available as part of Oracle Database Enterprise Edition
– Includes any of the DBMS_SPM procedures
• But, below features require the Tuning Pack
– Automatic Plan Evolution for SQL Plan Management STA
– Load plans from SQL Tuning Sets into SPM, STS can only be created
if you have Tuning pack or RAT pack
• Maria Colgan’s Blog
https://blogs.oracle.com/optimizer/entry/does_the_use_of_sql
56. SPM Administration
• Acceptance threshold in the evolve process
– Controlled by hidden parameter
– How to Evolve a SQL Plan Baseline and Adjust the Acceptance
Threshold (Doc ID 1617790.1)
57. Agenda
• Cost Based Optimizer
• SQL Profiles
- SQL Tuning Advisor
- COE script (custom SQL Profiles)
• Oracle SQL Plan Management
- SPM Framework
- Enable SQL plan baseline capture
- Evolve plans to stabilize performance
- Pack, Load and Unpack baselines
• Oracle Real Time Monitoring
- Usage
- OEM 12c
58. Oracle Real-Time SQL Monitoring
• Introduced in 11g
• By default, started automatically when a SQL runs in
parallel or has consumed at least 5 seconds of CPU and/or
I/O combined in a single execution
• Can be forced using the hint /*+ MONITOR */
• Requires STATISTICS_LEVEL set to ‘TYPICAL’ or ‘ALL’
• Diagnostic and Tuning license
• 2 System Views
– V$SQL_MONITOR
– V$SQL_PLAN_MONITOR
• 2 Packages
– DBMS_SQLTUNE.report_sql_monitor_list
– DBMS_SQLTUNE.report_sql_detail