Skip Headers
Oracle® Database Performance Tuning Guide
11g Release 2 (11.2)

Part Number E10821-03
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

15 Using SQL Plan Management

This chapter describes how to manage SQL execution plans using SQL plan management. SQL plan management prevents performance regressions resulting from sudden changes to the execution plan of a SQL statement by providing components for capturing, selecting, and evolving SQL plan information.

This chapter contains the following topics:

15.1 Overview of SQL Plan Baselines

SQL plan management is a preventative mechanism that records and evaluates the execution plans of SQL statements over time. This mechanism can build a SQL plan baseline, which is a set of accepted plans for a SQL statement. The accepted plans have been proven to perform well.

15.1.1 Purpose of SQL Plan Baselines

The goal of baselines is to preserve the performance of corresponding SQL statements, regardless of changes in the database. Examples of changes include:

  • New optimizer version

  • Changes to optimizer statistics and optimizer parameters

  • Changes to schema and metadata definitions

  • Changes to system settings

  • SQL profile creating

SQL plan baselines cannot help in cases where an event has caused irreversible execution plan changes, such as dropping an index.

The SQL tuning features of Oracle Database generate SQL profiles that help the optimizer to produce well-tuned plans, but this is a reactive mechanism and cannot guarantee stable performance when drastic database changes occur. SQL tuning can only resolve performance issues after they have occurred and are identified. For example, a SQL statement may become high-load because of a plan change, but SQL tuning cannot solve this problem until after the plan change occurs.

Common usage scenarios where SQL plan management can improve or preserve SQL performance include:

  • A database upgrade that installs a new optimizer version usually results in plan changes for a small percentage of SQL statements, with most of the plan changes resulting in either no performance change or improvement. However, certain plan changes may cause performance regressions. The use of SQL plan baselines significantly minimizes potential performance regressions resulting from a database upgrade.

  • Ongoing system and data changes can impact plans for some SQL statements, potentially causing performance regressions. The use of SQL plan baselines will help to minimize performance regressions and stabilize SQL performance.

  • Deployment of new application modules means introducing new SQL statements into the system. The application software may use appropriate SQL execution plans developed in a standard test configuration for the new statements. If your system configuration is significantly different from the test configuration, then the SQL plan baselines can be evolved over time to produce better performance.

15.1.2 Architecture of SQL Plan Baselines

A SQL plan baseline contains one or more accepted plans, each of which contains the following information:

  • Set of hints

  • Plan hash value

  • Plan-related information

The plan history is the set of plans, both accepted and not accepted, generated for a SQL statement over time. Because only accepted plans are included in the SQL plan baseline, the plans in the baseline form a susbset of the plan history. For example, after the optimizer generates the first acceptable plan for a SQL plan baseline, subsequent plans are part of the plan history but not part of the plan baseline.

The process of adding plans to a SQL plan baseline is known as plan evolution. To be eligible to be evolved, a plan must be enabled for use by the optimizer.

Figure 15-1 shows a single SELECT statement that has two accepted plans in its SQL plan baseline. The SQL plan history includes two other plans for the statement that have not been proven to perform well.

Figure 15-1 SQL Plan Baseline and SQL Plan History

Description of Figure 15-1 follows
Description of "Figure 15-1 SQL Plan Baseline and SQL Plan History"

SQL plan baselines and plan history are stored in the SQL management base (SMB), which also contains SQL profiles. The SMB is part of the data dictionary and is stored in the SYSAUX tablespace. The SMB uses automatic space management.

15.2 Managing SQL Plan Baselines

Managing SQL plan baselines involves three phases:

15.2.1 Capturing SQL Plan Baselines

During the SQL plan baseline capture phase, the database detects plan changes and records the new plan so that it can be evolved (verified) by the database administrator. To this end, the database maintains a plan history for individual SQL statements. Because ad hoc SQL statements do not repeat and thus do not suffer performance degradation, the database maintains plan history only for repeatable SQL statements.

To recognize repeatable SQL statements, a statement log is maintained that contains the SQL ID of various SQL statements that the optimizer has evaluated over time. A SQL statement is recognized as repeatable when it is parsed or executed again after it has been logged.

For each repeatable SQL statement, the system maintains a plan history that contains all plans generated by the optimizer. The set of all accepted plans in the plan history is the SQL plan baseline.

The SQL Plan Baseline Capture phase can be configured for automatic capture of plan history and SQL plan baselines for repeatable SQL statements, or a set of plans can be manually loaded as SQL plan baselines.

This section contains the following topics:

15.2.1.1 Capturing Plans Automatically

When automatic plan capture is enabled, the system automatically creates and maintains the plan history for SQL statements using information provided by the optimizer. The plan history will include relevant information used by the optimizer to reproduce an execution plan, such as the SQL text, outline, bind variables, and compilation environment.

The initial plan generated for a SQL statement is marked as accepted for use by the optimizer, and represents both the plan history and the SQL plan baseline. All subsequent plans will be included in the plan history, and those plans that are verified not to cause performance regressions will be added to the SQL plan baseline during the SQL plan baseline evolution phase.

To enable automatic plan capture, set the OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES initialization parameter to TRUE. By default, this parameter is set to FALSE.

15.2.1.2 Creating Baselines from Existing Plans

