SQL Profile 實驗01

yepkeepmoving發表於2016-04-02
##建立測試表和資料
SQL> conn scott/tiger;
Connected.
SQL> create table t1 as select * from all_objects where rownum<=50000;
Table created.
SQL> create table t2 as select * from all_objects where rownum<=50000;
Table created.
##開啟autotrace 並執行SQL

SQL> set autotrace trace explain;
SQL>SELECT *
  FROM t2 a,  t1 b
  WHERE a.object_id = b.object_id AND a.owner = b.owner AND a.object_id = 100;

Execution Plan
----------------------------------------------------------
Plan hash value: 2959412835

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |    13 |  4108 |   401   (1)| 00:00:05 |
|*  1 |  HASH JOIN         |      |    13 |  4108 |   401   (1)| 00:00:05 |
|*  2 |   TABLE ACCESS FULL| T2   |     8 |  1264 |   200   (0)| 00:00:03 |
|*  3 |   TABLE ACCESS FULL| T1   |     8 |  1264 |   201   (1)| 00:00:03 |
---------------------------------------------------------------------------

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

   1 - access("A"."OBJECT_ID"="B"."OBJECT_ID" AND
              "A"."OWNER"="B"."OWNER")
   2 - filter("A"."OBJECT_ID"=100)
   3 - filter("B"."OBJECT_ID"=100)

Note
-----
   - dynamic sampling used for this statement (level=2)
##在T1的object_id列建立索引
SQL> create index t1_idx on t1(object_id);
Index created.
##再次執行查詢並對比執行計劃
SQL>SELECT *
  FROM t2 a,  t1 b
  WHERE a.object_id = b.object_id AND a.owner = b.owner AND a.object_id = 100;

Execution Plan
----------------------------------------------------------
Plan hash value: 2010545385

---------------------------------------------------------------------------------------
| Id  | Operation                    | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |        |     1 |   194 |   202   (0)| 00:00:03 |
|   1 |  NESTED LOOPS                |        |     1 |   194 |   202   (0)| 00:00:03 |
|   2 |   NESTED LOOPS               |        |     1 |   194 |   202   (0)| 00:00:03 |
|*  3 |    TABLE ACCESS FULL         | T2     |     1 |    97 |   200   (0)| 00:00:03 |
|*  4 |    INDEX RANGE SCAN          | T1_IDX |     1 |       |     1   (0)| 00:00:01 |
|*  5 |   TABLE ACCESS BY INDEX ROWID| T1     |     1 |    97 |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------------------

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

   3 - filter("A"."OBJECT_ID"=100)
   4 - access("B"."OBJECT_ID"=100)
   5 - filter("A"."OWNER"="B"."OWNER")

##查詢執行SQL對應的SQL_ID
SQL>select sql_id,sql_text from v$sql where sql_text like '%b.owner%' ;

bz1xauwq4cx7n
##利用sqlt的指令碼為SQL_ID建立profile,生成profile指令碼coe_xfr_sql_profile_bz1xauwq4cx7n_2010545385.sql

SQL> @coe_xfr_sql_profile.sql        ##需要使用sys賬戶

Parameter 1:
SQL_ID (required)

Enter value for 1: bz1xauwq4cx7n        ##輸入SQL_ID

PLAN_HASH_VALUE AVG_ET_SECS
--------------- -----------
     2010545385
Parameter 2:
PLAN_HASH_VALUE (required)

Enter value for 2: 2010545385            ##輸入plan_hash_value

Values passed to coe_xfr_sql_profile:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL_ID         : "bz1xauwq4cx7n"
PLAN_HASH_VALUE: "2010545385"

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


COE_XFR_SQL_PROFILE completed.
##匯入Profile檔案

SQL>@coe_xfr_sql_profile_bz1xauwq4cx7n_2010545385.sql

