【DB笔试面试608】在Oracle中,如何使用STA来生成SQL Profile?
♣题目部分
在Oracle中,如何使用STA来生成SQL Profile?
♣答案部分
利用STA对语句进行优化后,STA会对语句进行分析,采用最优的优化策略,并给出优化后的查询计划。可以按照STA给出的建议重写语句。但是,有些情况下,你可能无法重写语句(比如在生产环境中,SQL语句又在一个包中)。这个时候就可以利用Sql Profile,将优化策略存储在Profile中,Oracle在构建这条语句的查询计划时,就不会使用已有相关统计数据,而使用Profile的策略,生成新的查询计划。
第一步:给用户赋权限
1[ZHLHRSPMDB2:oracle]:/oracle>sqlplus / as sysdba 2 3SQL*Plus: Release 11.2.0.4.0 Production on Wed May 25 16:47:29 2016 4 5Copyright (c) 1982, 2013, Oracle. All rights reserved. 6 7 8Connected to: 9Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production 10With the Partitioning, Real Application Clusters, OLAP, Data Mining 11and Real Application Testing options 12 13SYS@dlhr> 14SYS@dlhr> 15SYS@dlhr> 16SYS@dlhr> GRANT CREATE ANY SQL PROFILE TO LHR; 17 18Grant succeeded. 19 20SYS@dlhr> GRANT DROP ANY SQL PROFILE TO LHR; 21 22Grant succeeded. 23 24SYS@dlhr> GRANT ALTER ANY SQL PROFILE TO LHR; 25 26Grant succeeded. 27 28SYS@dlhr> conn lhr/lhr 29Connected. 30LHR@dlhr> 31LHR@dlhr> select * from v$version; 32 33BANNER 34-------------------------------------------------------------------------------- 35Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production 36PL/SQL Release 11.2.0.4.0 - Production 37CORE 11.2.0.4.0 Production 38TNS for IBM/AIX RISC System/6000: Version 11.2.0.4.0 - Production 39NLSRTL Version 11.2.0.4.0 - Production 40 41LHR@dlhr> create table lhr.TB_LHR_20160525_01 as select * from dba_objects; 42 43Table created. 44 45LHR@dlhr> create index lhr.TB_LHR_20160525_01_idx on TB_LHR_20160525_01(object_id); 46 47Index created. 48 49LHR@dlhr> exec dbms_stats.gather_table_stats('lhr','TB_LHR_20160525_01',cascade=>true,degree=>4); 50 51PL/SQL procedure successfully completed. 52 53LHR@dlhr> set autot on 54LHR@dlhr> select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_01 where object_id = 100 ; 55 56 COUNT(*) 57---------- 58 1 59 60 61Execution Plan 62---------------------------------------------------------- 63Plan hash value: 3612989399 64 65----------------------------------------------------------------------------------------- 66| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 67----------------------------------------------------------------------------------------- 68| 0 | SELECT STATEMENT | | 1 | 5 | 351 (2)| 00:00:05 | 69| 1 | SORT AGGREGATE | | 1 | 5 | | | 70|* 2 | TABLE ACCESS FULL| TB_LHR_20160525_01 | 1 | 5 | 351 (2)| 00:00:05 | 71----------------------------------------------------------------------------------------- 72 73Predicate Information (identified by operation id): 74--------------------------------------------------- 75 76 2 - filter("OBJECT_ID"=100) 77 78 79Statistics 80---------------------------------------------------------- 81 1 recursive calls 82 0 db block gets 83 1249 consistent gets 84 0 physical reads 85 0 redo size 86 526 bytes sent via SQL*Net to client 87 520 bytes received via SQL*Net from client 88 2 SQL*Net roundtrips to/from client 89 0 sorts (memory) 90 0 sorts (disk) 91 1 rows processed 92LHR@dlhr> set autot off 93LHR@dlhr> SELECT v.SQL_ID, v.SQL_TEXT FROM v$sql v WHERE v.SQL_TEXT like '%no_index(TB_LHR_20160525_01%' and v.SQL_TEXT not like '%v$sql%' ; 94 95SQL_ID 96------------- 97SQL_TEXT 98------------------------------------------------------------------------------------------------------------------------------------ 997jt1btjkcczb8100select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_01 where object_id = 1001011027suktf0w95cry103EXPLAIN PLAN SET STATEMENT_ID='PLUS150249' FOR select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_L104HR_20160525_01 where object_id = 10023SQL*Plus: Release 11.2.0.4.0 Production on Wed May 25 16:47:29 201645Copyright (c) 1982, 2013, Oracle. All rights reserved.678Connected to:9Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production10With the Partitioning, Real Application Clusters, OLAP, Data Mining11and Real Application Testing options1213SYS@dlhr> 14SYS@dlhr> 15SYS@dlhr> 16SYS@dlhr> GRANT CREATE ANY SQL PROFILE TO LHR;1718Grant succeeded.1920SYS@dlhr> GRANT DROP ANY SQL PROFILE TO LHR;2122Grant succeeded.2324SYS@dlhr> GRANT ALTER ANY SQL PROFILE TO LHR;2526Grant succeeded.2728SYS@dlhr> conn lhr/lhr29Connected.30LHR@dlhr> 31LHR@dlhr> select * from v$version;3233BANNER34--------------------------------------------------------------------------------35Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production36PL/SQL Release 11.2.0.4.0 - Production37CORE 11.2.0.4.0 Production38TNS for IBM/AIX RISC System/6000: Version 11.2.0.4.0 - Production39NLSRTL Version 11.2.0.4.0 - Production4041LHR@dlhr> create table lhr.TB_LHR_20160525_01 as select * from dba_objects; 4243Table created.4445LHR@dlhr> create index lhr.TB_LHR_20160525_01_idx on TB_LHR_20160525_01(object_id); 4647Index created.4849LHR@dlhr> exec dbms_stats.gather_table_stats('lhr','TB_LHR_20160525_01',cascade=>true,degree=>4); 5051PL/SQL procedure successfully completed.5253LHR@dlhr> set autot on54LHR@dlhr> select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_01 where object_id = 100 ;5556 COUNT(*)57----------58 1596061Execution Plan62----------------------------------------------------------63Plan hash value: 36129893996465-----------------------------------------------------------------------------------------66| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |67-----------------------------------------------------------------------------------------68| 0 | SELECT STATEMENT | | 1 | 5 | 351 (2)| 00:00:05 |69| 1 | SORT AGGREGATE | | 1 | 5 | | |70|* 2 | TABLE ACCESS FULL| TB_LHR_20160525_01 | 1 | 5 | 351 (2)| 00:00:05 |71-----------------------------------------------------------------------------------------7273Predicate Information (identified by operation id):74---------------------------------------------------7576 2 - filter("OBJECT_ID"=100)777879Statistics80----------------------------------------------------------81 1 recursive calls82 0 db block gets83 1249 consistent gets84 0 physical reads85 0 redo size86 526 bytes sent via SQL*Net to client87 520 bytes received via SQL*Net from client88 2 SQL*Net roundtrips to/from client89 0 sorts (memory)90 0 sorts (disk)91 1 rows processed92LHR@dlhr> set autot off93LHR@dlhr> SELECT v.SQL_ID, v.SQL_TEXT FROM v$sql v WHERE v.SQL_TEXT like '%no_index(TB_LHR_20160525_01%' and v.SQL_TEXT not like '%v$sql%' ;9495SQL_ID96-------------97SQL_TEXT98------------------------------------------------------------------------------------------------------------------------------------997jt1btjkcczb8100select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_01 where object_id = 1001011027suktf0w95cry103EXPLAIN PLAN SET STATEMENT_ID='PLUS150249' FOR select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_L104HR_20160525_01 where object_id = 100
第二步:创建、执行优化任务
1LHR@dlhr> DECLARE 2 2 my_task_name VARCHAR2(30); 3 3 my_sqltext CLOB; 4 4 BEGIN 5 5 my_sqltext := 'select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_01 where object_id = 100'; 6 6 my_task_name := DBMS_SQLTUNE.CREATE_TUNING_TASK( 7 7 sql_text => my_sqltext, 8 8 user_name => 'LHR', 9 9 scope => 'COMPREHENSIVE',10 10 time_limit => 60,11 11 task_name => 'sql_profile_test',12 12 description => 'Task to tune a query on a specified table');13 13 DBMS_SQLTUNE.EXECUTE_TUNING_TASK( task_name => 'sql_profile_test');14 14 END;15 15 /1617PL/SQL procedure successfully completed.DECLARE2 2 my_task_name VARCHAR2(30);3 3 my_sqltext CLOB;4 4 BEGIN5 5 my_sqltext := 'select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_01 where object_id = 100';6 6 my_task_name := DBMS_SQLTUNE.CREATE_TUNING_TASK(7 7 sql_text => my_sqltext,8 8 user_name => 'LHR',9 9 scope => 'COMPREHENSIVE',10 10 time_limit => 60,11 11 task_name => 'sql_profile_test',12 12 description => 'Task to tune a query on a specified table');13 13 DBMS_SQLTUNE.EXECUTE_TUNING_TASK( task_name => 'sql_profile_test');14 14 END;15 15 /1617PL/SQL procedure successfully completed.
或者也可以使用sqlid来生成优化任务,如下:
1LHR@dlhr> DECLARE 2 2 a_tuning_task VARCHAR2(30); 3 3 BEGIN 4 4 a_tuning_task := dbms_sqltune.create_tuning_task(sql_id => '7jt1btjkcczb8', 5 5 task_name => 'sql_profile_test_SQLID'); 6 6 dbms_sqltune.execute_tuning_task(a_tuning_task); 7 7 END; 8 8 / 910PL/SQL procedure successfully completed.DECLARE2 2 a_tuning_task VARCHAR2(30);3 3 BEGIN4 4 a_tuning_task := dbms_sqltune.create_tuning_task(sql_id => '7jt1btjkcczb8',5 5 task_name => 'sql_profile_test_SQLID');6 6 dbms_sqltune.execute_tuning_task(a_tuning_task);7 7 END;8 8 /910PL/SQL procedure successfully completed.
第三步:查看优化建议
1LHR@dlhr> set autot off 2LHR@dlhr> set long 10000 3LHR@dlhr> set longchunksize 1000 4LHR@dlhr> set linesize 100 5LHR@dlhr> SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK( 'sql_profile_test') from DUAL; 6 7DBMS_SQLTUNE.REPORT_TUNING_TASK('SQL_PROFILE_TEST') 8---------------------------------------------------------------------------------------------------- 9GENERAL INFORMATION SECTION 10------------------------------------------------------------------------------- 11Tuning Task Name : sql_profile_test 12Tuning Task Owner : LHR 13Workload Type : Single SQL Statement 14Scope : COMPREHENSIVE 15Time Limit(seconds): 60 16Completion Status : COMPLETED 17Started at : 05/25/2016 16:58:31 18Completed at : 05/25/2016 16:58:32 19 20------------------------------------------------------------------------------- 21Schema Name: LHR 22SQL ID : 9kzm8scz6t92z 23SQL Text : select /*+no_index(TB_LHR_20160525_01 24 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_01 25 where object_id = 100 26 27------------------------------------------------------------------------------- 28FINDINGS SECTION (1 finding) 29------------------------------------------------------------------------------- 30 311- SQL Profile Finding (see explain plans section below) 32-------------------------------------------------------- 33 A potentially better execution plan was found for this statement. 34 35 Recommendation (estimated benefit: 99.83%) 36 ------------------------------------------ 37 - Consider accepting the recommended SQL profile. 38 execute dbms_sqltune.accept_sql_profile(task_name => 'sql_profile_test', 39 task_owner => 'LHR', replace => TRUE); 40 41 Validation results 42 ------------------ 43 The SQL profile was tested by executing both its plan and the original plan 44 and measuring their respective execution statistics. A plan may have been 45 only partially executed if the other could be run to completion in less time. 46 47 Original Plan With SQL Profile % Improved 48 ------------- ---------------- ---------- 49 Completion Status: COMPLETE COMPLETE 50 Elapsed Time (s): .006278 .00004 99.36 % 51 CPU Time (s): .003397 .000021 99.38 % 52 User I/O Time (s): 0 0 53 Buffer Gets: 1249 2 99.83 % 54 Physical Read Requests: 0 0 55 Physical Write Requests: 0 0 56 57DBMS_SQLTUNE.REPORT_TUNING_TASK('SQL_PROFILE_TEST') 58---------------------------------------------------------------------------------------------------- 59 Physical Read Bytes: 0 0 60 Physical Write Bytes: 0 0 61 Rows Processed: 1 1 62 Fetches: 1 1 63 Executions: 1 1 64 65 Notes 66 ----- 67 1. Statistics for the original plan were averaged over 10 executions. 68 2. Statistics for the SQL profile plan were averaged over 10 executions. 69 70------------------------------------------------------------------------------- 71EXPLAIN PLANS SECTION 72------------------------------------------------------------------------------- 73 741- Original With Adjusted Cost 75------------------------------ 76Plan hash value: 3612989399 77 78----------------------------------------------------------------------------------------- 79| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 80----------------------------------------------------------------------------------------- 81| 0 | SELECT STATEMENT | | 1 | 5 | 351 (2)| 00:00:05 | 82| 1 | SORT AGGREGATE | | 1 | 5 | | | 83|* 2 | TABLE ACCESS FULL| TB_LHR_20160525_01 | 1 | 5 | 351 (2)| 00:00:05 | 84----------------------------------------------------------------------------------------- 85 86Predicate Information (identified by operation id): 87--------------------------------------------------- 88 89 2 - filter("OBJECT_ID"=100) 90 912- Using SQL Profile 92-------------------- 93Plan hash value: 661515879 94 95-------------------------------------------------------------------------------------------- 96| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 97-------------------------------------------------------------------------------------------- 98| 0 | SELECT STATEMENT | | 1 | 5 | 1 (0)| 00:00:01 | 99| 1 | SORT AGGREGATE | | 1 | 5 | | |100|* 2 | INDEX RANGE SCAN| TB_LHR_20160525_01_IDX | 1 | 5 | 1 (0)| 00:00:01 |101--------------------------------------------------------------------------------------------102103Predicate Information (identified by operation id):104---------------------------------------------------105106107DBMS_SQLTUNE.REPORT_TUNING_TASK('SQL_PROFILE_TEST')108----------------------------------------------------------------------------------------------------109 2 - access("OBJECT_ID"=100)110111-------------------------------------------------------------------------------set autot off2LHR@dlhr> set long 100003LHR@dlhr> set longchunksize 10004LHR@dlhr> set linesize 1005LHR@dlhr> SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK( 'sql_profile_test') from DUAL;67DBMS_SQLTUNE.REPORT_TUNING_TASK('SQL_PROFILE_TEST')8----------------------------------------------------------------------------------------------------9GENERAL INFORMATION SECTION10-------------------------------------------------------------------------------11Tuning Task Name : sql_profile_test12Tuning Task Owner : LHR13Workload Type : Single SQL Statement14Scope : COMPREHENSIVE15Time Limit(seconds): 6016Completion Status : COMPLETED17Started at : 05/25/2016 16:58:3118Completed at : 05/25/2016 16:58:321920-------------------------------------------------------------------------------21Schema Name: LHR22SQL ID : 9kzm8scz6t92z23SQL Text : select /*+no_index(TB_LHR_20160525_0124 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_0125 where object_id = 1002627-------------------------------------------------------------------------------28FINDINGS SECTION (1 finding)29-------------------------------------------------------------------------------30311- SQL Profile Finding (see explain plans section below)32--------------------------------------------------------33 A potentially better execution plan was found for this statement.3435 Recommendation (estimated benefit: 99.83%)36 ------------------------------------------37 - Consider accepting the recommended SQL profile.38 execute dbms_sqltune.accept_sql_profile(task_name => 'sql_profile_test',39 task_owner => 'LHR', replace => TRUE);4041 Validation results42 ------------------43 The SQL profile was tested by executing both its plan and the original plan44 and measuring their respective execution statistics. A plan may have been45 only partially executed if the other could be run to completion in less time.4647 Original Plan With SQL Profile % Improved48 ------------- ---------------- ----------49 Completion Status: COMPLETE COMPLETE50 Elapsed Time (s): .006278 .00004 99.36 %51 CPU Time (s): .003397 .000021 99.38 %52 User I/O Time (s): 0 053 Buffer Gets: 1249 2 99.83 %54 Physical Read Requests: 0 055 Physical Write Requests: 0 05657DBMS_SQLTUNE.REPORT_TUNING_TASK('SQL_PROFILE_TEST')58----------------------------------------------------------------------------------------------------59 Physical Read Bytes: 0 060 Physical Write Bytes: 0 061 Rows Processed: 1 162 Fetches: 1 163 Executions: 1 16465 Notes66 -----67 1. Statistics for the original plan were averaged over 10 executions.68 2. Statistics for the SQL profile plan were averaged over 10 executions.6970-------------------------------------------------------------------------------71EXPLAIN PLANS SECTION72-------------------------------------------------------------------------------73741- Original With Adjusted Cost75------------------------------76Plan hash value: 36129893997778-----------------------------------------------------------------------------------------79| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |80-----------------------------------------------------------------------------------------81| 0 | SELECT STATEMENT | | 1 | 5 | 351 (2)| 00:00:05 |82| 1 | SORT AGGREGATE | | 1 | 5 | | |83|* 2 | TABLE ACCESS FULL| TB_LHR_20160525_01 | 1 | 5 | 351 (2)| 00:00:05 |84-----------------------------------------------------------------------------------------8586Predicate Information (identified by operation id):87---------------------------------------------------8889 2 - filter("OBJECT_ID"=100)90912- Using SQL Profile92--------------------93Plan hash value: 6615158799495--------------------------------------------------------------------------------------------96| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |97--------------------------------------------------------------------------------------------98| 0 | SELECT STATEMENT | | 1 | 5 | 1 (0)| 00:00:01 |99| 1 | SORT AGGREGATE | | 1 | 5 | | |100|* 2 | INDEX RANGE SCAN| TB_LHR_20160525_01_IDX | 1 | 5 | 1 (0)| 00:00:01 |101--------------------------------------------------------------------------------------------102103Predicate Information (identified by operation id):104---------------------------------------------------105106107DBMS_SQLTUNE.REPORT_TUNING_TASK('SQL_PROFILE_TEST')108----------------------------------------------------------------------------------------------------109 2 - access("OBJECT_ID"=100)110111-------------------------------------------------------------------------------
这里可以看到,在优化建议中给出了新的查询计划。现在,我们决定接受这个建议,并且不重写语句。
第四步:接受profile
1LHR@dlhr> set autot on 2LHR@dlhr> select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_01 where object_id = 100 ; 3 4 COUNT(*) 5---------- 6 1 7 8 9Execution Plan10----------------------------------------------------------1112Plan hash value: 36129893991314-----------------------------------------------------------------------------------------15| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |16-----------------------------------------------------------------------------------------17| 0 | SELECT STATEMENT | | 1 | 5 | 351 (2)| 00:00:05 |18| 1 | SORT AGGREGATE | | 1 | 5 | | |19|* 2 | TABLE ACCESS FULL| TB_LHR_20160525_01 | 1 | 5 | 351 (2)| 00:00:05 |20-----------------------------------------------------------------------------------------2122Predicate Information (identified by operation id):23---------------------------------------------------2425 2 - filter("OBJECT_ID"=100)262728Statistics29----------------------------------------------------------30 0 recursive calls31 0 db block gets32 1249 consistent gets33 0 physical reads34 0 redo size35 526 bytes sent via SQL*Net to client36 520 bytes received via SQL*Net from client37 2 SQL*Net roundtrips to/from client38 0 sorts (memory)39 0 sorts (disk)40 1 rows processed41LHR@dlhr> execute dbms_sqltune.accept_sql_profile(task_name =>'sql_profile_test_SQLID', task_owner => 'LHR', replace => TRUE);4243PL/SQL procedure successfully completed.4445LHR@dlhr> set autot off46LHR@dlhr> SELECT e.task_name, b.name, d.sql_text, extractvalue(value(h), '.') as hints47 2 FROM dba_sql_profiles d,48 3 dba_advisor_tasks e,49 4 SYS.SQLOBJ$DATA A,50 5 SYS.SQLOBJ$ B,51 6 TABLE(XMLSEQUENCE(EXTRACT(XMLTYPE(A.COMP_DATA),52 7 '/outline_data/hint'))) h53 8 where a.signature = b.signature54 9 and a.category = b.category55 10 and a.obj_type = b.obj_type56 11 and a.plan_id = b.plan_id57 12 and a.signature = d.signature 58 13 and d.task_id=e.task_id59 14 and d.name = 'SYS_SQLPROF_0154e728ad3f0000'60 15 ;6162TASK_NAME NAME63------------------------------ ------------------------------64SQL_TEXT65----------------------------------------------------------------------------------------------------66HINTS67----------------------------------------------------------------------------------------------------68sql_profile_test SYS_SQLPROF_0154e728ad3f000069select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_0170where object_id = 10071OPTIMIZER_FEATURES_ENABLE(default)7273sql_profile_test SYS_SQLPROF_0154e728ad3f000074select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_0175where object_id = 10076IGNORE_OPTIM_EMBEDDED_HINTSset autot on2LHR@dlhr> select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_01 where object_id = 100 ;34 COUNT(*)5----------6 1789Execution Plan10----------------------------------------------------------1112Plan hash value: 36129893991314-----------------------------------------------------------------------------------------15| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |16-----------------------------------------------------------------------------------------17| 0 | SELECT STATEMENT | | 1 | 5 | 351 (2)| 00:00:05 |18| 1 | SORT AGGREGATE | | 1 | 5 | | |19|* 2 | TABLE ACCESS FULL| TB_LHR_20160525_01 | 1 | 5 | 351 (2)| 00:00:05 |20-----------------------------------------------------------------------------------------2122Predicate Information (identified by operation id):23---------------------------------------------------2425 2 - filter("OBJECT_ID"=100)262728Statistics29----------------------------------------------------------30 0 recursive calls31 0 db block gets32 1249 consistent gets33 0 physical reads34 0 redo size35 526 bytes sent via SQL*Net to client36 520 bytes received via SQL*Net from client37 2 SQL*Net roundtrips to/from client38 0 sorts (memory)39 0 sorts (disk)40 1 rows processed41LHR@dlhr> execute dbms_sqltune.accept_sql_profile(task_name =>'sql_profile_test_SQLID', task_owner => 'LHR', replace => TRUE);4243PL/SQL procedure successfully completed.4445LHR@dlhr> set autot off46LHR@dlhr> SELECT e.task_name, b.name, d.sql_text, extractvalue(value(h), '.') as hints47 2 FROM dba_sql_profiles d,48 3 dba_advisor_tasks e,49 4 SYS.SQLOBJ$DATA A,50 5 SYS.SQLOBJ$ B,51 6 TABLE(XMLSEQUENCE(EXTRACT(XMLTYPE(A.COMP_DATA),52 7 '/outline_data/hint'))) h53 8 where a.signature = b.signature54 9 and a.category = b.category55 10 and a.obj_type = b.obj_type56 11 and a.plan_id = b.plan_id57 12 and a.signature = d.signature 58 13 and d.task_id=e.task_id59 14 and d.name = 'SYS_SQLPROF_0154e728ad3f0000'60 15 ;6162TASK_NAME NAME63------------------------------ ------------------------------64SQL_TEXT65----------------------------------------------------------------------------------------------------66HINTS67----------------------------------------------------------------------------------------------------68sql_profile_test SYS_SQLPROF_0154e728ad3f000069select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_0170where object_id = 10071OPTIMIZER_FEATURES_ENABLE(default)7273sql_profile_test SYS_SQLPROF_0154e728ad3f000074select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_0175where object_id = 10076IGNORE_OPTIM_EMBEDDED_HINTS
在这里用了包DBMS_SQLTUNE的另一个函数:ACCEPT_SQL_PROFILE。其中,参数task_name即我们创建的优化建议任务的名称,name是profile的名字,可以是任意合法名称。此外这个函数还有其他一些函数,下面是这个函数的原型:
1DBMS_SQLTUNE.ACCEPT_SQL_PROFILE ( 2 task_name IN VARCHAR2, 3 object_id IN NUMBER := NULL, 4 name IN VARCHAR2 := NULL, 5 description IN VARCHAR2 := NULL, 6 category IN VARCHAR2 := NULL; 7 task_owner IN VARCHAR2 := NULL, 8 replace IN BOOLEAN := FALSE, 9 force_match IN BOOLEAN := FALSE)10 RETURN VARCHAR2;2 task_name IN VARCHAR2,3 object_id IN NUMBER := NULL,4 name IN VARCHAR2 := NULL,5 description IN VARCHAR2 := NULL,6 category IN VARCHAR2 := NULL;7 task_owner IN VARCHAR2 := NULL,8 replace IN BOOLEAN := FALSE,9 force_match IN BOOLEAN := FALSE)10 RETURN VARCHAR2;
Description是profile的描述信息;task_owner是优化建议任务的所有者;replace为TRUE时,如果这个profile已经存在,就代替它;force_match为TURE时,表示与语句强制匹配,即强制使用绑定变量,和系统参数cursor_sharing设置为FORCE时类似,为FALSE时,与cursor_sharing设置为EXACT时类似,即完全匹配。
这里要特别提到的是category这个参数,你可以通过设置这个参数,制定特定会话使用这个profile。在10g中,每个会话都有一个新参数SQLTUNE_CATEGORY,他的默认值是DEFAULT。而我们在调用这个函数时,如果没有指定这个参数,那它的值也是DEFAULT,而如果我们给这个profile指定了一个其它的CATEGORY值,如FOR_TUNING,那么只有会话参SQLTUNE_CATEGORY也为FOR_TUNING时,才会使用这个porfile。为什么说这个参数很有用呢?试想一个这样的环境:你在一个生产系统上利用STA调优一条语句,STA已经给出了优化建议,但是你又不敢贸然实施它给出的建议(毕竟它只是机器嘛,不能完全信任),你就可以创建一个有特殊CATEGORY的profile,然后在你自己的会话中制定SQLTUNE_CATEGORY为这个特殊的CATEGORY,那就既可以看优化建议的实际效果又不影响生产环境。
此外可以通过视图DBA_SQL_PROFILES来查看已经创建的profile。
第五步:查看profile的效果
1LHR@dlhr> set autot on 2LHR@dlhr> select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_01 where object_id = 100 ; 3 4 COUNT(*) 5---------- 6 1 7 8 9Execution Plan10----------------------------------------------------------11Plan hash value: 6615158791213--------------------------------------------------------------------------------------------14| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |15--------------------------------------------------------------------------------------------16| 0 | SELECT STATEMENT | | 1 | 5 | 1 (0)| 00:00:01 |17| 1 | SORT AGGREGATE | | 1 | 5 | | |18|* 2 | INDEX RANGE SCAN| TB_LHR_20160525_01_IDX | 1 | 5 | 1 (0)| 00:00:01 |19--------------------------------------------------------------------------------------------2021Predicate Information (identified by operation id):22---------------------------------------------------2324 2 - access("OBJECT_ID"=100)2526Note27-----28 - SQL profile "SYS_SQLPROF_0154e728ad3f0000" used for this statement293031Statistics32----------------------------------------------------------33 1 recursive calls34 0 db block gets35 2 consistent gets36 0 physical reads37 0 redo size38 526 bytes sent via SQL*Net to client39 520 bytes received via SQL*Net from client40 2 SQL*Net roundtrips to/from client41 0 sorts (memory)42 0 sorts (disk)43 1 rows processedset autot on2LHR@dlhr> select /*+no_index(TB_LHR_20160525_01 TB_LHR_20160525_01_idx)*/count(*) from lhr.TB_LHR_20160525_01 where object_id = 100 ;34 COUNT(*)5----------6 1789Execution Plan10----------------------------------------------------------11Plan hash value: 6615158791213--------------------------------------------------------------------------------------------14| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |15--------------------------------------------------------------------------------------------16| 0 | SELECT STATEMENT | | 1 | 5 | 1 (0)| 00:00:01 |17| 1 | SORT AGGREGATE | | 1 | 5 | | |18|* 2 | INDEX RANGE SCAN| TB_LHR_20160525_01_IDX | 1 | 5 | 1 (0)| 00:00:01 |19--------------------------------------------------------------------------------------------2021Predicate Information (identified by operation id):22---------------------------------------------------2324 2 - access("OBJECT_ID"=100)2526Note27-----28 - SQL profile "SYS_SQLPROF_0154e728ad3f0000" used for this statement293031Statistics32----------------------------------------------------------33 1 recursive calls34 0 db block gets35 2 consistent gets36 0 physical reads37 0 redo size38 526 bytes sent via SQL*Net to client39 520 bytes received via SQL*Net from client40 2 SQL*Net roundtrips to/from client41 0 sorts (memory)42 0 sorts (disk)43 1 rows processed
从NOTE部分可以看到,语句采用了profile中的数据,创建了新的查询计划。并且在查询计划中还有一些附加信息,表明这个语句是采用了“SYS_SQLPROF_0154e728ad3f0000”这个Profile,而不是根据对象上面的统计数据来生成的查询计划。
但上述方法主要是依赖sql tuning advisor,如果它无法生成你想要的执行计划.你还可以通过手动的方式,通过sql profile把hint加进去.复杂的SQL的hint可以采用脚本coe_xfr_sql_profile.sql来产生原语句的outline data和加hint语句的outline data,然后替换对应的SYS.SQLPROF_ATTR,最后执行生成的sql就可以了。
使用PLSQL DEVELOPER 11查看执行计划,如下图,新版本的好处:
本文选自《Oracle程序员面试笔试宝典》,作者:李华荣。
---------------优质麦课------------
详细内容可以添加麦老师微信或QQ私聊。
About Me:小麦苗
●本文作者:小麦苗,只专注于数据库的技术,更注重技术的运用
● 作者博客地址:http://blog.itpub.net/26736162/abstract/1/
●本系列题目来源于作者的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解
●版权所有,欢迎分享本文,转载请保留出处
●QQ:646634621 QQ群:618766405
●提供OCP、OCM和高可用部分最实用的技能培训
●题目解答若有不当之处,还望各位朋友批评指正,共同进步
DBA宝典
长按下图识别二维码或微信扫描下图二维码来关注小麦苗的微信公众号:xiaomaimiaolhr,学习最实用的数据库技术。
喜欢就点击“好看”吧
【DB笔试面试608】在Oracle中,如何使用STA来生成SQL Profile?相关推荐
- 【DB笔试面试164】在Oracle中,如何彻底停止expdp数据泵进程?
[DB笔试面试164]在Oracle中,如何彻底停止expdp数据泵进程? 真题1. 如何彻底停止 expdp 进程? 答案:许多同事在使用expdp命令时,不小心按了CTRL+C组合键,然后又输入e ...
- 【DB笔试面试605】在Oracle中,SQL概要(SQL Profile)的作用是什么?
♣题目 部分 在Oracle中,SQL概要(SQL Profile)的作用是什么? ♣答案部分 SQL Profile就是为某条SQL语句提供除了系统统计信息.对象(表和索引等)统计信息之外的其它信息 ...
- 【DB笔试面试594】在Oracle中,关键字NOLOGGING、APPEND和PARALLEL提高DML性能方面有什么差别?...
♣题目部分 在Oracle中,关键字NOLOGGING.APPEND和PARALLEL提高DML性能方面有什么差别? ♣答案部分 众所周知,表模式(LOGGING/NOLOGGING).插入模式(AP ...
- 【DB笔试面试622】在Oracle中,说说COUNT(*)计算行数有哪些优化手段?
♣ 题目部分 在Oracle中,说说COUNT(*)计算行数有哪些优化手段? ♣ 答案部分 手段 命令 执行计划 主要原理 详细说明 性能情况 全表扫描 TABLE ACCESS FULL 全表扫描 ...
- 【DB笔试面试606】在Oracle中,coe_xfr_sql_profile.sql脚本的作用是什么?
♣题目部分 在Oracle中,coe_xfr_sql_profile.sql脚本的作用是什么? ♣答案部分 使用coe_xfr_sql_profile.sql脚本生成sqlprof_attr数据 最麻 ...
- 【DB笔试面试597】在Oracle中,获取执行计划有哪几种方法?
♣题目部分 在Oracle中,获取执行计划有哪几种方法? ♣答案部分 一般来说,有如下几种获取执行计划的方式: 1.AUTOTRACE方式 AUTOTRACE是Oracle自带的客户端工具SQL*Pl ...
- 【DB笔试面试713】在Oracle中,如何将一个数据库添加到CRS中?
♣ 题目部分 在Oracle中,如何将一个数据库添加到CRS中? ♣ 答案部分 虽然通过DBCA(DataBase Configuration Assistant,数据库配置助手)创建的数据库会自动加 ...
- 【DB笔试面试621】在Oracle中,举例说明“DISTINCT配置(Distinct Placement,DP)”查询转换。...
♣ 题目部分 在Oracle中,举例说明"DISTINCT配置(Distinct Placement,DP)"查询转换. ♣ 答案部分 1LHR@orclasm > SELE ...
- 【DB笔试面试607】在Oracle中,coe_load_sql_profile.sql脚本的作用是什么?
♣题目 部分 在Oracle中,coe_load_sql_profile.sql脚本的作用是什么? ♣答案部分 可以使用coe_load_sql_profile.sql脚本直接固定执行计划,该脚本也可 ...
最新文章
- VS2017 配置 OpenGL 环境
- 使用Hyper-V安装Ubuntu16.04 Server 网络配置
- 微信自动回复和自动抢红包实现原理(一):AccessibilityService的介绍和配置
- [Everyday Mathematics]20150103
- Android实例-手机安全卫士(十一)-自定义对话框点击事件处理
- 三种地理参考信息模型:WMS,WFS,WCS(转)
- Shell脚本编程30分钟入门
- 求1!+2!+3!+....20!的值
- 从3年前接触区块链,到开发出装机量最大客户端Geth,看看人家的职业发展之路 | 人物志...
- 单按钮启停电路实物图_手绘220V清洗机电路原理图和接线方法,单相电机常见故障排查...
- resizableImageWithCapInsets:方法的探析 (转载笔记)
- 8. 	memached安全性
- matlab遗传算法选址(多约束条件)
- 程序媛秋招心得及面试经验分享
- Mysql基础篇(10)—— MySQL8.0新特性概览
- 英语计算机手抄报图片大全,五年级英语手抄报图片大全
- mac修改终端显示前缀
- 用于传感器互操作性问题的指纹匹配系统的大规模研究
- warning/error
- 算法学习--排序算法--插入排序
热门文章
- 人工智能如何改变了我们的日常生活?
- 在Windows 7 Media Center中创建幻灯片放映
- ryuyan 方差分析_如何使用R语言做不同设计的方差分析(ANOVA)、简单效应检验、事后多重比较?...
- python实现四种基本图形的面积计算 :圆形,长方形,正方形,梯形。
- android仿美团评论
- php测试教程,PHP单元测试基础教程
- it guy流利说_Guy RoutledgeCSS预处理器和字体末端开发
- 戴尔 OptiPlex 3020重新安装win10系统的教程
- 【OBS】VS调试启动exe的环境设置
- 《金阁寺》金阁美之于幻想,我用摧毁它来成就其美