SlideShare una empresa de Scribd logo
1 de 4
Descargar para leer sin conexión
PE RFORMANC E
                                                                                                            CORN E R




    SQL Plan Management
   for Performance Testing
                                           by Maris Elsins
                                                                                                              Maris Elsins

    This article is the third in a series by Pythian experts that is   the optimizer produces different execution plans in each data-
being published as the “Performance Corner” column in the              base. This problem is partially addressed by SPM, but the risk
NoCOUG Journal.                                                        still remains if the implemented change does not allow repro-



S
         QL Plan Management (SPM) is a feature introduced              ducing any of the SQL plans in the baseline (e.g., an index was
         with Oracle Database 11gR1 that effectively addresses         dropped) or the change introduces new SQL statements that
         the long-standing problem of unpredictable effects            don’t have any baselines yet.
         caused on the performance of SQL statements by the                Another possible reason for unexpected behavior after im-
changes of optimizer statistics, system configuration, and             plementation of the change is incomplete testing— only the
schema objects, or the patching and upgrading of the database          changed functionality was tested or testing was done using
software. In many cases the cause of the degraded performance          automated testing tools that don’t have the test cases for testing
is the different execution plan produced by the optimizer for          the full scope of functionality. In such cases, the testing can
the same statement, compared to the one used before.                   miss problems introduced in the parts of the application that
    SPM solves the problem by capturing the execution plan             were not tested. It is also possible the testing did not miss the
history and creating plan baselines that contain all accepted          change, but it was not noticed because the overall workload in
execution plans for the statement; the optimizer is aware of           the test system was lower and the issue did not reveal itself.
SPM and allows only accepted plans in the baseline to be used              It is often hard to assess the quality of testing and, as the
for execution of the SQL statement. This mechanism prevents            DBAs are unaware of all of the changes being implemented, it’s
the use of any unverified execution plan, thereby eliminating          nearly impossible to estimate the performance impact of the
the unpredictable degradation of the performance. Maria                change using tools like AWR or ASH. It is hard to involve DBAs
Colgan has explained how SPM works in a series of articles             in the active testing process, and usually the involvement stops
starting here:                                                         with troubleshooting of the discovered performance issues.
    https://blogs.oracle.com/optimizer/entry/sql_plan_                 On the other hand, DBAs are usually the first to be called for
management_part_1_of_4_creating_sql_plan_baselines.                    help when the change causes a severe performance issue in
    In addition to the main purpose of SPM, it also can be used        production, but by then it’s too late—the business is already