You can create SQL plan baselines by manually loading existing plans for a set of SQL statements as plan baselines. The manually loaded plans are not verified for performance, but are added as accepted plans to existing or new SQL plan baselines. You can use manual plan loading with or as an alternative to automatic plan capture.

You can perform manual plan loading by:

15.2.1.2.1 Loading Plans from SQL Tuning Sets and AWR Snapshots

To load plans from a SQL Tuning Set, use the LOAD_PLANS_FROM_SQLSET function of the DBMS_SPM package:

DECLARE
  my_plans PLS_INTEGER;
BEGIN
  my_plans := DBMS_SPM.LOAD_PLANS_FROM_SQLSET(
    sqlset_name => 'tset1');
END;
/

In this example, the database loads the plans stored in SQL Tuning Set named tset1. To learn about additional parameters used by the LOAD_PLANS_FROM_SQLSET function, see Oracle Database PL/SQL Packages and Types Reference.

To load plans from Automatic Workload Repository (AWR), load the plans stored in AWR snapshots into a SQL Tuning Set before using the LOAD_PLANS_FROM_SQLSET function as described in this section.

15.2.1.2.2 Loading Plans from the Cursor Cache

To load plans from the cursor cache, use the LOAD_PLANS_FROM_CURSOR_CACHE function of the DBMS_SPM package:

DECLARE
  my_plans PLS_INTEGER;
BEGIN
  my_plans := DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE(
    sql_id => '99twu5t2dn5xd');
END;
/

In this example, Oracle Database loads the plans located in the cursor cache for the SQL statement identified by its sql_id. Plans in the cursor cache can be identified by:

  • SQL identifier (SQL_ID)

  • SQL text (SQL_TEXT)

  • One of the following attributes:

    • PARSING_SCHEMA_NAME

    • MODULE

    • ACTION

See Also:

Oracle Database PL/SQL Packages and Types Reference to learn how to use the LOAD_PLANS_FROM_CURSOR_CACHE function

15.2.2 Selecting SQL Plan Baselines

During the SQL plan baseline selection phase, Oracle Database detects plan changes based on the stored plan history, and selects plans to avoid potential performance regressions for a set of SQL statements.

Each time a SQL statement is compiled, the optimizer first uses a cost-based search method to build a best-cost plan, then tries to find a matching plan in the SQL plan baseline. If a match is found, the optimizer will proceed using this plan. Otherwise, it evaluates the cost of each accepted plan in the SQL plan baseline and selects the plan with the lowest cost. The best-cost plan found by the optimizer that does not match any plans in the plan history for the SQL statement represents a new plan, and is added as a non-accepted plan to the plan history. The new plan is not used until it is verified to not cause a performance regression. However, if a change in the system (such as a dropped index) causes all accepted plans to become non-reproducible, the optimizer will select the best-cost plan. Thus, the presence of a SQL plan baseline causes the optimizer to use conservative plan selection strategy for the SQL statement.

To enable the use of SQL plan baselines, set the OPTIMIZER_USE_SQL_PLAN_BASELINES initialization parameter to TRUE (default).

15.2.3 Evolving SQL Plan Baselines

During the SQL plan baseline evolution phase, the database evaluates the performance of new plans and integrates plans with better performance into SQL plan baselines.

When the optimizer finds a new plan for a SQL statement, the plan is added to the plan history as a non-accepted plan. The plan can then be verified for performance relative to the SQL plan baseline performance. When a non-accepted plan is verified to not cause a performance regression, it is changed to an accepted plan and integrated into the SQL plan baseline. A successful verification of a non-accepted plan consists of comparing its performance to that of a plan selected from the SQL plan baseline and ensuring that it delivers better performance.

This section describes how to evolve SQL plan baselines and contains the following topics:

15.2.3.1 Evolving Plans With Manual Plan Loading

You can evolve an existing SQL plan baseline by manually loading plans either from the cursor cache or from a SQL tuning set. When you manually load plans into a SQL plan baseline, these loaded plans are added as accepted plans.

15.2.3.2 Evolving Plans With DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE

The PL/SQL function DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE tries to evolve new plans added by the optimizer to the plan history of existing plan baselines. If the function can verify that the new plan performs better than a plan chosen from the corresponding SQL plan baseline, the new plan is added as an accepted plan.

The following is an example of the DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE function:

SET SERVEROUTPUT ON
SET LONG 10000
DECLARE
    report clob;
BEGIN
    report := DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE(
                  sql_handle => 'SYS_SQL_593bc74fca8e6738');
    DBMS_OUTPUT.PUT_LINE(report);
END;
/

Output:

REPORT
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
                       Evolve SQL Plan Baseline Report
--------------------------------------------------------------------------------
 
Inputs:
-------
 SQL_HANDLE = SYS_SQL_593bc74fca8e6738
 PLAN_NAME  =
 TIME_LIMIT = DBMS_SPM.AUTO_LIMIT
 VERIFY     = YES
 COMMIT     = YES
 
Plan: SYS_SQL_PLAN_ca8e6738a57b5fc2
-----------------------------------
 Plan was verified: Time used .07 seconds.
 Passed performance criterion: Compound improvement ratio >= 7.32.
 Plan was changed to an accepted plan.
 
                     Baseline Plan      Test Plan     Improv. Ratio
                     -------------      ---------     -------------
 Execution Status:        COMPLETE       COMPLETE
 Rows Processed:                40             40
 Elapsed Time(ms):              23              8              2.88
 CPU Time(ms):                  23              8              2.88
 Buffer Gets:                  450             61              7.38
 Disk Reads:                     0              0
 Direct Writes:                  0              0
 Fetches:                        0              0
 Executions:                     1              1
 
