SQL Profile 實驗02

yepkeepmoving發表於2016-04-05
實驗說明:本實驗利用工具coe_xfr_sql_profile.sql改變執行計劃。
實驗描述
透過建立表T3和索引T3_INDEX,執行SQL語句,正常情況下該走索引A情況,透過hint強制查詢走全表掃描B情況,利用profile將A情況的執行計劃改為B情況的執行計劃,從而達到利用SQL Profile更改SQL執行計劃,讓所有查詢走全表掃描的目的。(本文僅用於便於理解profile改變執行計劃使用


##建立測試表和資料
SQL> conn scott/tiger;
Connected.
SQL> create table t3 as select * from all_objects;
Table created.
SQL> create index t3_index on t3(object_id);
Index created.
##開啟trace跟蹤
SQL> set autotrace trace explain;
##正常執行查詢SQL,其走索引
SQL> select count(*) from t3 where object_id<=5000;

Execution Plan
----------------------------------------------------------
Plan hash value: 129501593

------------------------------------------------------------------------------
| Id  | Operation         | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |          |     1 |    13 |     9   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE   |          |     1 |    13 |            |          |
|*  2 |   INDEX RANGE SCAN| T3_INDEX |  2500 | 32500 |     9   (0)| 00:00:01 |
------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("OBJECT_ID"<=5000)

Note
-----
   - dynamic sampling used for this statement (level=2)

##給執行的SQL加hint,強制走全表掃描
SQL> select /*+full(t3)*/ count(*) from t3 where object_id<=5000;

Execution Plan
----------------------------------------------------------
Plan hash value: 463314188

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    13 |   327   (1)| 00:00:04 |
|   1 |  SORT AGGREGATE    |      |     1 |    13 |            |          |
|*  2 |   TABLE ACCESS FULL| T3   |  2500 | 32500 |   327   (1)| 00:00:04 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("OBJECT_ID"<=5000)

Note
-----
   - dynamic sampling used for this statement (level=2)

#查詢SQL_ID,PLAN_HASH_VALUE,SQL_TEXT內容
SQL> col sql_id for a20;
SQL> col sql_text for a100;
SQL> set linesize 200;
SQL> set pagesize 20000;
SQL> conn /as sysdba;
SQL> select sql_id,plan_hash_value,sql_text from v$sql where sql_text like '%count(*)%t3%where%';
SQL_ID        PLAN_HASH_VALUE SQL_TEXT
------------- --------------- ------------------------------------------------------------------------------------------------------------------------
4jtu9dktxjhgk       129501593 EXPLAIN PLAN SET STATEMENT_ID='PLUS735315' FOR select count(*) from t3 where object_id<=5000
01zbb82vy2af3       463314188 EXPLAIN PLAN SET STATEMENT_ID='PLUS735315' FOR select /*+full(t3)*/ count(*) from t3 where object_id<=5000
0gmwrzua0usax       129501593 select count(*) from t3 where object_id<=5000
fdv618tykuv26       463314188 select /*+full(t3)*/ count(*) from t3 where object_id<=5000
5872fg2ftfz76       903671040 select sql_id,sql_text from v$sql where sql_text like '%count(*)%t3%where%'
4qunptrts3tpg       903671040 select sql_id,plan_hash_value,sql_text from v$sql where sql_text like '%count(*)%t3%where%'


##根據要修改的SQL_ID生成Profile檔案,其中讓本走索引的SQL_ID走全表掃描的執行計劃
SQL> @coe_xfr_sql_profile.sql

Parameter 1:
SQL_ID (required)

Enter value for 1: 0gmwrzua0usax                ##要修改的SQL語句的SQL_ID


PLAN_HASH_VALUE AVG_ET_SECS
--------------- -----------
      129501593

Parameter 2:
PLAN_HASH_VALUE (required)

Enter value for 2: 463314188                        ##將來要實現的SQL執行計劃的PLAN_HASH_VALUE
    
Values passed to coe_xfr_sql_profile:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL_ID         : "0gmwrzua0usax"
PLAN_HASH_VALUE: "463314188"

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_0gmwrzua0usax_463314188.sql
on TARGET system in order to create a custom SQL Profile
with plan 463314188 linked to adjusted sql_text.


COE_XFR_SQL_PROFILE completed.

#執行Profile匯入操作
SQL>@coe_xfr_sql_profile_0gmwrzua0usax_463314188.sql

SQL>REM
SQL>REM $Header: 215187.1 coe_xfr_sql_profile_0gmwrzua0usax_463314188.sql 11.4.4.4 2016/04/05 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_0gmwrzua0usax_463314188.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 0gmwrzua0usax based on plan hash
SQL>REM   value 463314188.
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_0gmwrzua0usax_463314188.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_0gmwrzua0usax_463314188');
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(*) from t3 where object_id<=5000]');
 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('11.2.0.4')]',
 20  q'[DB_VERSION('11.2.0.4')]',
 21  q'[ALL_ROWS]',
 22  q'[OUTLINE_LEAF(@"SEL$1")]',
 23  q'[FULL(@"SEL$1" "T3"@"SEL$1")]',
 24  q'[END_OUTLINE_DATA]');
 25  :signature := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt);
 26  :signaturef := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt, TRUE);
 27  DBMS_SQLTUNE.IMPORT_SQL_PROFILE (
 28  sql_text    => sql_txt,
 29  profile     => h,
 30  name        => 'coe_0gmwrzua0usax_463314188',
 31  description => 'coe 0gmwrzua0usax 463314188 '||:signature||' '||:signaturef||'',
 32  category    => 'DEFAULT',
 33  validate    => TRUE,
 34  replace     => TRUE,
 35  force_match => FALSE /* TRUE:FORCE (match even when different literals in SQL). FALSE:EXACT (similar to CURSOR_SHARING) */ );
 36  DBMS_LOB.FREETEMPORARY(sql_txt);
 37  END;
 38  /

PL/SQL procedure successfully completed.

SQL>WHENEVER SQLERROR CONTINUE
SQL>SET ECHO OFF;

            SIGNATURE
---------------------
  3834930897575772753


           SIGNATUREF
---------------------
 15295415828971277362


... manual custom SQL Profile has been created

COE_XFR_SQL_PROFILE_0gmwrzua0usax_463314188 completed

#測試執行計劃是不是用到profile,並且走全表掃描

SQL> select count(*) from t3 where object_id<=5000;

Execution Plan
----------------------------------------------------------
Plan hash value: 463314188

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    13 |   277   (1)| 00:00:04 |
|   1 |  SORT AGGREGATE    |      |     1 |    13 |            |          |
|*  2 |   TABLE ACCESS FULL| T3   |  4129 | 53677 |   277   (1)| 00:00:04 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("OBJECT_ID"<=5000)

Note
-----
   - SQL profile "coe_0gmwrzua0usax_463314188" used for this statement


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27067062/viewspace-2075505/,如需轉載,請註明出處,否則將追究法律責任。

相關文章