[Click Here For PDF document, more readable doc]
- Database Upgrade
- Database / Schema Stats collection
- Change in environment ( LinkUnix to Linux Migration )
- Change in data
Case: You want to upgrade database from 10g to 11g and this change can cause regressions in SQL performance,and fixing them manually can be difficult and time consuming. Sql tuning can be used but, this is a reactive mechanism and cannot guarantee stable performance when drastic changes happen to the system. SQL tuning can only resolve performance issues after they have occurred and are identified. For example, a SQL statement may become a high-load statement due to a plan change, but this cannot be resolved by SQL tuning until after the plan change occurs.
Solution: Oracle 11g new features SQL plan management (SPM) records and evaluates the execution plans of SQL Statements over time, and builds SQL plan baselines composed of a set of existing plans known to be efficient. The SQL plan baselines are then used to preserve performance of corresponding SQL statements, regardless of changes occurring in the system.
Note:- Before upgrading database to 11g (11.1.0.6) , i have collect SQL Tuning set (STS) for 7 Day using "capture_cursor_cache_sqlset". By assuming that in 7 day all SQL stas will collected in STS. After that upgraded database to 11g ( from 10g 10.2.0.3) , i have used this STS to compare execution plan from 11g. Please check "SQL Performance Analyzer Part - 2"for steps to transfer STS one database to other database
1) Capturing SQL Plan Baselines
· Automatic Plan Capture ( 11g Only )
· Manual Plan Capture ( 10g and 11g )
2) Make Changes Upgrade Database / collect stats / Migrate Database to Linux
3) Upload SQL Plan Baseline
4) Enable the use of SQL plan baselines
5) Evolving SQL Plan Baselines
1) Capturing SQL Plan Baselines in oracle 10g before upgrading to 11g
a) Automatic Plan Capture(11g only)
When OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES set true , then database automatically creates and maintains the plan history for SQL statements using information provided by the optimizer.
>
In init.ora file OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES = true OR SQL>Alter system set OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES = true; |
Note : - Above automatic plan capture only work on 11g , if you want to capture plan in 10g , then check section “Manual Plan Capture”
b) Manual Plan Capture ( 10g and 11g )
Following script will capture sql plan in SQL tuning set for 7 Day ( this script will run for 7 day , you can change time according to your need )
exec dbms_sqltune.drop_sqlset('STS_RAG_CMUL'); DECLARE dbms_sqltune.capture_cursor_cache_sqlset(sts_cmul, END; |
capture_cursor_cache_sqlset : The procedure captures a workload from the cursor cache into a SQL tuning set, polling the cache multiple times over a time period and updating the workload data stored there. It can execute over as long a period as required to capture an entire system workload.
- time_limit : 604800 ( The total amount of time, in seconds, to execute , 7 day = 7 * 24 * 60 * 60 = 604800 )
- repeat_interval : 1 ( The amount of time, in seconds, to pause between sampling )
- capture_option During capture, either insert new statements, update existing ones, or both.
- 'INSERT'
- 'UPDATE',
- 'MERGE'
- capture_mode capture Option (UPDATE and MERGE capture options).
- MODE_REPLACE_OLD_STATS - Replace statistics when the number of executions
seen is greater than that stored in the SQL tuning set
MODE_ACCUMULATE_STATS - Add new values to current values for SQL we already store. Note that this mode detects if a statement has been aged out, so the final value for a statistics will be the sum of the statistics of all cursors that statement existed under.
The CAPTURE_CURSOR_CACHE_SQLSET function enables the capture of the full system workload by repeatedly polling the cursor cache over a specified interval. This function is a lot more efficient than repeatedly using the SELECT_CURSOR_CACHE and LOAD_SQLSET procedures to capture the cursor cache over an extended period of time. This function effectively captures the entire workload, as SQL Profiles opposed to the AWR—which only captures the workload of high-load SQL statements or the LOAD_SQLSET procedure, which accesses the data source only once.
2) Make Changes e.g. Upgrade Database / collect stats / Migrate Database to Linux Collect stats
exec dbms_stats.set_param('ESTIMATE_PERCENT','100'); exec dbms_stats.set_param('method_opt','FOR ALL COLUMNS SIZE 254'); exec dbms_stats.Gather_Database_Stats; |
3) Upload SQL Plan Baseline
If you have collected stats manually in SQL tuning sets ( STS) , then you need to
upload baseline from STS
-- Upload plan manually using dbms_spm variable pls number; exec :pls := dbms_spm.load_plans_from_sqlset(sqlset_name=>'STS_RAG_CMUL',- |
4) Enable the use of SQL plan baselines
To enable the use of SQL plan baselines, set the OPTIMIZER_USE_SQL_PLAN_BASELINES
initialization parameter to TRUE. By default, this parameter is set to TRUE.
--In init.ora file OPTIMIZER_USE_SQL_PLAN_BASELINES = TRUE OR |
5) Evolving SQL Plan Baselines
I have captured 10g plans in STS and after upgrading database to 11g uploaded those plans ,using dbms_spm.load_plans_from_sqlset. After uploading plans manually , I have set
OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES to true.
--Check not accepted Plans SQL> select sql_handle, plan_name, enabled, accepted, fixed SQL_HANDLE PLAN_NAME ENABLED ACCEPTED FIXED |
-- Displaying SQL Plan Baselines SQL> select * from table(dbms_xplan.display_sql_plan_baseline(sql_handle=>'SYS_SQL_13836d6b3da62bbb',format=>'basic')); -------------------------------------------------------------------------------- Plan hash value: 2877140902 -------------------------------------------------------------------------------- Plan hash value: 3245492848 ----------------------------------------- 42 rows selected. SQL> |
Evolving Plans with pls/sql function DBMS_SPM.EVOLVE_SQL_PLAN_BASELIN
--Evolving Plans With DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE SQL> DECLARE report := DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE( ------------------------------------------------------------------------------- Plan: SYS_SQL_PLAN_3da62bbbe5990995 Baseline Plan Test Plan Improv. Ratio ------------- --------- ------------- Execution Status: COMPLETE COMPLETE Number of SQL plan baselines verified: 1. |
Just for demo purpose , lets accept plan 2 ('SYS_SQL_PLAN_3da62bbbe5990995')
-- Acccept plan 2 ('SYS_SQL_PLAN_3da62bbbe5990995') and Fixed it SQL> exec :cnt := dbms_spm.alter_sql_plan_baseline(sql_handle => PL/SQL procedure successfully completed. -- Same pl/sql function used to fix plans PL/SQL procedure successfully completed. SQL> - - Check status of plans SQL> select sql_handle, plan_name, enabled, accepted, fixed from dba_sql_plan_baselines SQL_HANDLE PLAN_NAME ENABLED ACCEPTED FIXED |
>
-- Displaying SQL Plan Baselines after changes SQL> select * from table(dbms_xplan.display_sql_plan_baseline(sql_handle=>'SYS_SQL_13836d6b3da62bbb',format=>'basic')); -------------------------------------------------------------------------------- Plan hash value: 2877140902 ------------------------------------------------- Plan hash value: 3245492848 ----------------------------------------- |
Very well written post. I have no hesitation to say that you did excellent job. Your post is very informative & useful.
ReplyDeletesap project
Very Nice.Well Said. To the Point and Well Explained.
ReplyDeletetuning oracle database performance
oracle sql performance tuning
sql performance tuning
oracle sql performance tuning and optimization
improve sql query performance
oracle database performance tuning
oracle performance tuning tips
sql query performance tuning
sql tuning for oracle
performance tuning in oracle
oracle sql free download
ReplyDeleteThank you for sharing such valuable and helpful information and knowledge. Thank you for sharing the amazing post with us. Keep it up. I would love to see your next update.
Share Market Tips | Stock Market Tips
Very well-written post. I have no hesitation to say that you did an excellent job. Your post is very informative & useful.
ReplyDeletefor more info click on Charter Bus Houston
Charter Coach Bus