sql 计划管理

  sql 计划基线是用来影响查询优化器生成执行计划的对象。sql基线包含了一个或多个执行计划,而执行计划里包含一组hint。sql计划基线用于强迫优化器针对给定sql语句生成特定的执行计划。

  sql计划基线的其中一个优势它适用于某个特定的sql语句,并且不需要修改sql语句本身。实际上,sql计划基线存储再sqll基础管理平台上。并且查询优化器会自动选择它们。

1.  First, the SQL statement is parsed in the conventional way. In other words, the query
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 'NO' means the loaded plans are used as non-fixed plans. Value 'YES' means the loaded plans are used as fixed plans and the SQL plan baseline will not be evolved over time.

扫描二维码关注公众号,回复: 1679391 查看本文章

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

enabled

'YES' means the plan is available for use by the optimizer. It may or may not be used depending on accepted status.

'YES' or 'NO'

fixed

'YES' means the SQL plan baseline is not evolved over time. A fixed plan takes precedence over a non-fixed plan.

'YES' or 'NO'

autopurge

'YES' means the plan is purged if it is not used for a time period. 'NO' means it is never purged.

'YES' or 'NO'

plan_name

Name of the plan

String of up to 30-characters

description

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 tables
drop 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/




猜你喜欢

转载自blog.csdn.net/j_ychen/article/details/79618899
今日推荐