-------------------------------------------------------------------------------
                                Report Summary
-------------------------------------------------------------------------------
Number of SQL plan baselines verified: 1.
Number of SQL plan baselines evolved: 1.

In this example, Oracle Database successfully evolved a plan for a SQL statement identified by its SQL handle. Alternatively, you can use the DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE function to specify:

  • The name of a particular plan that you want to evolve

  • A list of plans to evolve

  • No value

    This enables Oracle Database to evolve all non-accepted plans currently in the SQL management base.

See Also:

Oracle Database PL/SQL Packages and Types Reference for information about using the DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE function

15.3 Using SQL Plan Baselines with the SQL Tuning Advisor

When tuning SQL statements with the SQL Tuning Advisor, if the advisor finds a tuned plan and verifies its performance to be better than a plan chosen from the corresponding SQL plan baseline, it makes a recommendation to accept a SQL profile. When the SQL profile is accepted, the tuned plan is added to the corresponding SQL plan baseline. However, the SQL Tuning Advisor will not verify existing unaccepted plans in the plan history.

In Oracle Database 11g, an automatically configured task runs the SQL Tuning Advisor during a maintenance window. This automatic SQL tuning task targets high-load SQL statements as identified by the execution performance data collected in the Automatic Workload Repository (AWR) snapshots. The SQL profile recommendations made by the SQL tuning advisor are implemented by the automatic SQL tuning task. Tuned plans are thus automatically added to the SQL plan baselines of the identified high-load SQL statements.

15.4 Using Fixed SQL Plan Baselines

A SQL plan baseline is fixed if it contains at least one enabled plan whose FIXED attribute is set to YES. You can use fixed SQL plan baselines to fix the set of possible plans (usually one plan) for a SQL statement, or migrate an existing stored outline by loading the "outlined" plan as a fixed plan.

If a fixed SQL plan baseline also contains non-fixed plans, the optimizer will give preference to fixed plans over non-fixed ones. This means that the optimizer will pick the fixed plan with the least cost even though a non-fixed plan may have an even lower cost. If none of the fixed plans is reproducible, then the optimizer will pick the best non-fixed plan.

The optimizer will not add new plans to a fixed SQL plan baseline. Since new plans are not automatically added, a fixed SQL plan baseline is not evolved when DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE is executed. However, a fixed SQL plan baseline can be evolved by manually loading new plans into it from the cursor cache or a SQL tuning set.

When a SQL statement with a fixed SQL plan baseline is tuned using the SQL Tuning Advisor, a SQL profile recommendation has special meaning. When the SQL profile is accepted, the tuned plan is added to the fixed SQL plan baseline as a non-fixed plan. However, as described above, the optimizer will not use the tuned plan as long as a reproducible fixed plan is present. Therefore, the benefit of SQL tuning may not be realized. To enable the use of the tuned plan, manually alter the tuned plan to a fixed plan by setting its FIXED attribute to YES.

15.5 Displaying SQL Plan Baselines

To view the plans stored in the SQL plan baseline for a given statement, use the DISPLAY_SQL_PLAN_BASELINE function of the DBMS_XPLAN package:

select * from table( 
    dbms_xplan.display_sql_plan_baseline( 
        sql_handle=>'SYS_SQL_209d10fabbedc741', 
        format=>'basic'));

The DISPLAY_SQL_PLAN_BASELINE function displays one or more execution plans for the specified SQL statement, specified by the handle (sql_handle). Alternatively, a single plan can be displayed by supplying a plan name (plan_name). For information about additional parameters used by the DISPLAY_SQL_PLAN_BASELINE function, see Oracle Database PL/SQL Packages and Types Reference.

This function uses plan information stored in the SQL management base to explain and display the plans. In this example, the DISPLAY_SQL_PLAN_BASELINE function displays the execution plans for the SQL statement specified by the handle SYS_SQL_209d10fabbedc741:

SQL handle: SYS_SQL_209d10fabbedc741
SQL text: select cust_last_name, amount_sold from customers c,
          sales s where c.cust_id=s.cust_id and cust_year_of_birth=:yob
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
Plan name: SYS_SQL_PLAN_bbedc741a57b5fc2
Enabled: YES      Fixed: NO      Accepted: NO      Origin: AUTO-CAPTURE
----------------------------------------------------------------------------------
Plan hash value: 2776326082

----------------------------------------------------------------------------------
| Id  | Operation                                | Name                          |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                         |                               |
|   1 | HASH JOIN                                |                               |
|   2 |   TABLE ACCESS BY INDEX ROWID            | CUSTOMERS                     |
|   3 |     BITMAP CONVERSION TO ROWIDS          |                               |
|   4 |     BITMAP INDEX SINGLE VALUE            | CUSTOMERS_YOB_BIX             |
|   5 |    PARTITION RANGE ALL                   |                               |
|   6 |    TABLE ACCESS FULL                     | SALES                         |
----------------------------------------------------------------------------------

----------------------------------------------------------------------------------
Plan name: SYS_SQL_PLAN_bbedc741f554c408
Enabled: YES     Fixed: NO      Accepted: YES       Origin: MANUAL-LOAD
----------------------------------------------------------------------------------
Plan hash value: 4115973128