SQL>REM
SQL>REM $Header: 215187.1 coe_xfr_sql_profile_bz1xauwq4cx7n_2010545385.sql 11.4.4.4 2016/04/02 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_bz1xauwq4cx7n_2010545385.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 bz1xauwq4cx7n based on plan hash
SQL>REM   value 2010545385.
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_bz1xauwq4cx7n_2010545385.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_bz1xauwq4cx7n_2010545385');
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 *
 15    FROM t2 a,  t1 b
 16    WHERE a.object_id = b.object_id AN]');
 17  wa(q'[D a.owner = b.owner AND a.object_id = 100]');
 18  DBMS_LOB.CLOSE(sql_txt);
 19  h := SYS.SQLPROF_ATTR(
 20  q'[BEGIN_OUTLINE_DATA]',
 21  q'[IGNORE_OPTIM_EMBEDDED_HINTS]',
 22  q'[OPTIMIZER_FEATURES_ENABLE('11.2.0.4')]',
 23  q'[DB_VERSION('11.2.0.4')]',
 24  q'[ALL_ROWS]',
 25  q'[OUTLINE_LEAF(@"SEL$1")]',
 26  q'[FULL(@"SEL$1" "A"@"SEL$1")]',
 27  q'[INDEX(@"SEL$1" "B"@"SEL$1" ("T1"."OBJECT_ID"))]',
 28  q'[LEADING(@"SEL$1" "A"@"SEL$1" "B"@"SEL$1")]',
 29  q'[USE_NL(@"SEL$1" "B"@"SEL$1")]',
 30  q'[NLJ_BATCHING(@"SEL$1" "B"@"SEL$1")]',
 31  q'[END_OUTLINE_DATA]');
 32  :signature := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt);
 33  :signaturef := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt, TRUE);
 34  DBMS_SQLTUNE.IMPORT_SQL_PROFILE (
 35  sql_text    => sql_txt,
 36  profile     => h,
 37  name        => 'coe_bz1xauwq4cx7n_2010545385',
 38  description => 'coe bz1xauwq4cx7n 2010545385 '||:signature||' '||:signaturef||'',
 39  category    => 'DEFAULT',
 40  validate    => TRUE,
 41  replace     => TRUE,
 42  force_match => FALSE /* TRUE:FORCE (match even when different literals in SQL). FALSE:EXACT (similar to CURSOR_SHARING) */ );
 43  DBMS_LOB.FREETEMPORARY(sql_txt);
 44  END;
 45  /

PL/SQL procedure successfully completed.

SQL>WHENEVER SQLERROR CONTINUE
SQL>SET ECHO OFF;

            SIGNATURE
---------------------
 12804301822878364520


           SIGNATUREF
---------------------
 10164005570873817823


... manual custom SQL Profile has been created


COE_XFR_SQL_PROFILE_bz1xauwq4cx7n_2010545385 completed

##重新執行SQL,檢視SQL執行計劃是不是用到Profile
SQL>SELECT *
       FROM t2 a,  t1 b
       WHERE a.object_id = b.object_id AND a.owner = b.owner AND a.object_id = 100;

Execution Plan
----------------------------------------------------------
Plan hash value: 2010545385

---------------------------------------------------------------------------------------
| Id  | Operation                    | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |        |     1 |   194 |   202   (0)| 00:00:03 |
|   1 |  NESTED LOOPS                |        |     1 |   194 |   202   (0)| 00:00:03 |
|   2 |   NESTED LOOPS               |        |     1 |   194 |   202   (0)| 00:00:03 |
|*  3 |    TABLE ACCESS FULL         | T2     |     1 |    97 |   200   (0)| 00:00:03 |
|*  4 |    INDEX RANGE SCAN          | T1_IDX |     1 |       |     1   (0)| 00:00:01 |
|*  5 |   TABLE ACCESS BY INDEX ROWID| T1     |     1 |    97 |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------------------

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

   3 - filter("A"."OBJECT_ID"=100)
   4 - access("B"."OBJECT_ID"=100)
   5 - filter("A"."OWNER"="B"."OWNER")

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

##刪除Profile

begin
DBMS_SQLTUNE.DROP_SQL_PROFILE(name => 'coe_bz1xauwq4cx7n_2010545385');
end;
/



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

相關文章