in different scenarios, such as preserving the execution plans         suffering.
of SQL statements while upgrading the database (http://docs.               In this article, I will describe a method for tracking down
oracle.com/cd/E11882_01/server.112/e23633/preup.                       SQL statements introduced with the application change and
htm#UPGRD00232) or forcing the use of a different execu-               SQL statements that have different execution plans compared
tion plan by creating a “fake baseline” (http://jonathanlewis.         to ones used in production before the change. This method
wordpress.com/2011/01/12/fake-baselines/).                             allows verification of the execution plans and the performance
    But take a look at the problems you may run into if you are        metrics of these queries before installing the change in pro-
about to start using SPM:                                              duction. The method also estimates the completeness of the
    http://intermediatesql.com/oracle/oracle-11g-sql-plan-             testing and helps to identify the functional areas that are not
management-the-dark-side-of-spm-part-4/.                               tested. It also gives an opportunity to involve the DBAs in the
                                                                       testing process—preparing them for the upcoming changes in
The Problem                                                            the production.
   Testing the application is a complicated process, and some-
times even a tested change causes performance issues after it’s        The Solution
implemented in production. There are a lot of reasons why                 The information captured in SQL Management Base can be
that may happen; for example, the test environment does not            used to assess the quality of testing and the impact of the ap-
accurately represent the production—the configuration, the             plication changes by comparing the snapshots of DBA_SQL_
data, or the optimizer statistics are different. Because of that,      PLAN_BASELINES taken before and after the testing to

The NoCOUG Journal
                                                                                                                                        19
identify the differences.                                                       For simplicity of this demonstration I’m setting the
    The DBA_SQL_PLAN_BASELINES columns CREATED,                                OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES to TRUE.
 LAST_EXECUTED, ACCEPTED, and REPRODUCED allow
                                                                                alter system set optimizer_capture_sql_plan_baselines=true;
 tracking the usage and changes of SQL Plan Baselines. Note
 that the REPRODUCED column was introduced in 11gR2,                              The application activity in PROD is simulated using the
 and as it is needed to assess the impact of the tested changes,               Hammerora default driver script for TPC-C workload and one
 the method fully works from 11gR2 onward.                                     virtual user.
    The full process contains the following steps:                                 DBA_SQL_PLAN_BASELINES can be queried to confirm
                                                                               that the SQL Plan Baselines have been captured for the appli-
      ➤   Capture SQL Baselines in production.
                                                                               cation schema.
      ➤   Import SQL Baselines from production into the test                    select count(*) from DBA_SQL_PLAN_BASELINES
          database.                                                             where PARSING_SCHEMA_NAME='TPCC';
      ➤   Implement the changes in the test environment.
                                                                               Importing SQL Plan Baselines into the Test Database
      ➤   Execute performance tests.                                              Transferring the baselines to another database is a four-step
      ➤   C o m p a re t h e s n a p s h o t s o f D B A _ S Q L _ P L A N _   process: create the staging table in the source database, pack
          BASELINES.                                                           SQL baselines into the staging table, copy the staging table to
                                                                               the target database, and unpack baselines from the staging
      ➤   Assess the results.                                                  table into the SQL Management Base. However, as the LAST_
    The following sections of the article will provide details and             EXECUTED value in DBMS_SQL_PLAN_MANAGEMENT is
 examples of how to execute each step of the plan.                             updated only once every 6.5 days (it is the intended function-
    I have set up two 11.2.0.3 databases named PROD and                        ality due to the performance overhead updating it on each
 TEST to demonstrate the process. Hammerora version 2.11                       execution would cause, as explained in Bug 8864741), an ad-
 (http://hammerora.sourceforge.net/) will be used to generate                  ditional step of resetting the values of LAST_EXECUTED has
 some workload and to simulate the testing of the application.                 to be done in the staging table before unpacking the baselines;
 The demo schema in the PROD database was built using the                      otherwise the LAST_EXECUTED values of the plan baselines
 following TPC-C Build Options: User=TPCC, Default                             may not be updated and the tracking of executed SQL state-
 Tablespace=USERS, Number of Warehouses=1, Virtual Users                       ments will not be accurate. It is also required to purge all
 to Build Schema=1.                                                            baselines in the test database to have a clean look at what is
    The new TPCC schema was imported in TEST using data                        happening during the testing.
 pump over database link, this way simulating the creation of                     The following statements are used to extract the SQL Plan
 the test database.                                                            Baselines from the production database:

     create database link PROD                                                  exec DBMS_SPM.CREATE_STGTAB_BASELINE('STAGE_
     connect to admin identified by password                                    SPM');
     using 'PROD';                                                              exec :n:=DBMS_SPM.PACK_STGTAB_BASELINE('STAGE_
                                                                                SPM');
     impdp SCHEMAS=TPCC NETWORK_LINK=PROD
                                                                                  The next set of commands prepares the SQL Management
 Capturing SQL Plan Baselines                                                  Base for testing in the test environment:
    The easiest way to capture SQL Plan Baselines is to set                     create table STAGE_SPM as
 the initialization parameter OPTIMIZER_CAPTURE_SQL_                            select * from STAGE_SPM@PROD;
 PLAN_BASELINES to TRUE at system level, but be aware of a
 few potential issues before you do that.                                       update STAGE_SPM
                                                                                set LAST_EXECUTED=NULL;
    Bug 9910484 causes high performance overhead, excessive                     commit;
 redo generation, and unexpected growth of SYSAUX tablespace.
 The bug is fixed in 11.2.0.3 for all platforms or where the patch              var n number
 9910484 is available for versions 11.1.0.7 and higher.                         begin
    If the application does not use bind variables SPM will not                   for c in (select distinct sql_handle from dba_sql_plan_baselines)
                                                                                  loop
 work, as it uses the signature of SQL (hash value calculated on                    :n:=DBMS_SPM.DROP_SQL_PLAN_BASELINE(c.sql_
 normalized SQL text) to look up the present baselines. If bind                 handle);
 variables are not used the signatures for the SQL statements                     end loop;
 will be different. If the SQL Plan Baseline capture is enabled it              end;
                                                                                /
 may also cause excessive growth of SYSAUX tablespace as large
 amount of statements are added to the SQL Management Base.                     exec :n:=DBMS_SPM.UNPACK_STGTAB_BASELINE('STAGE_
                                                                                SPM');
    There are also other options to create the SQL Baselines,
 such as loading them from SQL Tuning Sets or collecting them                     I’m creating the first snapshot from the DBA_SQL_PLAN_
 from the Shared SQL Area using procedures in DBMS_SPM                         BASELINES after the baselines are unpacked; it will represent
 package.                                                                      the production state of the SPM. Additional filters can be


20                                                                                                                                  August 2012
SQL CORNER
added to limit the scope of the analysis using this method; in       Assessing the Results
this case only enabled baselines for the application schema             Based on the snapshots representing two states of SPM—the
TPCC are collected.                                                  production state and the testing state—comparison can be done
 create table SPM_SNAP_P as                                          to observe the differences and address following questions:
 select * from DBA_SQL_PLAN_BASELINES
 where enabled='YES'                                                   ➤   Completeness of the testing: The expectation is to test
 and parsing_schema_name = 'TPCC';                                         full functionality of the application to make sure that
                                                                           none of the possible changes are missed during testing.
Implementing the Changes                                                   This query provides a list of statements that were not
   This is the time to implement all the planned changes in the            executed during testing. It should be reviewed to find
test system. For the purpose of the demonstration, the follow-             out if the queries were removed from the application
ing changes will be done in TEST:                                          because of the implemented changes or if they were
                                                                           listed because the testing had missed some part of the
 DROP INDEX tpcc.orders_i1;
                                                                           functionality. The number of queries returned can be
 CREATE INDEX tpcc.c_fn ON tpcc.customer(Upper(c_first));                  compared to the total number of statements in the SQL
                                                                           Management Base to assess the quality of testing and
 ALTER TABLE tpcc.orders ADD (ocn NUMBER);                                 the level of risk; e.g., if the total is 1000 statements and
 CREATE OR REPLACE TRIGGER tpcc.cust_ord_counter                           400 of them were not tested, it might be worth checking
 BEFORE INSERT ON tpcc.orders FOR EACH ROW                                 to determine why the difference is so big.
 BEGIN
   SELECT Count(*) INTO :new.ocn                                      SELECT sql_handle, sql_text
   FROM tpcc.orders WHERE o_c_id = :new.o_c_id;                       FROM SPM_SNAP_P P
 END;                                                                 WHERE NOT EXISTS (
 /                                                                       SELECT 1 FROM SPM_SNAP_T T
                                                                         WHERE p.signature = t.signature
    One of the Hammerora files was also modified to simulate            AND (t.last_executed IS NOT NULL or t.created > p.created)
a situation in which a query is not executed during the test-         );
ing.
                                                                       ➤   SQL statements introduced by the implemented change:
 sed -r -i 's,max(w_id),max(w_id) mwhid,g'                            Having a list of queries that were executed during the
  ./hora-components/hora_tpcc.tcl                                          testing but were not executed in production provides the
                                                                           opportunity to check the performance of the new SQL
Running the Performance Tests                                              statements using the information available in AWR by
   Any method of testing can be used. The tests can be per-                looking at the statements that are related to the applica-
formed manually or automated tools executing predefined                    tion change. For the best results the new baselines created
unit tests or load testing tools can be used, or it can be a mix           for these queries should be imported into production
of test methods. It is important to remember that the purpose              together with the application change to ensure that the
of the method is to assess the quality of the testing and the              exact same tested execution plans are used.
overall impact of the introduced changes, so the target should
be testing the full functionality of the application. For the best    SELECT sql_handle ,sql_text
                                                                      FROM SPM_SNAP_T T
results, each of the test cases should be executed twice—other-       WHERE NOT EXISTS (
wise the SQL Plan Baselines are not captured for the new state-          SELECT 1 FROM SPM_SNAP_P P
ments. Keeping this in mind, the method may seem more                    WHERE T.signature = P.signature
appealing in cases where automated testing tools are available        );
or the application is undergoing a bigger change, which would
require complete functionality testing.                                ➤   Non-reproducible execution plans: This is the list of
   Parameters OPTIMIZER_CAPTURE_SQL_PLAN_                                  statements in which the performance is definitely affected
BASELINES and OPTIMIZER_USE_SQL_PLAN_BASELINES                             by the implemented change, as the previous execution
have to be set to true before the testing is started.                      plan is no longer reproducible. The execution plan used
                                                                           is different even if SPM is used, so make sure the perfor-
 alter system set optimizer_capture_sql_plan_baselines=true;               mance of the new execution plan is acceptable.
 alter system set optimizer_use_sql_plan_baselines=true;
                                                                      SELECT sql_handle, plan_name, sql_text
   For the simulation of testing I’m running Hammerora on
                                                                      FROM SPM_SNAP_T T
the TEST database the same way it was done in PROD. When              WHERE T.reproduced = 'NO'
the testing completes, another snapshot from DBA_SQL_                 AND T.accepted='YES'
PLAN_BASELINES needs to be created.                                   AND EXISTS (
                                                                         SELECT 1 FROM SPM_SNAP_P P
 create table SPM_SNAP_T as                                              WHERE T.signature = P.signature
 select * from DBA_SQL_PLAN_BASELINES                                   AND P.plan_name=T.plan_name
 where enabled='YES'                                                     and P.reproduced = 'YES'
 and parsing_schema_name = 'TPCC';                                    );


The NoCOUG Journal
                                                                                                                                      21
➤   New execution plans: This is the list of all queries for           The use of this method allows active involvement of DBAs
          which a new execution plan was found and added to the          in the application testing, as a detailed view of the impact that
          plan history during the testing. As the SPM is in use, the     the application change has at the database level becomes avail-
          new execution plan may not have been actually used             able. The tighter cooperation between DBAs, developers, and
          during the testing; therefore, the baselines for these que-    testers helps to move closer to the common goal of minimizing
          ries should be evolved, and if the new plans perform           the risk of negative performance impact after the installation
          better than the old ones, they should be imported into         of the change in production. ▲
          production together with the change to use the best
          possible execution plan immediately after the installa-        Maris Elsins is an Oracle Applications DBA currently working at
          tion of the change.                                            Pythian, the global industry leader in remote database adminis-
                                                                         tration services and consulting for Oracle, MySQL, and SQL
     SELECT sql_handle, plan_name, dbms_lob.Substr(sql_
     text,80,1)
                                                                         Server. Maris has extensive experience in troubleshooting and
     FROM spm_snap_t t                                                   performance tuning of Oracle databases and Oracle E-Business
     WHERE EXISTS (                                                      Suite systems; his certifications include: 10g DBA OCM, 9i/10g/
        SELECT 1 FROM spm_snap_p p                                       11g DBA OCP, and 11i E-Business Suite DBA OCP. He has led
        WHERE T.signature = P.signature
                                                                         or taken part in multiple Oracle Database and Oracle E-Business
     )
     AND NOT EXISTS (                                                    Suite 11i/R12 implementation, maintenance, migration, and
        SELECT 1 FROM spm_snap_p p                                       upgrade projects and has been a speaker at multiple UKOUG,
        WHERE P.signature = T.signature                                  LVOUG, and OUGF events.                     Copyright © 2012, Maris Elsins
       AND P.plan_name = T.plan_name
     );
                                                                         BOOK EXCERPT (continued from page 18)
 Calibrating the Test Database                                           prove performance, but they all rely on having a complete
    Configuration differences between production and test en-            picture of the data available. And this is where you hit a wall:
 vironments can severely affect the results of testing. The gen-         this approach simply doesn’t work when half the data is on
 eral recommendation is to have the test environment exactly             another server.
 the same as production: the same hardware, the same architec-               Of course, you might have a small database that simply gets
 ture, the same configuration parameters, the same version of            lots of requests, so you just need to share the workload. Un-
 software, the same optimizer statistics, etc. In reality this is        fortunately, here you hit another wall. You need to ensure that
 rarely possible, so how do we know if the results observed dur-         data written to the first server is available to the second server.
 ing the testing are introduced by the application change or by          And you face additional issues if updates are made on two
 the fact that the test environment differs from production?             separate masters simultaneously. For example, you need to
    The question can be answered by running an additional                determine which update is the correct one. Another problem
 round of “calibration” testing, done exactly the same way as            you can encounter: someone might query for information on
 described above but just before the planned changes are in-             the second server that has just been written to the first server,
 stalled in the test environment. In this case we would have             but that information hasn’t been updated yet on the second
 three snapshots of DBA_SQL_PLAN_BASELINES: the pro-                     server. When you consider all these issues, it becomes easy to
 duction snapshot, the calibration run snapshot, and the test            see why the Oracle solution is so expensive—these problems
 run snapshot. Comparing the production snapshot to the                  are extremely hard to address.
 calibration run snapshot reveals the impact of the differences              MongoDB solves this problem in a very clever way—it
 between the production and the test systems. Comparing the              avoids it completely. Recall that MongoDB stores data in BSON
 calibration run snapshot to the test run snapshot will show the         documents, so the data is self-contained. That is, although sim-
 impact of the implemented changes.                                      ilar documents are stored together, individual documents aren’t
                                                                         made up of relationships. This means everything you need is all
 Summary                                                                 in one place. Because queries in MongoDB look for specific
    SQL Plan Management is one of the greatest new perfor-               keys and values in a document, this information can be easily
 mance features introduced in 11g. It not only does what it has          spread across as many servers as you have available. Each server
 been built for, but it also offers a lot of versatility, allowing its   checks the content it has and returns the result. This effectively
 usage for nonstandard purposes.                                         allows almost linear scalability and performance. As an added
    In this article I have described a method for assessing the          bonus, it doesn’t even require that you take out a new mortgage
 results of testing the changes in the application, configuration,       to pay for this functionality.
 and database software using the information collected in the                Admittedly, MongoDB does not offer master/master replica-
 SQL Management Base. The main idea of this technique is to              tion, where two separate servers can both accept write requests.
 capture snapshots of DBA_SQL_PLAN_MANAGEMENT and                        However, it does have sharding, which allows data to split
 compare them to evaluate the quality of testing and to observe          across multiple machines, with each machine responsible for
 the impact that the installed changes have on the SQL state-            updating different parts of the dataset. The benefit of this de-
 ments and their execution plans. The information obtained is            sign is that, while some solutions allow two master databases,
 sufficient to be used for checking the execution plans and per-         MongoDB can potentially scale to hundreds of machines as
 formance of the affected statements in the test database.               easily as it can run on two. ▲


22                                                                                                                               August 2012

Más contenido relacionado

La actualidad más candente

Configuration of sap net weaver for oracle grid infrastructure 11.2 with orac...
Configuration of sap net weaver for oracle grid infrastructure 11.2 with orac...Configuration of sap net weaver for oracle grid infrastructure 11.2 with orac...
Configuration of sap net weaver for oracle grid infrastructure 11.2 with orac...
Graciete Martins
 

La actualidad más candente (8)

Configuration of sap net weaver for oracle grid infrastructure 11.2 with orac...
Configuration of sap net weaver for oracle grid infrastructure 11.2 with orac...Configuration of sap net weaver for oracle grid infrastructure 11.2 with orac...
Configuration of sap net weaver for oracle grid infrastructure 11.2 with orac...
 
R12 d49656 gc10-apps dba 11
R12 d49656 gc10-apps dba 11R12 d49656 gc10-apps dba 11
R12 d49656 gc10-apps dba 11
 
R12 d49656 gc10-apps dba 27
R12 d49656 gc10-apps dba 27R12 d49656 gc10-apps dba 27
R12 d49656 gc10-apps dba 27
 
Solman screens
Solman screensSolman screens
Solman screens
 
R12 d49656 gc10-apps dba 13
R12 d49656 gc10-apps dba 13R12 d49656 gc10-apps dba 13
R12 d49656 gc10-apps dba 13
 
Oracle core dba online training
Oracle core dba online trainingOracle core dba online training
Oracle core dba online training
 
Sap system copy procedure
Sap system copy procedureSap system copy procedure
Sap system copy procedure
 
Towards Holistic Continuous Software Performance Assessment
Towards Holistic Continuous Software Performance AssessmentTowards Holistic Continuous Software Performance Assessment
Towards Holistic Continuous Software Performance Assessment
 

Similar a Using SQL Plan Management for Performance Testing

Cooper Oracle 11g Overview
Cooper Oracle 11g OverviewCooper Oracle 11g Overview
Cooper Oracle 11g Overview
moin_azeem
 
Reducing Risk When Upgrading MySQL
Reducing Risk When Upgrading MySQLReducing Risk When Upgrading MySQL
Reducing Risk When Upgrading MySQL
Kenny Gryp
 
Twp Upgrading 10g To 11g What To Expect From Optimizer
Twp Upgrading 10g To 11g What To Expect From OptimizerTwp Upgrading 10g To 11g What To Expect From Optimizer
Twp Upgrading 10g To 11g What To Expect From Optimizer
qiw
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
InSync Conference
 

Similar a Using SQL Plan Management for Performance Testing (20)

Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 
Performance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and UnderscoresPerformance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and Underscores
 
Modeling and Testing Dovetail in MagicDraw
Modeling and Testing Dovetail in MagicDrawModeling and Testing Dovetail in MagicDraw
Modeling and Testing Dovetail in MagicDraw
 
Crack the complexity of oracle applications r12 workload v2
Crack the complexity of oracle applications r12 workload v2Crack the complexity of oracle applications r12 workload v2
Crack the complexity of oracle applications r12 workload v2
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cPresentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12c
 
Cooper Oracle 11g Overview
Cooper Oracle 11g OverviewCooper Oracle 11g Overview
Cooper Oracle 11g Overview
 
Reducing Risk When Upgrading MySQL
Reducing Risk When Upgrading MySQLReducing Risk When Upgrading MySQL
Reducing Risk When Upgrading MySQL
 
Twp Upgrading 10g To 11g What To Expect From Optimizer
Twp Upgrading 10g To 11g What To Expect From OptimizerTwp Upgrading 10g To 11g What To Expect From Optimizer
Twp Upgrading 10g To 11g What To Expect From Optimizer
 
Hovitaga OpenSQL Editor - Overview
Hovitaga OpenSQL Editor - OverviewHovitaga OpenSQL Editor - Overview
Hovitaga OpenSQL Editor - Overview
 
Store procedures
Store proceduresStore procedures
Store procedures
 
Maestro_Abstract
Maestro_AbstractMaestro_Abstract
Maestro_Abstract
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
 
Oracle Sql Tuning
Oracle Sql TuningOracle Sql Tuning
Oracle Sql Tuning
 
Modernizing SQL Server the Right Way
Modernizing SQL Server the Right WayModernizing SQL Server the Right Way
Modernizing SQL Server the Right Way
 
Patrick_Rebrook_Resume
Patrick_Rebrook_ResumePatrick_Rebrook_Resume
Patrick_Rebrook_Resume
 
Oracle audit and reporting in one hour or less
Oracle audit and reporting in one hour or lessOracle audit and reporting in one hour or less
Oracle audit and reporting in one hour or less
 
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
 
11g nf sql_anlz
11g nf sql_anlz11g nf sql_anlz
11g nf sql_anlz
 
My Experience Using Oracle SQL Plan Baselines 11g/12c
My Experience Using Oracle SQL Plan Baselines 11g/12cMy Experience Using Oracle SQL Plan Baselines 11g/12c
My Experience Using Oracle SQL Plan Baselines 11g/12c
 

Más de Maris Elsins

Más de Maris Elsins (19)

An AWS DMS Replication Journey from Oracle to Aurora MySQL
An AWS DMS Replication Journey from Oracle to Aurora MySQLAn AWS DMS Replication Journey from Oracle to Aurora MySQL
An AWS DMS Replication Journey from Oracle to Aurora MySQL
 
Oracle Databases on AWS - Getting the Best Out of RDS and EC2
Oracle Databases on AWS - Getting the Best Out of RDS and EC2Oracle Databases on AWS - Getting the Best Out of RDS and EC2
Oracle Databases on AWS - Getting the Best Out of RDS and EC2
 
Migrating and Running DBs on Amazon RDS for Oracle
Migrating and Running DBs on Amazon RDS for OracleMigrating and Running DBs on Amazon RDS for Oracle
Migrating and Running DBs on Amazon RDS for Oracle
 
Mining AWR V2 - Trend Analysis
Mining AWR V2 - Trend AnalysisMining AWR V2 - Trend Analysis
Mining AWR V2 - Trend Analysis
 
DB12c: All You Need to Know About the Resource Manager
DB12c: All You Need to Know About the Resource ManagerDB12c: All You Need to Know About the Resource Manager
DB12c: All You Need to Know About the Resource Manager
 
C15LV: Ins and Outs of Concurrent Processing Configuration in Oracle e-Busine...
C15LV: Ins and Outs of Concurrent Processing Configuration in Oracle e-Busine...C15LV: Ins and Outs of Concurrent Processing Configuration in Oracle e-Busine...
C15LV: Ins and Outs of Concurrent Processing Configuration in Oracle e-Busine...
 
Mining the AWR: Alternative Methods for Identification of the Top SQLs (inclu...
Mining the AWR: Alternative Methods for Identification of the Top SQLs (inclu...Mining the AWR: Alternative Methods for Identification of the Top SQLs (inclu...
Mining the AWR: Alternative Methods for Identification of the Top SQLs (inclu...
 
Database as a Service on the Oracle Database Appliance Platform
Database as a Service on the Oracle Database Appliance PlatformDatabase as a Service on the Oracle Database Appliance Platform
Database as a Service on the Oracle Database Appliance Platform
 
LVOUG meetup #2 - Forcing SQL Execution Plan Instability
LVOUG meetup #2 - Forcing SQL Execution Plan InstabilityLVOUG meetup #2 - Forcing SQL Execution Plan Instability
LVOUG meetup #2 - Forcing SQL Execution Plan Instability
 
OUG Harmony 2012 - Using SQL Plan Baselines for Performance Testing
OUG Harmony 2012 -  Using SQL Plan Baselines for Performance TestingOUG Harmony 2012 -  Using SQL Plan Baselines for Performance Testing
OUG Harmony 2012 - Using SQL Plan Baselines for Performance Testing
 
LVOUG meetup #4 - Case Study 10g to 11g
LVOUG meetup #4 - Case Study 10g to 11gLVOUG meetup #4 - Case Study 10g to 11g
LVOUG meetup #4 - Case Study 10g to 11g
 
Surviving the Crisis With the Help of Oracle Database Resource Manager
Surviving the Crisis With the Help of Oracle Database Resource ManagerSurviving the Crisis With the Help of Oracle Database Resource Manager
Surviving the Crisis With the Help of Oracle Database Resource Manager
 
Concurrent Processing Performance Analysis for Apps DBAs
Concurrent Processing Performance Analysis for Apps DBAsConcurrent Processing Performance Analysis for Apps DBAs
Concurrent Processing Performance Analysis for Apps DBAs
 
Simplify Consolidation with Oracle Database 12c
Simplify Consolidation with Oracle Database 12cSimplify Consolidation with Oracle Database 12c
Simplify Consolidation with Oracle Database 12c
 
10 ways to improve your rman script
10 ways to improve your rman script10 ways to improve your rman script
10 ways to improve your rman script
 
Wildcard13 - warmup slides for the "Roundtable discussion with Oracle Profess...
Wildcard13 - warmup slides for the "Roundtable discussion with Oracle Profess...Wildcard13 - warmup slides for the "Roundtable discussion with Oracle Profess...
Wildcard13 - warmup slides for the "Roundtable discussion with Oracle Profess...
 
Whitepaper: Running Oracle e-Business Suite Database on Oracle Database Appli...
Whitepaper: Running Oracle e-Business Suite Database on Oracle Database Appli...Whitepaper: Running Oracle e-Business Suite Database on Oracle Database Appli...
Whitepaper: Running Oracle e-Business Suite Database on Oracle Database Appli...
 
Running E-Business Suite Database on Oracle Database Appliance
Running E-Business Suite Database on Oracle Database ApplianceRunning E-Business Suite Database on Oracle Database Appliance
Running E-Business Suite Database on Oracle Database Appliance
 
Internals of concurent managers
Internals of concurent managersInternals of concurent managers
Internals of concurent managers
 

Using SQL Plan Management for Performance Testing

  • 1. PE RFORMANC E CORN E R SQL Plan Management for Performance Testing by Maris Elsins Maris Elsins This article is the third in a series by Pythian experts that is the optimizer produces different execution plans in each data- being published as the “Performance Corner” column in the base. This problem is partially addressed by SPM, but the risk NoCOUG Journal. still remains if the implemented change does not allow repro- S QL Plan Management (SPM) is a feature introduced ducing any of the SQL plans in the baseline (e.g., an index was with Oracle Database 11gR1 that effectively addresses dropped) or the change introduces new SQL statements that the long-standing problem of unpredictable effects don’t have any baselines yet. caused on the performance of SQL statements by the Another possible reason for unexpected behavior after im- changes of optimizer statistics, system configuration, and plementation of the change is incomplete testing— only the schema objects, or the patching and upgrading of the database changed functionality was tested or testing was done using software. In many cases the cause of the degraded performance automated testing tools that don’t have the test cases for testing is the different execution plan produced by the optimizer for the full scope of functionality. In such cases, the testing can the same statement, compared to the one used before. miss problems introduced in the parts of the application that SPM solves the problem by capturing the execution plan were not tested. It is also possible the testing did not miss the history and creating plan baselines that contain all accepted change, but it was not noticed because the overall workload in execution plans for the statement; the optimizer is aware of the test system was lower and the issue did not reveal itself. SPM and allows only accepted plans in the baseline to be used It is often hard to assess the quality of testing and, as the for execution of the SQL statement. This mechanism prevents DBAs are unaware of all of the changes being implemented, it’s the use of any unverified execution plan, thereby eliminating nearly impossible to estimate the performance impact of the the unpredictable degradation of the performance. Maria change using tools like AWR or ASH. It is hard to involve DBAs Colgan has explained how SPM works in a series of articles in the active testing process, and usually the involvement stops starting here: with troubleshooting of the discovered performance issues. https://blogs.oracle.com/optimizer/entry/sql_plan_ On the other hand, DBAs are usually the first to be called for management_part_1_of_4_creating_sql_plan_baselines. help when the change causes a severe performance issue in In addition to the main purpose of SPM, it also can be used production, but by then it’s too late—the business is already in different scenarios, such as preserving the execution plans suffering. of SQL statements while upgrading the database (http://docs. In this article, I will describe a method for tracking down oracle.com/cd/E11882_01/server.112/e23633/preup. SQL statements introduced with the application change and htm#UPGRD00232) or forcing the use of a different execu- SQL statements that have different execution plans compared tion plan by creating a “fake baseline” (http://jonathanlewis. to ones used in production before the change. This method wordpress.com/2011/01/12/fake-baselines/). allows verification of the execution plans and the performance But take a look at the problems you may run into if you are metrics of these queries before installing the change in pro- about to start using SPM: duction. The method also estimates the completeness of the http://intermediatesql.com/oracle/oracle-11g-sql-plan- testing and helps to identify the functional areas that are not management-the-dark-side-of-spm-part-4/. tested. It also gives an opportunity to involve the DBAs in the testing process—preparing them for the upcoming changes in The Problem the production. Testing the application is a complicated process, and some- times even a tested change causes performance issues after it’s The Solution implemented in production. There are a lot of reasons why The information captured in SQL Management Base can be that may happen; for example, the test environment does not used to assess the quality of testing and the impact of the ap- accurately represent the production—the configuration, the plication changes by comparing the snapshots of DBA_SQL_ data, or the optimizer statistics are different. Because of that, PLAN_BASELINES taken before and after the testing to The NoCOUG Journal 19
  • 2. identify the differences. For simplicity of this demonstration I’m setting the The DBA_SQL_PLAN_BASELINES columns CREATED, OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES to TRUE. LAST_EXECUTED, ACCEPTED, and REPRODUCED allow alter system set optimizer_capture_sql_plan_baselines=true; tracking the usage and changes of SQL Plan Baselines. Note that the REPRODUCED column was introduced in 11gR2, The application activity in PROD is simulated using the and as it is needed to assess the impact of the tested changes, Hammerora default driver script for TPC-C workload and one the method fully works from 11gR2 onward. virtual user. The full process contains the following steps: DBA_SQL_PLAN_BASELINES can be queried to confirm that the SQL Plan Baselines have been captured for the appli- ➤ Capture SQL Baselines in production. cation schema. ➤ Import SQL Baselines from production into the test select count(*) from DBA_SQL_PLAN_BASELINES database. where PARSING_SCHEMA_NAME='TPCC'; ➤ Implement the changes in the test environment. Importing SQL Plan Baselines into the Test Database ➤ Execute performance tests. Transferring the baselines to another database is a four-step ➤ C o m p a re t h e s n a p s h o t s o f D B A _ S Q L _ P L A N _ process: create the staging table in the source database, pack BASELINES. SQL baselines into the staging table, copy the staging table to the target database, and unpack baselines from the staging ➤ Assess the results. table into the SQL Management Base. However, as the LAST_ The following sections of the article will provide details and EXECUTED value in DBMS_SQL_PLAN_MANAGEMENT is examples of how to execute each step of the plan. updated only once every 6.5 days (it is the intended function- I have set up two 11.2.0.3 databases named PROD and ality due to the performance overhead updating it on each TEST to demonstrate the process. Hammerora version 2.11 execution would cause, as explained in Bug 8864741), an ad- (http://hammerora.sourceforge.net/) will be used to generate ditional step of resetting the values of LAST_EXECUTED has some workload and to simulate the testing of the application. to be done in the staging table before unpacking the baselines; The demo schema in the PROD database was built using the otherwise the LAST_EXECUTED values of the plan baselines following TPC-C Build Options: User=TPCC, Default may not be updated and the tracking of executed SQL state- Tablespace=USERS, Number of Warehouses=1, Virtual Users ments will not be accurate. It is also required to purge all to Build Schema=1. baselines in the test database to have a clean look at what is The new TPCC schema was imported in TEST using data happening during the testing. pump over database link, this way simulating the creation of The following statements are used to extract the SQL Plan the test database. Baselines from the production database: create database link PROD exec DBMS_SPM.CREATE_STGTAB_BASELINE('STAGE_ connect to admin identified by password SPM'); using 'PROD'; exec :n:=DBMS_SPM.PACK_STGTAB_BASELINE('STAGE_ SPM'); impdp SCHEMAS=TPCC NETWORK_LINK=PROD The next set of commands prepares the SQL Management Capturing SQL Plan Baselines Base for testing in the test environment: The easiest way to capture SQL Plan Baselines is to set create table STAGE_SPM as the initialization parameter OPTIMIZER_CAPTURE_SQL_ select * from STAGE_SPM@PROD; PLAN_BASELINES to TRUE at system level, but be aware of a few potential issues before you do that. update STAGE_SPM set LAST_EXECUTED=NULL; Bug 9910484 causes high performance overhead, excessive commit; redo generation, and unexpected growth of SYSAUX tablespace. The bug is fixed in 11.2.0.3 for all platforms or where the patch var n number 9910484 is available for versions 11.1.0.7 and higher. begin If the application does not use bind variables SPM will not for c in (select distinct sql_handle from dba_sql_plan_baselines) loop work, as it uses the signature of SQL (hash value calculated on :n:=DBMS_SPM.DROP_SQL_PLAN_BASELINE(c.sql_ normalized SQL text) to look up the present baselines. If bind handle); variables are not used the signatures for the SQL statements end loop; will be different. If the SQL Plan Baseline capture is enabled it end; / may also cause excessive growth of SYSAUX tablespace as large amount of statements are added to the SQL Management Base. exec :n:=DBMS_SPM.UNPACK_STGTAB_BASELINE('STAGE_ SPM'); There are also other options to create the SQL Baselines, such as loading them from SQL Tuning Sets or collecting them I’m creating the first snapshot from the DBA_SQL_PLAN_ from the Shared SQL Area using procedures in DBMS_SPM BASELINES after the baselines are unpacked; it will represent package. the production state of the SPM. Additional filters can be 20 August 2012
  • 3. SQL CORNER added to limit the scope of the analysis using this method; in Assessing the Results this case only enabled baselines for the application schema Based on the snapshots representing two states of SPM—the TPCC are collected. production state and the testing state—comparison can be done create table SPM_SNAP_P as to observe the differences and address following questions: select * from DBA_SQL_PLAN_BASELINES where enabled='YES' ➤ Completeness of the testing: The expectation is to test and parsing_schema_name = 'TPCC'; full functionality of the application to make sure that none of the possible changes are missed during testing. Implementing the Changes This query provides a list of statements that were not This is the time to implement all the planned changes in the executed during testing. It should be reviewed to find test system. For the purpose of the demonstration, the follow- out if the queries were removed from the application ing changes will be done in TEST: because of the implemented changes or if they were listed because the testing had missed some part of the DROP INDEX tpcc.orders_i1; functionality. The number of queries returned can be CREATE INDEX tpcc.c_fn ON tpcc.customer(Upper(c_first)); compared to the total number of statements in the SQL Management Base to assess the quality of testing and ALTER TABLE tpcc.orders ADD (ocn NUMBER); the level of risk; e.g., if the total is 1000 statements and CREATE OR REPLACE TRIGGER tpcc.cust_ord_counter 400 of them were not tested, it might be worth checking BEFORE INSERT ON tpcc.orders FOR EACH ROW to determine why the difference is so big. BEGIN SELECT Count(*) INTO :new.ocn SELECT sql_handle, sql_text FROM tpcc.orders WHERE o_c_id = :new.o_c_id; FROM SPM_SNAP_P P END; WHERE NOT EXISTS ( / SELECT 1 FROM SPM_SNAP_T T WHERE p.signature = t.signature One of the Hammerora files was also modified to simulate AND (t.last_executed IS NOT NULL or t.created > p.created) a situation in which a query is not executed during the test- ); ing. ➤ SQL statements introduced by the implemented change: sed -r -i 's,max(w_id),max(w_id) mwhid,g' Having a list of queries that were executed during the ./hora-components/hora_tpcc.tcl testing but were not executed in production provides the opportunity to check the performance of the new SQL Running the Performance Tests statements using the information available in AWR by Any method of testing can be used. The tests can be per- looking at the statements that are related to the applica- formed manually or automated tools executing predefined tion change. For the best results the new baselines created unit tests or load testing tools can be used, or it can be a mix for these queries should be imported into production of test methods. It is important to remember that the purpose together with the application change to ensure that the of the method is to assess the quality of the testing and the exact same tested execution plans are used. overall impact of the introduced changes, so the target should be testing the full functionality of the application. For the best SELECT sql_handle ,sql_text FROM SPM_SNAP_T T results, each of the test cases should be executed twice—other- WHERE NOT EXISTS ( wise the SQL Plan Baselines are not captured for the new state- SELECT 1 FROM SPM_SNAP_P P ments. Keeping this in mind, the method may seem more WHERE T.signature = P.signature appealing in cases where automated testing tools are available ); or the application is undergoing a bigger change, which would require complete functionality testing. ➤ Non-reproducible execution plans: This is the list of Parameters OPTIMIZER_CAPTURE_SQL_PLAN_ statements in which the performance is definitely affected BASELINES and OPTIMIZER_USE_SQL_PLAN_BASELINES by the implemented change, as the previous execution have to be set to true before the testing is started. plan is no longer reproducible. The execution plan used is different even if SPM is used, so make sure the perfor- alter system set optimizer_capture_sql_plan_baselines=true; mance of the new execution plan is acceptable. alter system set optimizer_use_sql_plan_baselines=true; SELECT sql_handle, plan_name, sql_text For the simulation of testing I’m running Hammerora on FROM SPM_SNAP_T T the TEST database the same way it was done in PROD. When WHERE T.reproduced = 'NO' the testing completes, another snapshot from DBA_SQL_ AND T.accepted='YES' PLAN_BASELINES needs to be created. AND EXISTS ( SELECT 1 FROM SPM_SNAP_P P create table SPM_SNAP_T as WHERE T.signature = P.signature select * from DBA_SQL_PLAN_BASELINES AND P.plan_name=T.plan_name where enabled='YES' and P.reproduced = 'YES' and parsing_schema_name = 'TPCC'; ); The NoCOUG Journal 21
  • 4. New execution plans: This is the list of all queries for The use of this method allows active involvement of DBAs which a new execution plan was found and added to the in the application testing, as a detailed view of the impact that plan history during the testing. As the SPM is in use, the the application change has at the database level becomes avail- new execution plan may not have been actually used able. The tighter cooperation between DBAs, developers, and during the testing; therefore, the baselines for these que- testers helps to move closer to the common goal of minimizing ries should be evolved, and if the new plans perform the risk of negative performance impact after the installation better than the old ones, they should be imported into of the change in production. ▲ production together with the change to use the best possible execution plan immediately after the installa- Maris Elsins is an Oracle Applications DBA currently working at tion of the change. Pythian, the global industry leader in remote database adminis- tration services and consulting for Oracle, MySQL, and SQL SELECT sql_handle, plan_name, dbms_lob.Substr(sql_ text,80,1) Server. Maris has extensive experience in troubleshooting and FROM spm_snap_t t performance tuning of Oracle databases and Oracle E-Business WHERE EXISTS ( Suite systems; his certifications include: 10g DBA OCM, 9i/10g/ SELECT 1 FROM spm_snap_p p 11g DBA OCP, and 11i E-Business Suite DBA OCP. He has led WHERE T.signature = P.signature or taken part in multiple Oracle Database and Oracle E-Business ) AND NOT EXISTS ( Suite 11i/R12 implementation, maintenance, migration, and SELECT 1 FROM spm_snap_p p upgrade projects and has been a speaker at multiple UKOUG, WHERE P.signature = T.signature LVOUG, and OUGF events. Copyright © 2012, Maris Elsins AND P.plan_name = T.plan_name ); BOOK EXCERPT (continued from page 18) Calibrating the Test Database prove performance, but they all rely on having a complete Configuration differences between production and test en- picture of the data available. And this is where you hit a wall: vironments can severely affect the results of testing. The gen- this approach simply doesn’t work when half the data is on eral recommendation is to have the test environment exactly another server. the same as production: the same hardware, the same architec- Of course, you might have a small database that simply gets ture, the same configuration parameters, the same version of lots of requests, so you just need to share the workload. Un- software, the same optimizer statistics, etc. In reality this is fortunately, here you hit another wall. You need to ensure that rarely possible, so how do we know if the results observed dur- data written to the first server is available to the second server. ing the testing are introduced by the application change or by And you face additional issues if updates are made on two the fact that the test environment differs from production? separate masters simultaneously. For example, you need to The question can be answered by running an additional determine which update is the correct one. Another problem round of “calibration” testing, done exactly the same way as you can encounter: someone might query for information on described above but just before the planned changes are in- the second server that has just been written to the first server, stalled in the test environment. In this case we would have but that information hasn’t been updated yet on the second three snapshots of DBA_SQL_PLAN_BASELINES: the pro- server. When you consider all these issues, it becomes easy to duction snapshot, the calibration run snapshot, and the test see why the Oracle solution is so expensive—these problems run snapshot. Comparing the production snapshot to the are extremely hard to address. calibration run snapshot reveals the impact of the differences MongoDB solves this problem in a very clever way—it between the production and the test systems. Comparing the avoids it completely. Recall that MongoDB stores data in BSON calibration run snapshot to the test run snapshot will show the documents, so the data is self-contained. That is, although sim- impact of the implemented changes. ilar documents are stored together, individual documents aren’t made up of relationships. This means everything you need is all Summary in one place. Because queries in MongoDB look for specific SQL Plan Management is one of the greatest new perfor- keys and values in a document, this information can be easily mance features introduced in 11g. It not only does what it has spread across as many servers as you have available. Each server been built for, but it also offers a lot of versatility, allowing its checks the content it has and returns the result. This effectively usage for nonstandard purposes. allows almost linear scalability and performance. As an added In this article I have described a method for assessing the bonus, it doesn’t even require that you take out a new mortgage results of testing the changes in the application, configuration, to pay for this functionality. and database software using the information collected in the Admittedly, MongoDB does not offer master/master replica- SQL Management Base. The main idea of this technique is to tion, where two separate servers can both accept write requests. capture snapshots of DBA_SQL_PLAN_MANAGEMENT and However, it does have sharding, which allows data to split compare them to evaluate the quality of testing and to observe across multiple machines, with each machine responsible for the impact that the installed changes have on the SQL state- updating different parts of the dataset. The benefit of this de- ments and their execution plans. The information obtained is sign is that, while some solutions allow two master databases, sufficient to be used for checking the execution plans and per- MongoDB can potentially scale to hundreds of machines as formance of the affected statements in the test database. easily as it can run on two. ▲ 22 August 2012