----------------------------------------------------------------------------------
| Id  | Operation                                | Name                          |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                         |                               |
|   1 |   NESTED LOOPS                           |                               |
|   2 |     NESTED LOOPS                         |                               |
|   3 |       TABLE ACCESS BY INDEX ROWID        | CUSTOMERS                     |
|   4 |         BITMAP CONVERSION TO ROWIDS      |                               |
|   5 |           BITMAP INDEX SINGLE VALUE      | CUSTOMERS_YOB_BIX             |
|   6 |       PARTITION RANGE                    |                               |
|   7 |        BITMAP CONVERSION TO ROWIDS       |                               |
|   8 |          BITMAP INDEX SINGLE VALUE       | SALES_CUST_BIX                |
|   9 |     TABLE ACCESS BY LOCAL INDEX ROWID    | SALES                         |
----------------------------------------------------------------------------------

You can also display SQL plan baseline information using a SELECT statement directly on the DBA_SQL_PLAN_BASELINES view:

select sql_handle, plan_name, enabled, accepted, fixed from dba_sql_plan_baselines;
 
SQL_HANDLE                PLAN_NAME                      ENA  ACC    FIX
------------------------------------------------------------------------
SYS_SQL_209d10fabbedc741  SYS_SQL_PLAN_bbedc741a57b5fc2  YES  NO     NO
SYS_SQL_209d10fabbedc741  SYS_SQL_PLAN_bbedc741f554c408  YES  YES    NO

15.6 SQL Management Base

The SQL management base (SMB) is a part of the data dictionary that resides in the SYSAUX tablespace. It stores statement log, plan histories, SQL plan baselines, and SQL profiles. To allow weekly purging of unused plans and logs, the SMB is configured with automatic space management enabled.

You can also add plans manually to the SMB for a set of SQL statements. This feature is especially useful when upgrading Oracle Database from a pre-11g version, since it helps to minimize plan regressions resulting from the use of a new optimizer version.

Because the SMB is stored entirely within the SYSAUX tablespace, SQL plan management and SQL tuning features will not be used if this tablespace is not available.

15.6.1 Disk Space Usage

Disk space used by the SQL management base is regularly checked against a limit based on the size of the SYSAUX tablespace. By default, the limit for the SMB is no more than 10% of the size of the SYSAUX tablespace. The allowable range for this limit is between 1% and 50%. A weekly background process measures the total space occupied by the SMB, and when the defined limit is exceeded, the process will generate a warning that is written to the alert log. The alerts are generated weekly until either the SMB space limit is increased, the size of the SYSAUX tablespace is increased, or the disk space used by the SMB is decreased by purging SQL management objects (SQL plan baselines or SQL profiles).

To change the percentage limit, use the CONFIGURE procedure of the DBMS_SPM package:

BEGIN
  DBMS_SPM.CONFIGURE('space_budget_percent',30);
END;
/

In this example, the space limit is changed to 30%. To learn about additional parameters used by the CONFIGURE procedure, see Oracle Database PL/SQL Packages and Types Reference.

15.6.2 Purging Policy

A weekly scheduled purging task manages the disk space used by SQL plan management. The task runs as an automated task in the maintenance window. Plans not used for more than 53 weeks are purged, as identified by the LAST_EXECUTED timestamp stored in the SMB for that plan. The 53-week time frame ensures plan information will be available during any yearly SQL processing activity. The unused plan retention period can range between 5 and 523 weeks (a little more than 10 years).

To configure the retention period, use the CONFIGURE procedure of the DBMS_SPM PL/SQL package:

BEGIN
  DBMS_SPM.CONFIGURE(
    'plan_retention_weeks',105);
END;
/

In this example, the retention period is changed to 105 weeks. To learn about additional parameters used by the CONFIGURE procedure, see Oracle Database PL/SQL Packages and Types Reference.

15.6.3 SQL Management Base Configuration Parameters

The current configuration settings for the SQL management base can be viewed using the DBA_SQL_MANAGEMENT_CONFIG view. The following query shows this information:

select parameter_name, parameter_value from dba_sql_management_config;
 
PARAMETER_NAME                 PARAMETER_VALUE
------------------------------ ---------------
SPACE_BUDGET_PERCENT                        30
PLAN_RETENTION_WEEKS                       105

15.7 Importing and Exporting SQL Plan Baselines

Oracle Database supports the export and import of SQL plan baselines using the Oracle Data Pump Import and Export utilities. Use the DBMS_SPM package to define a staging table, which is then used to pack and unpack SQL plan baselines.

