如何使用SQL PROFILE固定当前SHARED POOL的执行计划。
如果采用手工的方法,需要用到 sqlprof_attr 类型。
自己手工构造相对麻烦点。
ORACLE提供了一个脚本coe_xfr_sql_profile.sql 用来用共享池中获得SQL语句及其执行计划,
并生成一个创建SQL PROFLE的脚本,这个脚本附属在SQLT工具中。
看一下这个脚本的使用方法。
SQL> desc test
名称 是否为空? 类型
----------------------------------------------------- -------- ------------------------------------
OWNER NOT NULL VARCHAR2(30)
OBJECT_NAME NOT NULL VARCHAR2(30)
SUBOBJECT_NAME VARCHAR2(30)
OBJECT_ID NOT NULL NUMBER
DATA_OBJECT_ID NUMBER
OBJECT_TYPE VARCHAR2(19)
CREATED NOT NULL DATE
LAST_DDL_TIME NOT NULL DATE
TIMESTAMP VARCHAR2(19)
STATUS VARCHAR2(7)
TEMPORARY VARCHAR2(1)
GENERATED VARCHAR2(1)
SECONDARY VARCHAR2(1)
SQL> set autot traceonly exp
SQL> select count(1) from test where object_type='SYNONYM';
执行计划
----------------------------------------------------------
Plan hash value: 3958077978
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 9 | 35 (3)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 9 | | |
|* 2 | INDEX FAST FULL SCAN| IDX_CPIC_01 | 19221 | 168K| 35 (3)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("OBJECT_TYPE"='SYNONYM')
SQL> set autot off
SQL> column sql_text format a40
SQL> select sql_id,sql_text from v$sql where sql_text like 'select count(1) from test where object_type=%';
SQL_ID SQL_TEXT
------------- ----------------------------------------
8ma7qaqmmt5bn select count(1) from test where object_t
ype='SYNONYM'
SQL> select plan_hash_value from v$sql_plan where sql_id='8ma7qaqmmt5bn' and rownum=1;
PLAN_HASH_VALUE
---------------
3958077978
SQL>
下面我们用coe_xfr_sql_profile.sql脚本生成创建SQL PROFILE的脚本。
SQL> @coe_xfr_sql_profile.sql 8ma7qaqmmt5bn 3958077978
Parameter 1:
SQL_ID (required)
PLAN_HASH_VALUE AVG_ET_SECS
--------------- -----------
3958077978
Parameter 2:
PLAN_HASH_VALUE (required)
Values passed to coe_xfr_sql_profile:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL_ID : "8ma7qaqmmt5bn"
PLAN_HASH_VALUE: "3958077978"
SQL>BEGIN
2 IF :sql_text IS NULL THEN
3 RAISE_APPLICATION_ERROR(-20100, 'SQL_TEXT for SQL_ID &&sql_id. was not found in memory (gv$sqltext_with_newlines) or AWR (dba_hist_sqltext).');
4 END IF;
5 END;
6 /
SQL>SET TERM OFF;
SQL>BEGIN
2 IF :other_xml IS NULL THEN
3 RAISE_APPLICATION_ERROR(-20101, 'PLAN for SQL_ID &&sql_id. and PHV &&plan_hash_value. was not found in memory (gv$sql_plan) or AWR (dba_hist_sql_plan).');
4 END IF;
5 END;
6 /
SQL>SET TERM OFF;
Execute coe_xfr_sql_profile_8ma7qaqmmt5bn_3958077978.sql
on TARGET system in order to create a custom SQL Profile
with plan 3958077978 linked to adjusted sql_text.
COE_XFR_SQL_PROFILE completed.
SQL>
生成的脚本coe_xfr_sql_profile_8ma7qaqmmt5bn_3958077978.sql内容如下:
SPO coe_xfr_sql_profile_8ma7qaqmmt5bn_3958077978.log;
SET ECHO ON TERM ON LIN 2000 TRIMS ON NUMF 99999999999999999999;
REM
REM $Header: 215187.1 coe_xfr_sql_profile_8ma7qaqmmt5bn_3958077978.sql 11.4.4.4 2013/01/11 carlos.sierra $
REM
REM Copyright (c) 2000-2012, Oracle Corporation. All rights reserved.
REM
REM AUTHOR
REM carlos.sierra@oracle.com
REM
REM SCRIPT
REM coe_xfr_sql_profile_8ma7qaqmmt5bn_3958077978.sql
REM
REM DESCRIPTION
REM This script is generated by coe_xfr_sql_profile.sql
REM It contains the SQL*Plus commands to create a custom
REM SQL Profile for SQL_ID 8ma7qaqmmt5bn based on plan hash
REM value 3958077978.
REM The custom SQL Profile to be created by this script
REM will affect plans for SQL commands with signature
REM matching the one for SQL Text below.
REM Review SQL Text and adjust accordingly.
REM
REM PARAMETERS
REM None.
REM
REM EXAMPLE
REM SQL> START coe_xfr_sql_profile_8ma7qaqmmt5bn_3958077978.sql;
REM
REM NOTES
REM 1. Should be run as SYSTEM or SYSDBA.
REM 2. User must have CREATE ANY SQL PROFILE privilege.
REM 3. SOURCE and TARGET systems can be the same or similar.
REM 4. To drop this custom SQL Profile after it has been created:
REM EXEC DBMS_SQLTUNE.DROP_SQL_PROFILE('coe_8ma7qaqmmt5bn_3958077978');
REM 5. Be aware that using DBMS_SQLTUNE requires a license
REM for the Oracle Tuning Pack.
REM 6. If you modified a SQL putting Hints in order to produce a desired
REM Plan, you can remove the artifical Hints from SQL Text pieces below.
REM By doing so you can create a custom SQL Profile for the original
REM SQL but with the Plan captured from the modified SQL (with Hints).
REM
WHENEVER SQLERROR EXIT SQL.SQLCODE;
REM
VAR signature NUMBER;
VAR signaturef NUMBER;
REM
DECLARE
sql_txt CLOB;
h SYS.SQLPROF_ATTR;
PROCEDURE wa (p_line IN VARCHAR2) IS
BEGIN
DBMS_LOB.WRITEAPPEND(sql_txt, LENGTH(p_line), p_line);
END wa;
BEGIN
DBMS_LOB.CREATETEMPORARY(sql_txt, TRUE);
DBMS_LOB.OPEN(sql_txt, DBMS_LOB.LOB_READWRITE);
-- SQL Text pieces below do not have to be of same length.
-- So if you edit SQL Text (i.e. removing temporary Hints),
-- there is no need to edit or re-align unmodified pieces.
wa(q'[select count(1) from test where object_type='SYNONYM' ]');
DBMS_LOB.CLOSE(sql_txt);
h := SYS.SQLPROF_ATTR(
q'[BEGIN_OUTLINE_DATA]',
q'[IGNORE_OPTIM_EMBEDDED_HINTS]',
q'[OPTIMIZER_FEATURES_ENABLE('10.2.0.1')]',
q'[ALL_ROWS]',
q'[OUTLINE_LEAF(@"SEL$1")]',
q'[INDEX_FFS(@"SEL$1" "TEST"@"SEL$1" ("TEST"."OBJECT_TYPE"))]',
q'[END_OUTLINE_DATA]');
:signature := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt);
:signaturef := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt, TRUE);
DBMS_SQLTUNE.IMPORT_SQL_PROFILE (
sql_text => sql_txt,
profile => h,
name => 'coe_8ma7qaqmmt5bn_3958077978',
description => 'coe 8ma7qaqmmt5bn 3958077978 '||:signature||' '||:signaturef||'',
category => 'DEFAULT',
validate => TRUE,
replace => TRUE,
force_match => FALSE /* TRUE:FORCE (match even when different literals in SQL). FALSE:EXACT (similar to CURSOR_SHARING) */ );
DBMS_LOB.FREETEMPORARY(sql_txt);
END;
/
WHENEVER SQLERROR CONTINUE
SET ECHO OFF;
PRINT signature
PRINT signaturef
PRO
PRO ... manual custom SQL Profile has been created
PRO
SET TERM ON ECHO OFF LIN 80 TRIMS OFF NUMF "";
SPO OFF;
PRO
PRO COE_XFR_SQL_PROFILE_8ma7qaqmmt5bn_3958077978 completed
我们可以直接使用这个脚本,也可以自己根据实际情况进行修改后在执行。
SQL>@coe_xfr_sql_profile_8ma7qaqmmt5bn_3958077978.sql
SQL>REM
SQL>REM $Header: 215187.1 coe_xfr_sql_profile_8ma7qaqmmt5bn_3958077978.sql 11.4.4.4 2013/01/11 carlos.sierra $
SQL>REM
SQL>REM Copyright (c) 2000-2012, Oracle Corporation. All rights reserved.
SQL>REM
SQL>REM AUTHOR
SQL>REM carlos.sierra@oracle.com
SQL>REM
SQL>REM SCRIPT
SQL>REM coe_xfr_sql_profile_8ma7qaqmmt5bn_3958077978.sql
SQL>REM
SQL>REM DESCRIPTION
SQL>REM This script is generated by coe_xfr_sql_profile.sql
SQL>REM It contains the SQL*Plus commands to create a custom
SQL>REM SQL Profile for SQL_ID 8ma7qaqmmt5bn based on plan hash
SQL>REM value 3958077978.
SQL>REM The custom SQL Profile to be created by this script
SQL>REM will affect plans for SQL commands with signature
SQL>REM matching the one for SQL Text below.
SQL>REM Review SQL Text and adjust accordingly.
SQL>REM
SQL>REM PARAMETERS
SQL>REM None.
SQL>REM
SQL>REM EXAMPLE
SQL>REM SQL> START coe_xfr_sql_profile_8ma7qaqmmt5bn_3958077978.sql;
SQL>REM
SQL>REM NOTES
SQL>REM 1. Should be run as SYSTEM or SYSDBA.
SQL>REM 2. User must have CREATE ANY SQL PROFILE privilege.
SQL>REM 3. SOURCE and TARGET systems can be the same or similar.
SQL>REM 4. To drop this custom SQL Profile after it has been created:
SQL>REM EXEC DBMS_SQLTUNE.DROP_SQL_PROFILE('coe_8ma7qaqmmt5bn_3958077978');
SQL>REM 5. Be aware that using DBMS_SQLTUNE requires a license
SQL>REM for the Oracle Tuning Pack.
SQL>REM 6. If you modified a SQL putting Hints in order to produce a desired
SQL>REM Plan, you can remove the artifical Hints from SQL Text pieces below.
SQL>REM By doing so you can create a custom SQL Profile for the original
SQL>REM SQL but with the Plan captured from the modified SQL (with Hints).
SQL>REM
SQL>WHENEVER SQLERROR EXIT SQL.SQLCODE;
SQL>REM
SQL>VAR signature NUMBER;
SQL>VAR signaturef NUMBER;
SQL>REM
SQL>DECLARE
2 sql_txt CLOB;
3 h SYS.SQLPROF_ATTR;
4 PROCEDURE wa (p_line IN VARCHAR2) IS
5 BEGIN
6 DBMS_LOB.WRITEAPPEND(sql_txt, LENGTH(p_line), p_line);
7 END wa;
8 BEGIN
9 DBMS_LOB.CREATETEMPORARY(sql_txt, TRUE);
10 DBMS_LOB.OPEN(sql_txt, DBMS_LOB.LOB_READWRITE);
11 -- SQL Text pieces below do not have to be of same length.
12 -- So if you edit SQL Text (i.e. removing temporary Hints),
13 -- there is no need to edit or re-align unmodified pieces.
14 wa(q'[select count(1) from test where object_type='SYNONYM' ]');
15 DBMS_LOB.CLOSE(sql_txt);
16 h := SYS.SQLPROF_ATTR(
17 q'[BEGIN_OUTLINE_DATA]',
18 q'[IGNORE_OPTIM_EMBEDDED_HINTS]',
19 q'[OPTIMIZER_FEATURES_ENABLE('10.2.0.1')]',
20 q'[ALL_ROWS]',
21 q'[OUTLINE_LEAF(@"SEL$1")]',
22 q'[INDEX_FFS(@"SEL$1" "TEST"@"SEL$1" ("TEST"."OBJECT_TYPE"))]',
23 q'[END_OUTLINE_DATA]');
24 :signature := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt);
25 :signaturef := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt, TRUE);
26 DBMS_SQLTUNE.IMPORT_SQL_PROFILE (
27 sql_text => sql_txt,
28 profile => h,
29 name => 'coe_8ma7qaqmmt5bn_3958077978',
30 description => 'coe 8ma7qaqmmt5bn 3958077978 '||:signature||' '||:signaturef||'',
31 category => 'DEFAULT',
32 validate => TRUE,
33 replace => TRUE,
34 force_match => FALSE /* TRUE:FORCE (match even when different literals in SQL). FALSE:EXACT (similar to CURSOR_SHARING) */ );
35 DBMS_LOB.FREETEMPORARY(sql_txt);
36 END;
37 /
PL/SQL 过程已成功完成。
SQL>WHENEVER SQLERROR CONTINUE
SQL>SET ECHO OFF;
SIGNATURE
---------------------
5779641010256874563
SIGNATUREF
---------------------
2423517717540587142
... manual custom SQL Profile has been created
COE_XFR_SQL_PROFILE_8ma7qaqmmt5bn_3958077978 completed
SQL>select name,sql_text,status from dba_sql_profiles;
NAME SQL_TEXT STATUS
------------------------------ -------------------------------------------------------------------------------- --------
coe_8ma7qaqmmt5bn_3958077978 select count(1) from test where object_type='SYNONYM' ENABLED
SQL>select sql_text,sql_profile from v$sql where sql_id='8ma7qaqmmt5bn';
SQL_TEXT SQL_PROFILE
------------------------------ ------------------------------
select count(1) from test wher
e object_type='SYNONYM'
SQL>comment on table test is '';--将当期的执行计划淘汰出去
注释已创建。
SQL>select sql_text,sql_profile from v$sql where sql_id='8ma7qaqmmt5bn';
未选定行
SQL>set autot traceonly exp
SQL>select count(1) from test where object_type='SYNONYM';
执行计划
----------------------------------------------------------
Plan hash value: 3958077978
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 9 | 35 (3)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 9 | | |
|* 2 | INDEX FAST FULL SCAN| IDX_CPIC_01 | 19221 | 168K| 35 (3)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("OBJECT_TYPE"='SYNONYM')
Note
-----
- SQL profile "coe_8ma7qaqmmt5bn_3958077978" used for this statement <-- 可以看到SQL PROFILE已经正常工作了。
SQL>set autot off
SQL>select sql_text,sql_profile from v$sql where sql_id='8ma7qaqmmt5bn';
SQL_TEXT SQL_PROFILE
------------------------------ ------------------------------
select count(1) from test wher coe_8ma7qaqmmt5bn_3958077978
e object_type='SYNONYM'
相关推荐
**Shared Pool** 是Oracle数据库实例内存结构SGA(System Global Area)中的一个重要组成部分,主要用于存储共享的数据结构,如SQL语句、PL/SQL代码、执行计划、数据字典缓存等。它对提升数据库性能至关重要,特别是...
- **Shared Pool**:管理共享池中的PL/SQL代码和SQL解析信息,避免软解析。 4. **数据库并行执行** - **并行查询**:通过并行执行选项(PARALLEL)加速大型查询,但需要权衡资源消耗。 - **并行DML**:并行DML...
- SQL PROFILE包含了执行计划优化的信息,使用HINT可以帮助优化SQL执行效率。 - 本书介绍了如何查看和使用这些HINT来改进SQL性能。 - 实例: - 使用`DBMS_XPLAN.DISPLAY_CURSOR`显示带有HINT的执行计划。 - ...
如果不存在,则Oracle将对SQL语句进行语法分析、语义分析,并生成执行计划,然后将其存储在库缓存中供后续查询使用。这一过程涉及到多个内部机制,如硬解析(Hard Parsing)、软解析(Soft Parsing)、共享池...
- `EXPLAIN PLAN`:分析SQL语句执行计划。 #### 三、Oracle数据库架构 Oracle 数据库架构主要包括以下几个方面: 1. **实例(Instance)**: - **后台进程**:如DBWn、CKPT、SMON等,负责执行特定的任务。 - *...
- **软解析**:当相同的SQL语句再次被执行时,Oracle会在Library Cache中查找是否存在该SQL语句的哈希值,如果找到并且内容相同,则直接使用已有的解析树和执行计划,避免重新生成,从而节省时间和资源。 - **硬解析...
- **主要内容**:这一章将教授如何手动调整 SQL 语句的执行计划以提升性能。 - **知识点**: - 使用 Hint 进行执行计划指导。 - 理解和调整 Cost-Based Optimizer (CBO)。 - 物理读取次数与逻辑读取次数的区别...
9. **SQL Profile和SQL Plan Baseline**:Oracle的SQL Profile可以针对特定SQL语句提供改进建议,而SQL Plan Baseline则用于锁定和管理SQL的执行计划,防止性能退化。 10. **数据库架构设计**:良好的数据库设计...
3.4 共享池统计(Shared Pool Statistics)提供了对共享池中各类内存使用情况的统计信息,帮助确定SQL语句和PL/SQL代码的执行效率。 3.5 TOP5事件(Top 5 Timed Events)列出了数据库响应时间中最耗时的五个事件,...
4. Shared Pool的构成和重要性:Shared Pool是Oracle数据库SGA(System Global Area)中一个重要的内存区域,主要用于缓存SQL语句和PL/SQL代码等。它主要分为两个部分:library cache和dictionary cache。Library ...
11. **SQL Profile与SQL Plan Baseline**:通过SQL Profile和SQL Plan Baseline,Oracle可以自动学习和保存执行良好的执行计划,避免因统计信息变化导致的性能波动。 12. **数据库维护最佳实践**:定期执行DBMS_...
二、《Oracle数据库SQL执行计划的取得和解析》PPT(附SQL文件) 三、Oracle 常用脚本 2pc_clean.txt ash_sql_line_id.txt ash_top_sql_event.txt ash_used awr_db_time.txt awr_event_histogram.txt awr_metric_...
报告中展示了Buffer Cache和Shared Pool的大小,Buffer Cache是缓存数据块的地方,Shared Pool则包含Library Cache(存储解析后的SQL和PL/SQL)和Dictionary Cache(存储数据字典)。理想的配置应该确保Shared Pool...
8. **SQL Profile与Plan Baseline**:Oracle通过SQL Profile和Plan Baseline自动学习并保存SQL语句的最佳执行计划,避免了因统计信息变化导致的性能波动。 9. **并发控制与锁**:Oracle的多版本并发控制(MVCC)和...
- 使用SQL Profile进行优化,根据实际执行情况调整执行计划。 - 利用索引优化查询,合理创建和维护索引。 - 避免使用子查询,转而使用连接查询或存在子句提高查询效率。 ##### 2. 缓存管理 - **问题描述**:缓存...
因此,Shared Pool 的设置要确保最近使用的数据都能被 cache。 四、Load Profile Load Profile 部分显示数据库负载概况,包括 Redo Size、Logical Reads、Block Changes、Physical Reads、Physical Writes、User ...
硬解析会带来cpu和资源使用的高昂开销,因为它需要oracle在shared pool中重新分配内存,然后再确定执行计划,最终语句才会被执行。 parse count (total):解析调用总数,包括软解析和硬解析。当session执行了一条...
如果存在,并且SQL完全相同,则使用现有的解析树和执行计划,避免重新生成执行计划,这就是软解析的过程。 - **Hard Parse (硬解析)**: 如果在 **Library Cache** 中找不到相同的hash值或者虽然找到了但SQL并不相同...