SQL Outlines
SQL Outlines
====================================
Plan Stability
==============
Plan stability forces the optimizer to use the same unvarying execution plan for a
statement.
It is implemented using stored outlines which are stored in the OUTLN schema.
o Oracle version
o Optimizer mode
o Statistics
o Data volumes
o Data distribution
o Creation of new indexes
Storing Outlines
==================
Stored outlines can be generated for a single atement or at the session or the
instance level
CREATE_STORED_OUTLINES
======================
By setting this parameter to TRUE or a category name, all subsequent SQL statements
in your session (or the instance) generate stored outlines. If this parameter is
set to an existing category, then new outlines will be added to that category.
If no category name is specified, the stored outlines are added to the DEFAULT
category.
If you do not specify NOOVERRIDE, this system setting takes effect in all sessions.
Existing outlines in the category are not updated if these should happen to be
executed again with a different execution plan during the period of storing
outlines in the category.
CREATE OUTLINE
================
This command creates (or replaces) the stored outline for the specified statement
without executing the statement. Note: Outline names must be unique across the
database.
Data Dictionary
=================
Stored outlines are stored as a set of hints necessary to reproduce the execution
plan.
{USER|ALL|DBA}_OUTLINES
{USER|ALL|DBA}_OUTLINE_HINTS
Columns in USER_OUTLINES
Columns in USER_OUTLINE_HINTS
Note that the normal, documented hints will not be able to reproduce all execution
plans.
To solve these problems stored outlines not only employ hints not supported as
normal hints (for example, NO_FACT) but they also use information on where in the
statement the hints apply (the NODE column), in what sequence tables
should be joined, and at what stage in the statement compilation the hint should be
applied.
For reasons of complexity it should therefore be obvious that direct editing of the
contents in the underlying table OUTLN.OL$HINTS is not supported nor encouraged.
When setting use_stored_outlines to TRUE, the category named DEFAULT will be used.
Setting CURSOR_SHARING to SIMILAR changes the SQL text in the presence of literals.
Hence, it prevents any outlines generated with literals from being used. Literals
in CREATE [OR REPLACE] OUTLINE... statements are not
replaced by bind variables.
Stored outlines will not be used if not all hints are valid. This is in contrast to
normal hints where if one hint fails (for example, an index has been removed) the
rest of the hints are still considered.
To test whether a statement did indeed use a stored outline, query the column
OUTLINE_CATEGORY in V$SQL (does not exist in V$SQLAREA); if this column is NULL the
statement did not use a stored outline.
Solution
Create the following trigger to be run on startup of the database:
create or replace trigger enable_outlines_trig
after startup on database
begin
execute immediate('alter system set use_stored_outlines=true');
end;
A Stored Outline can be rebuilt according to present conditions; the statement can
be re-parsed and a new set of outline hints stored.
o DROP_UNUSED - Drops all stored outlines that have never been used
(USED column in %_OUTLINES).
o DROP_BY_CAT (category) - Drops all stored outlines within the specified
category.
Export and import of the tables used for stored outlines is supported.
Exporting With a Condition By using the QUERY parameter of the export utility it is
possible to export a particular outline or category to later import into another
database.
Windows NT example:
exp outln/outln FILE=exportfile TABLES='OL$' 'OL$HINTS'
QUERY=\"WHERE CATEGORY='category'\"
Set the OPTIMIZER_MODE to RULE, and enable the creation of stored outlines for your
session.
Suppose a query on the REGISTRATIONS table has the following execution plan:
Execution Plan
----------------------------------------------------
SELECT STATEMENT Optimizer=RULE
SORT (AGGREGATE)
TABLE ACCESS (BY INDEX ROWID) OF 'REGISTRATIONS'
INDEX (RANGE SCAN) OF 'REG_PK' (UNIQUE)
If this plan is stored in a outline called "Outline_Test" then this can be used to
force the desired plan in preference to one the optimizer has chosen.
To choose a different plan, disable the creation of stored outlines, and change the
session environment:
Running the same SQL statement again probably displays the following execution
plan:
Execution Plan
------------------------------------------
SELECT STATEMENT Optimizer=ALL_ROWS (Cost=43 Card=1 Bytes=11)
SORT (AGGREGATE)
TABLE ACCESS (FULL) OF 'REGISTRATIONS'
Execution Plan
----------------------------------------------------------------
SELECT STATEMENT Optimizer=ALL_ROWS (Cost=11779 Card=1 Bytes=11)
SORT (AGGREGATE)
TABLE ACCESS (BY INDEX ROWID) OF 'REGISTRATIONS'
INDEX (RANGE SCAN) OF 'REG_PK' (UNIQUE)
The execution plan from the RBO environment reappears even though the normal
indicators of the CBO (costs in the execution plan) are present.