To import a set of SQL plan baselines from one system to another:

  1. On the original system, create a staging table using the CREATE_STGTAB_BASELINE procedure:

    BEGIN
      DBMS_SPM.CREATE_STGTAB_BASELINE(
        table_name => 'stage1');
    END;
    /
    

    This example creates a staging table named stage1.

  2. Pack the SQL plan baselines you want to export from the SQL management base into the staging table using the PACK_STGTAB_BASELINE function:

    DECLARE
    my_plans number;
    BEGIN
      my_plans := DBMS_SPM.PACK_STGTAB_BASELINE(
        table_name => 'stage1',
        enabled => 'yes',
        creator => 'dba1');
    END;
    /
    

    This example packs enabled plan baselines created by user dba1 into staging table stage1. You can select SQL plan baselines using the plan name (plan_name), SQL handle (sql_handle), or any other plan criteria. The table_name parameter is mandatory.

  3. Export the staging table stage1 into a flat file using the Oracle Data Pump Export utility.

  4. Transfer the flat file to the target system.

  5. Import the staging table stage1 from the flat file using the Oracle Data Pump Import utility.

  6. Unpack the SQL plan baselines from the staging table into the SQL management base on the target system using the UNPACK_STGTAB_BASELINE function:

    DECLARE
    my_plans number;
    BEGIN
      my_plans := DBMS_SPM.UNPACK_STGTAB_BASELINE(
        table_name => 'stage1',
        fixed => 'yes');
    END;
    /
    

    This example unpacks all fixed plan baselines stored in the staging table stage1.

See Also:

15.8 Migrating Stored Outlines to SQL Plan Baselines

This section explains the concepts and tasks relating to stored outline migration. This section contains the following topics:

15.8.1 Overview of Stored Outline Migration

A stored outline is a set of hints for a SQL statement. The hints direct the optimizer to choose a specific plan for the statement. A stored outline is a legacy technique for providing plan stability.

Stored outline migration is the user-initiated process of converting stored outlines to SQL plan baselines. A SQL plan baseline is a set of plans proven to provide good performance.

This section contains the following topics:

15.8.1.1 Purpose of Stored Outline Migration

This section assumes that you rely on stored outlines to maintain plan stability and prevent performance regressions. The goal of this section is to provide a convenient method to safely migrate from stored outlines to SQL plan baselines. After the migration, you can maintain the same plan stability that you had using stored outlines while being able to utilize the more advanced features provided by the SQL Plan Management framework.

Specifically, the section explains how to address the following problems:

  • Stored outlines cannot automatically evolve over time. Consequently, a stored outline may be good when it is created, but become a bad plan after a database change, leading to performance degradation.

  • Hints in a stored outline can become invalid, for example, an index hint on a dropped index. In such cases, the database still uses the outlines but excludes the invalid hints, producing a plan that is often worse than the original plan or the current best-cost plan generated by the optimizer.

  • For a SQL statement, the optimizer can only choose the plan defined in the stored outline in the currently specified category. The optimizer cannot choose from other stored outlines in different categories or the current cost-based plan even if they improve performance.

  • Stored outlines are a reactive tuning technique, which means that you only use a stored outline to address a performance problem after it has occurred. For example, you may implement a stored outline to correct the plan of a SQL statement that became high-load. In this case, you used stored outlines instead of proactively tuning the statement before it became high-load.

The stored outline migration PL/SQL API helps solve the preceding problems in the following ways:

  • SQL plan baselines enable the optimizer to use the same good plan and allow this plan to evolve over time.

    For a specified SQL statement, you can add new plans as SQL plan baselines after they are verified not to cause performance regressions.

  • SQL plan baselines prevent plans from going bad because of invalid hints.

    If hints stored in a plan baseline become invalid, the plan may not be reproducible by the optimizer. In this case, the optimizer selects an alternative reproducible plan baseline or the current best-cost plan generated by optimizer.

  • For a specific SQL statement, the database can maintain multiple plan baselines.

    The optimizer can choose from a set of good plans for a specific SQL statement instead of being restricted to a single plan per category, as required by stored outlines.

15.8.1.2 How Stored Outline Migration Works

This section explains how the database migrates stored outlines to SQL plan baselines. This information is important for performing the task of migrating stored outlines.

15.8.1.2.1 Stages of Stored Outline Migration

The following graphic shows the main stages in stored outline migration:

Description of pfgrf231.gif follows
Description of the illustration pfgrf231.gif

The migration process has the following stages:

  1. The user invokes a function that specifies which outlines should be migrated.

  2. The database processes the outlines as follows:

    1. The database copies information in the outline needed by the plan baseline.

      The database copies it directly or calculates it based on information in the outline. For example, the text of the SQL statement exists in both schemas, so the database can copy the text from outline to baseline.

    2. The database reparses the hints to obtain information not in the outline.

      The plan hash value and plan cost cannot be derived from the existing information in the outline, which necessitates reparsing the hints.

    3. The database creates the baselines.

  3. The database obtains missing information when it chooses the SQL plan baseline for the first time to execute the same SQL statement.

    The compilation environment and execution statistics are only available during execution when the plan baseline is parsed and compiled.

The migration is complete only after the preceding phases complete.

15.8.1.2.2 Outline Categories and Baseline Modules

An outline is a set of hints, whereas a SQL plan baseline is a set of plans. Because they are different technologies, some functionality of outlines does not map exactly to functionality of baselines. For example, a single SQL statement can have multiple outlines, each of which is in a different outline category, but the only category that currently exists for baselines is DEFAULT.

The equivalent of a category for an outline is a module for a SQL plan baseline. Table 15-1 explains how outline categories map to modules.

Table 15-1 Outline Categories

Concept Description Default Value

Outline Category

Specifies a user-defined grouping for a set of stored outlines.

You can use categories to maintain different stored outlines for a SQL statement. For example, a single statement can have an outline in the OLTP category and the DW category.

Each SQL statement can have one or more stored outlines. Each stored outline is in one and only one outline category. A statement can have multiple stored outlines in different categories, but only one stored outline exists per category per statement.

