sql 计划基线是用来影响查询优化器生成执行计划的对象。sql基线包含了一个或多个执行计划,而执行计划里包含一组hint。sql计划基线用于强迫优化器针对给定sql语句生成特定的执行计划。
sql计划基线的其中一个优势它适用于某个特定的sql语句,并且不需要修改sql语句本身。实际上,sql计划基线存储再sqll基础管理平台上。并且查询优化器会自动选择它们。
optimizer generates an execution plan without the support of a SQL plan baseline.
2. Then, the query optimizer normalizes the SQL statement to make it both case-insensitive
and independent of the blank spaces present in the text. The signature of the resulting SQL
statement is computed, and a lookup into the SQL Management Base is performed.
If a SQL plan baseline with the same signature is found, a check is performed to make sure
that the SQL statement to be optimized and the SQL statement associated with the SQL
plan baseline are equivalent. This check is necessary because the signature is a hash value,
and consequently, there can be conflicts.
3. When the test is successful, the query optimizer verifies whether the SQL plan baseline
contains the execution plan generated without the SQL plan baseline. If it’s contained
within it and accepted (trusted), it’s executed.
4. If another accepted execution plan is stored in the SQL plan baseline, the hints associated
to it are used for the generation of another execution plan. Note that if the SQL plan
baseline contains several accepted execution plans, the query optimizer selects the one
with the lowest cost.
5. Lastly, the query optimizer checks whether the execution plan generated with the
information provided by the SQL plan baseline reproduced the expected execution
plan. Only if this last check is fulfilled, the execution plan can be used. If it’s not satisfied,
the query optimizer tries the other accepted execution plans or, if all of them are
unreproducible, it falls back to the execution plan generated without the SQL plan baseline.
DBMS_SPM.load_plans_from_cursor_cache(
sql_id => '8d6dshztauuct',
fixed => 'YES');
This function loads one or more plans present in the cursor cache for a SQL statement, or a set of SQL statements. It has four overloads: using SQL statement text, using SQL handle, using SQL ID, or using attribute_name and attribute_value pair.
fixed:
Default |
ALTER_SQL_PLAN_BASELINE Function
This function changes an attribute of a single plan or all plans associated with a SQL statement using the attribute name/value format.
Name | Description | Possible Values |
---|---|---|
|
' |
' |
|
' |
' |
|
' |
' |
|
Name of the plan |
String of up to 30-characters |
|
Plan description. |
String of up to 500-characters |
DROP_SQL_PLAN_BASELINE Function
This function drops a single plan, or all plans associated with a SQL statement.
— export / transfer baselines to a different instance
— create a staging table
execute dbms_spm.CREATE_STGTAB_BASELINE(table_name=>’RGRAEFF_STAGE_BL’,table_owner=>’RGRAEFF’);
— export them to the staging table
variable var varchar2(30);
execute :var:=dbms_spm.PACK_STGTAB_BASELINE(table_name=>’RGRAEFF_STAGE_BL’ ,table_owner=>’RGRAEFF’);
execute dbms_output.put_line(‘Baselines exportiert: ‘||:var);
— now this table can be exported/imported via datapump to the target instance
— with dbms_spm.UNPACK_STGTAB_BASELINE the exported baselines can be loaded
— remove test tablesdrop table tst purge;
CREATE_STGTAB_BASELINE Procedure
This procedure creates a staging table used for transporting SQL plan baselines from one system to another.
PACK_STGTAB_BASELINE Function
This function packs (exports) SQL plan baselines from SQL management base into a staging table.
UNPACK_STGTAB_BASELINE Function
This function unpacks (imports) SQL plan baselines from a staging table into SQL management base.
实验连接:https://mygraeff.wordpress.com/2011/05/04/sql-plan-management-how-to-fix-an-execution-plan/