During migration, the database maps each outline category to a SQL plan baseline module.

DEFAULT

Baseline Module

Specifies a high-level function being performed.

A SQL plan baseline can belong to one and only one module.

After an outline is migrated to a SQL plan baseline, module name defaults to outline category name

Baseline Category

Only one SQL plan baseline category exists. This category is named DEFAULT. During stored outline migration, the module name of the SQL plan baseline is set to the category name of the stored outline.

A statement can have multiple SQL plan baselines in the DEFAULT category.

DEFAULT


When migrating stored outlines to SQL plan baselines, Oracle Database maps every outline category to a SQL plan baseline module with the same name. As shown in the following diagram, the outline category OLTP is mapped to the baseline module OLTP. After migration, DEFAULT is a super-category that contains all SQL plan baselines.

Description of pfgrf230.gif follows
Description of the illustration pfgrf230.gif

15.8.1.3 User Interface for Stored Outline Migration

You can use the DBMS_SPM package to perform the stored outline migration. Table 15-2 describes the relevant functions in this package.

Table 15-2 DBMS_SPM Functions Relating to Stored Outline Migration

DBMS_SPM Function Description

MIGRATE_STORED_OUTLINE

Migrates existing stored outlines to plan baselines.

Use either of the following formats:

  • Specify outline name, SQL text, outline category, or all stored outlines.

  • Specify a list of outline names.

ALTER_SQL_PLAN_BASELINE

Changes an attribute of a single plan or all plans associated with a SQL statement.

DROP_MIGRATED_STORED_OUTLINE

Drops stored outlines that have already been migrated to SQL plan baselines.

The function finds stored outlines marked as MIGRATED in the DBA_OUTLINES view, and then drops these outlines from the database.


You can control stored outline and plan baseline behavior with initialization and session parameters. Table 15-3 describes the relevant parameters. See Table 15-5 and Table 15-6 for an explanation of how these parameter settings interact.

Table 15-3 Parameters Relating to Stored Outline Migration

Initialization or Session Parameter Description

CREATE_STORED_OUTLINES

Determines whether Oracle automatically creates and stores an outline for each query submitted during the session.

OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES

Enables or disables the automatic recognition of repeatable SQL statement and the generation of SQL plan baselines for these statements.

USE_STORED_OUTLINES

Determines whether the optimizer will use stored outlines to generate execution plans.

Note: This is a session parameter, not an initialization parameter.

OPTIMIZER_USE_SQL_PLAN_BASELINES

Enables or disables the use of SQL plan baselines stored in SQL Management Base.


You can use database views to access information relating to stored outline migration. Table 15-4 describes the following main views.

Table 15-4 Views Relating to Stored Outline Migration

View Description

DBA_OUTLINES

Describes all stored outlines in the database.

The MIGRATED column is important for outline migration and shows one of the following values: NOT-MIGRATED and MIGRATED. When MIGRATED, the stored outline has been migrated to a plan baseline and is not usable.

DBA_SQL_PLAN_BASELINES

Displays information about the SQL plan baselines currently created for specific SQL statements.

The ORIGIN column indicates how the plan baseline was created. The value STORED-OUTLINE indicates the baseline was created by migrating an outline.


See Also:

15.8.1.4 Basic Steps in Stored Outline Migration

This section explains the basic steps in using the PL/SQL API to perform stored outline migration. The basic steps are as follows:

  1. Prepare for stored outline migration.

    Review the migration prerequisites and determine how you want the migrated plan baselines to behave.

    See "Preparing for Stored Outline Migration".

  2. Do one of the following:

  3. Perform post-migration confirmation and cleanup.

    See "Performing Follow-Up Tasks After Stored Outline Migration".

15.8.2 Preparing for Stored Outline Migration

This section explains how to prepare for stored outline migration.

To prepare for stored outline migration: 

  1. Start SQL*Plus and log on as a user with SYSDBA privileges or the EXECUTE privilege on the DBMS_SPM package.

    For example, do the following to use operating system authentication to log on to a database as SYS:

    % sqlplus /nolog
    SQL> CONNECT / AS SYSDBA
    
  2. Query the stored outlines in the database.

    The following example queries all stored outlines that have not already been migrated to SQL plan baselines:

    SELECT NAME, CATEGORY, SQL_TEXT
    FROM   DBA_OUTLINES
    WHERE  MIGRATED = 'NOT-MIGRATED';
    
  3. Determine which stored outlines meet the following prerequisites for migration eligibility:

    • The statement must not be a runtime INSERT AS SELECT statement.

    • The statement must not reference a remote object.

    • This statement must not be a private stored outline.

  4. Decide whether to migrate all outlines, specified stored outlines, or outlines belonging to a specified outline category.

    If you do not decide to migrate all outlines, then list the outlines or categories that you intend to migrate.

  5. Decide whether the stored outlines migrated to SQL plan baselines should use fixed plans or nonfixed plans:

    • Fixed plans

      A fixed plan is frozen. If a fixed plan is reproducible using the hints stored in plan baseline, then the optimizer always chooses the lowest-cost fixed plan baseline over plan baselines that are not fixed. Essentially, a fixed plan baseline acts as a stored outline with valid hints.

      A fixed plan is reproducible when the database can parse the statement based on the hints stored in the plan baseline and create a plan with the same plan hash value as the one in the plan baseline. If one of more of the hints become invalid, then the database may not be able to create a plan with the same plan hash value. In this case, the plan is nonreproducible.

      If a fixed plan cannot be reproduced when parsed using its hints, then the optimizer chooses a different plan, which can be either of the following:

      • Another plan for the SQL plan baseline

      • The current cost-based plan created by the optimizer

      In some cases, a performance regression occurs because of the different plan, requiring SQL tuning.

    • Nonfixed plans

      If a plan baseline does not contain fixed plans, then SQL Plan Management considers the plans equally when picking a plan for a SQL statement.

  6. Before beginning the actual migration, ensure that the Oracle database meets the following prerequisites:

    • The database must be Enterprise Edition.

    • The database must be open and must not be in a suspended state.

    • The database must not be in restricted access (DBA), read-only, or migrate mode.

    • OCI must be available.

See Also:

15.8.3 Migrating Outlines to Utilize SQL Plan Management Features

The goals of this task are as follows:

  • To allow SQL Plan Management to select from all plans in a plan baseline for a SQL statement instead of applying the same fixed plan after migration

  • To allow the SQL plan baseline to evolve in the face of database changes by adding new plans to the baseline

The scenario in this section assumes the following:

  • You migrate all outlines.

    To migrate specific outlines, see Oracle Database PL/SQL Packages and Types Reference for details about the DBMS_SPM.MIGRATE_STORED_OUTLINE function.

  • You want the module names of the baselines to be identical to the category names of the migrated outlines.

  • You do not want the SQL plans to be fixed.

    By default, generated plans are not fixed and SQL Plan Management considers all plans equally when picking a plan for a SQL statement. This situation permits the advanced feature of plan evolution to capture new plans for a SQL statement, verify their performance, and accept these new plans into the plan baseline.

To migrate stored outlines to SQL plan baselines: 

  1. In SQL*Plus, call PL/SQL function MIGRATE_STORED_OUTLINE.

    The following sample PL/SQL block migrates all stored outlines to fixed baselines:

    DECLARE
      my_report CLOB;
    BEGIN
      my_outlines := DBMS_SPM.MIGRATE_STORED_OUTLINE( attribute_name => 'all' );
    END;
    /
    

See Also:

15.8.4 Migrating Outlines to Preserve Stored Outline Behavior

The goal of this task is to migrate stored outlines to SQL plan baselines and preserve the original behavior of the stored outlines by creating fixed plan baselines. A fixed plan has higher priority over other plans for the same SQL statement. If a plan is fixed, then the plan baseline cannot be evolved. The database does not add new plans to a plan baseline that contains a fixed plan.

This section assumes the following:

  • You want to migrate only the stored outlines in the category named firstrow.

    See Oracle Database PL/SQL Packages and Types Reference for syntax and semantics of the DBMS_SPM.MIGRATE_STORED_OUTLINE function.

  • You want the module names of the baselines to be identical to the category names of the migrated outlines.

To migrate stored outlines to plan baselines: 

  1. In SQL*Plus, call PL/SQL function MIGRATE_STORED_OUTLINE.

    The following sample PL/SQL block migrates stored outlines in the category firstrow to fixed baselines:

    DECLARE
      my_report CLOB;
    BEGIN
      my_outlines := DBMS_SPM.MIGRATE_STORED_OUTLINE( 
        attribute_name => 'category', 
        attribute_value => 'firstrow',
        fixed => 'YES' );
    END;
    /
    

    After the migration, the SQL plan baselines will be in module firstrow and category DEFAULT.

See Also:

15.8.5 Performing Follow-Up Tasks After Stored Outline Migration

The goals of this task are as follows:

  • To configure the database to use plan baselines instead of stored outlines for stored outlines that have been migrated to SQL plan baselines

  • To create SQL plan baselines instead of stored outlines for future SQL statements

  • To drop the stored outlines that have already been migrated to SQL plan baselines

This section assumes the following:

  • You have completed the basic steps in the stored outline migration.

  • Some stored outlines may have been created before Oracle Database 10g.

    Hints in in releases before Oracle Database 10g use a local hint format. After migration, hints stored in a plan baseline use the global hints format introduced in Oracle Database 10g.

This section explains how to set initialization parameters relating to stored outlines and plan baselines. The OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES and CREATE_STORED_OUTLINES initialization parameters determine how and when the database creates stored outlines and SQL plan baselines. Table 15-5 explains the interaction between these parameters.

Table 15-5 Creation of Outlines and Baselines

CREATE_STORED_OUTLINES Initialization Parameter OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES Initialization Parameter Database Behavior

FALSE

FALSE

When executing a SQL statement, the database does not create stored outlines or SQL plan baselines.

FALSE

TRUE

The automatic recognition of repeatable SQL statements and the generation of SQL plan baselines for these statements is enabled. When executing a SQL statement, the database creates only new SQL plan baselines (if they do not already exist) with the category name DEFAULT for the statement.

TRUE

FALSE

Oracle Database automatically creates and stores an outline for each query submitted during the session. When executing a SQL statement, the database creates only new stored outlines (if they do not already exist) with the category name DEFAULT for the statement.

category

FALSE

When executing a SQL statement, the database creates only new stored outlines (if they do not already exist) with the specified category name for the statement.

TRUE

TRUE

Oracle Database automatically creates and stores an outline for each query submitted during the session. The automatic recognition of repeatable SQL statements and the generation of SQL plan baselines for these statements is also enabled.

When executing a SQL statement, the database creates both stored outlines and SQL plan baselines with the category name DEFAULT.

category

TRUE

Oracle Database automatically creates and stores an outline for each query submitted during the session. The automatic recognition of repeatable SQL statements and the generation of SQL plan baselines for these statements is also enabled.

When executing a SQL statement, the database creates stored outlines with the specified category name and SQL plan baselines with the category name DEFAULT.


The USE_STORED_OUTLINES session parameter (it is not an initialization parameter) and OPTIMIZER_USE_SQL_PLAN_BASELINES initialization parameter determine how the database uses stored outlines and plan baselines. Table 15-6 explains how these parameters interact.

Table 15-6 Use of Stored Outlines and SQL Plan Baselines

USE_STORED_OUTLINES Session Parameter OPTIMIZER_USE_SQL_PLAN_BASELINES Initialization Parameter Database Behavior

FALSE

FALSE

When choosing a plan for a SQL statement, the database does not use stored outlines or plan baselines.

FALSE

TRUE

When choosing a plan for a SQL statement, the database uses only SQL plan baselines.

TRUE

FALSE

When choosing a plan for a SQL statement, the database uses stored outlines with the category name DEFAULT.

category

FALSE

When choosing a plan for a SQL statement, the database uses stored outlines with the specified category name.

If a stored outline with the specified category name does not exist, then the database uses a stored outline in the DEFAULT category if it exists.

TRUE

TRUE

When choosing a plan for a SQL statement, stored outlines take priority over plan baselines.

If a stored outline with the category name DEFAULT exists for the statement and is applicable, then the database applies the stored outline. Otherwise, the database uses SQL plan baselines.

category

TRUE

When choosing a plan for a SQL statement, stored outlines take priority over plan baselines.

If a stored outline with the specified category name or the DEFAULT category exists for the statement and is applicable, then the database applies the stored outline. Otherwise, the database uses SQL plan baselines. However, if the stored outline has the property MIGRATED, then the database does not use the outline and uses the corresponding SQL plan baseline instead (if it exists).


To place the database in the proper state after the migration: 

  1. Check that SQL plan baselines have been created as the result of migration.

    Ensure that the plans are enabled and accepted. For example, enter the following query (partial sample output included):

    SELECT SQL_HANDLE, PLAN_NAME, ORIGIN, ENABLED, ACCEPTED, FIXED, MODULE
    FROM   DBA_SQL_PLAN_BASELINES;
    
    SQL_HANDLE                     PLAN_NAME  ORIGIN         ENA ACC FIX MODULE
    ------------------------------ ---------- -------------- --- --- --- ------
    SYS_SQL_f44779f7089c8fab       STMT01     STORED-OUTLINE YES YES NO  DEFAULT
    .
    .
    .
    
  2. Optionally, change the attributes of the SQL plan baselines.

    For example, the following statement changes the status of the baseline for the specified SQL statement to fixed:

    DECLARE
      v_cnt PLS_INTEGER;
    BEGIN 
      v_cnt := DBMS_SPM.ALTER_SQL_PLAN_BASELINE(               
                               sql_handle=>'SYS_SQL_f44779f7089c8fab', 
                               attribute_name=>'FIXED', 
                               attribute_value=>'NO');
      DBMS_OUTPUT.PUT_LINE('Plans altered: ' || v_cnt);
    END;
    /
    
  3. Check the status of the original stored outlines.

    For example, enter the following query (partial sample output included):

    SELECT NAME, OWNER, CATEGORY, USED, MIGRATED 
    FROM   DBA_OUTLINES
    ORDER BY NAME;
    
    NAME       OWNER      CATEGORY   USED   MIGRATED
    ---------- ---------- ---------- ------ ------------
    STMT01     SYS        DEFAULT    USED   MIGRATED
    STMT02     SYS        DEFAULT    USED   MIGRATED
    .
    .
    .
    
  4. Drop all stored outlines that have been migrated to SQL plan baselines.

    For example, the following statements drops all stored outlines with status MIGRATED in DBA_OUTLINES:

    DECLARE
      v_cnt PLS_INTEGER;
    BEGIN 
      v_cnt := DBMS_SPM.DROP_MIGRATED_STORED_OUTLINE();
      DBMS_OUTPUT.PUT_LINE('Migrated stored outlines dropped: ' || v_cnt);
    END;
    /
    
  5. Set initialization parameters so that:

    • When executing a SQL statement, the database creates plan baselines but does not create stored outlines.

    • The database only uses stored outlines when the equivalent SQL plan baselines do not exist.

    For example, the following SQL statements instruct the database to create SQL plan baselines instead of stored outlines when a SQL statement is executed. The example also instructs the database to apply a stored outline in category allrows or DEFAULT only if it exists and has not been migrated to a SQL plan baseline. In other cases, the database applies SQL plan baselines instead.

    ALTER SYSTEM 
      SET CREATE_STORED_OUTLINE = false;
    
    ALTER SYSTEM 
      SET OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES = true;
    
    ALTER SYSTEM 
       SET OPTIMIZER_USE_SQL_PLAN_BASELINES = true;
    
    ALTER SESSION
       SET USE_STORED_OUTLINES = allrows;
    

See Also: