sql profile使用
近日 ,每天晚上固定時間,資料庫會出現大量的latch:cache buffer chains 等待。經檢視,發現原SQL語句走錯執行計劃,240G的表,進行全表掃描,引起熱點塊爭用。通過固定SQL語句的執行計劃,使其可正常使用索引掃描,可以將該問題解決。下面為操作過程:
1. 檢視原來語句的執行計劃
SQL> set autotrace traceonly
SQL> SELECT NODEID
2 FROM ICDMIP.LHB_TEST T
WHERE --T.INANITIONID is null AND
3 4 PARTID = SUBSTR(201302130046087636, 5, 4)
5 ;
2 FROM ICDMIP.LHB_TEST T
WHERE --T.INANITIONID is null AND
3 4 PARTID = SUBSTR(201302130046087636, 5, 4)
5 ;
596762 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 514708567
----------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
----------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 412K| 10M| 3513 (1)| 00:00:43 | | |
| 1 | PARTITION LIST SINGLE| | 412K| 10M| 3513 (1)| 00:00:43 | 44 | 44 |
| 2 | TABLE ACCESS FULL | LHB_TEST | 412K| 10M| 3513 (1)| 00:00:43 | 44 | 44 | --這裡發現走的全表掃描,全表有240G,引起效能問題
----------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
----------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 412K| 10M| 3513 (1)| 00:00:43 | | |
| 1 | PARTITION LIST SINGLE| | 412K| 10M| 3513 (1)| 00:00:43 | 44 | 44 |
| 2 | TABLE ACCESS FULL | LHB_TEST | 412K| 10M| 3513 (1)| 00:00:43 | 44 | 44 | --這裡發現走的全表掃描,全表有240G,引起效能問題
----------------------------------------------------------------------------------------------------------------
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
54997 consistent gets --嚴重的邏輯讀,SQL語句效能低下
580 physical reads
0 redo size
20489108 bytes sent via SQL*Net to client
438116 bytes received via SQL*Net from client
39786 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
596762 rows processed
SQL> set autotrace off
SQL> alter session set current_schema=ICDMIP
2 ;
SQL> alter session set current_schema=ICDMIP
2 ;
Session altered.
2. 指定SQL使用選擇性比較好的索引
提前已檢視,條件PARTID可以使用索引PK_MIP_OPERATIONPROCESS.來試試,使用這個索引後,執行計劃怎麼樣。
SQL> explain plan for SELECT/*+ INDEX(T PK_MIP_OPERATIONPROCESS)*/ NODEID
2 FROM ICDMIP.LHB_TEST T
3 WHERE T.INANITIONID = :B2
4 AND PARTID = SUBSTR(:B1, 5, 4);
2 FROM ICDMIP.LHB_TEST T
3 WHERE T.INANITIONID = :B2
4 AND PARTID = SUBSTR(:B1, 5, 4);
Explained.
SQL> set linesize 500 pages 900
SQL> select * from table(dbms_xplan.display(null,null,'outline');
select * from table(dbms_xplan.display(null,null,'outline')
*
ERROR at line 1:
ORA-00907: missing right parenthesis
SQL> select * from table(dbms_xplan.display(null,null,'outline');
select * from table(dbms_xplan.display(null,null,'outline')
*
ERROR at line 1:
ORA-00907: missing right parenthesis
SQL> a )
1* select * from table(dbms_xplan.display(null,null,'outline'))
SQL> /
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1671553065
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1671553065
------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 44 | 4 (0)| 00:00:01 | | |
| 1 | PARTITION LIST SINGLE | | 1 | 44 | 4 (0)| 00:00:01 | KEY | KEY |
| 2 | TABLE ACCESS BY LOCAL INDEX ROWID| LHB_TEST | 1 | 44 | 4 (0)| 00:00:01 | KEY | KEY |
|* 3 | INDEX UNIQUE SCAN | PK_MIP_OPERATIONPROCESS | 1 | | 3 (0)| 00:00:01 | KEY | KEY | --SQL已使用相關索引
------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 44 | 4 (0)| 00:00:01 | | |
| 1 | PARTITION LIST SINGLE | | 1 | 44 | 4 (0)| 00:00:01 | KEY | KEY |
| 2 | TABLE ACCESS BY LOCAL INDEX ROWID| LHB_TEST | 1 | 44 | 4 (0)| 00:00:01 | KEY | KEY |
|* 3 | INDEX UNIQUE SCAN | PK_MIP_OPERATIONPROCESS | 1 | | 3 (0)| 00:00:01 | KEY | KEY | --SQL已使用相關索引
------------------------------------------------------------------------------------------------------------------------------
Outline Data
-------------
-------------
/*+
BEGIN_OUTLINE_DATA
INDEX_RS_ASC(@"SEL$1" "T"@"SEL$1" ("LHB_TEST"."INANITIONID" "LHB_TEST"."PARTID"))
OUTLINE_LEAF(@"SEL$1")
ALL_ROWS
OPT_PARAM('optimizer_dynamic_sampling' 1)
OPTIMIZER_FEATURES_ENABLE('10.2.0.3')
IGNORE_OPTIM_EMBEDDED_HINTS
END_OUTLINE_DATA
*/
BEGIN_OUTLINE_DATA
INDEX_RS_ASC(@"SEL$1" "T"@"SEL$1" ("LHB_TEST"."INANITIONID" "LHB_TEST"."PARTID"))
OUTLINE_LEAF(@"SEL$1")
ALL_ROWS
OPT_PARAM('optimizer_dynamic_sampling' 1)
OPTIMIZER_FEATURES_ENABLE('10.2.0.3')
IGNORE_OPTIM_EMBEDDED_HINTS
END_OUTLINE_DATA
*/
Predicate Information (identified by operation id):
---------------------------------------------------
---------------------------------------------------
3 - access("T"."INANITIONID"=:B2 AND "PARTID"=SUBSTR(:B1,5,4))
29 rows selected.
3. 生成sql profile(注意對比第二步中'Outline Data' 資料與本步驟中sys.sqlprof_attr內容的關係)
SQL> declare
2 v_hints sys.sqlprof_attr;
3 begin
4 v_hints := sys.sqlprof_attr(
5 'BEGIN_OUTLINE_DATA',
6 'IGNORE_OPTIM_EMBEDDED_HINTS',
7 'OPTIMIZER_FEATURES_ENABLE(''10.2.0.3'')',
8 'DB_VERSION(''10.2.0.3'')',
9 'ALL_ROWS',
10 'OUTLINE_LEAF(@"SEL$1")',
11 'INDEX_RS_ASC(@"SEL$1" "T"@"SEL$1" ("LHB_TEST"."INANITIONID" "LHB_TEST"."PARTID"))',
12 'END_OUTLINE_DATA');
13 dbms_sqltune.import_sql_profile(
14 'SELECT NODEID FROM ICDMIP.LHB_TEST T WHERE T.INANITIONID = :B2 AND PARTID = SUBSTR(:B1, 5, 4)',
15 v_hints,
16 'SQLPROFILE_T_M_O', --sql profile 名稱
17 force_match=>true,
18 replace=>true);
19 end;
20 /
2 v_hints sys.sqlprof_attr;
3 begin
4 v_hints := sys.sqlprof_attr(
5 'BEGIN_OUTLINE_DATA',
6 'IGNORE_OPTIM_EMBEDDED_HINTS',
7 'OPTIMIZER_FEATURES_ENABLE(''10.2.0.3'')',
8 'DB_VERSION(''10.2.0.3'')',
9 'ALL_ROWS',
10 'OUTLINE_LEAF(@"SEL$1")',
11 'INDEX_RS_ASC(@"SEL$1" "T"@"SEL$1" ("LHB_TEST"."INANITIONID" "LHB_TEST"."PARTID"))',
12 'END_OUTLINE_DATA');
13 dbms_sqltune.import_sql_profile(
14 'SELECT NODEID FROM ICDMIP.LHB_TEST T WHERE T.INANITIONID = :B2 AND PARTID = SUBSTR(:B1, 5, 4)',
15 v_hints,
16 'SQLPROFILE_T_M_O', --sql profile 名稱
17 force_match=>true,
18 replace=>true);
19 end;
20 /
4. 驗證sql profile已生效
PL/SQL procedure successfully completed.
SQL> explain plan for SELECT NODEID FROM ICDMIP.LHB_TEST T WHERE T.INANITIONID = :B2 AND PARTID = SUBSTR(:B1, 5, 4);
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1671553065
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1671553065
------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 44 | 4 (0)| 00:00:01 | | |
| 1 | PARTITION LIST SINGLE | | 1 | 44 | 4 (0)| 00:00:01 | KEY | KEY |
| 2 | TABLE ACCESS BY LOCAL INDEX ROWID| LHB_TEST | 1 | 44 | 4 (0)| 00:00:01 | KEY | KEY |
|* 3 | INDEX UNIQUE SCAN | PK_MIP_OPERATIONPROCESS | 1 | | 3 (0)| 00:00:01 | KEY | KEY |
------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 44 | 4 (0)| 00:00:01 | | |
| 1 | PARTITION LIST SINGLE | | 1 | 44 | 4 (0)| 00:00:01 | KEY | KEY |
| 2 | TABLE ACCESS BY LOCAL INDEX ROWID| LHB_TEST | 1 | 44 | 4 (0)| 00:00:01 | KEY | KEY |
|* 3 | INDEX UNIQUE SCAN | PK_MIP_OPERATIONPROCESS | 1 | | 3 (0)| 00:00:01 | KEY | KEY |
------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
---------------------------------------------------
3 - access("T"."INANITIONID"=:B2 AND "PARTID"=SUBSTR(:B1,5,4))
Note
-----
- SQL profile "SQLPROFILE_T_M_O" used for this statement--注意,當生效後,這裡會提示sql profile已使用
-----
- SQL profile "SQLPROFILE_T_M_O" used for this statement--注意,當生效後,這裡會提示sql profile已使用
19 rows selected.
SQL> exit
注:
刪除sql profile方法:
exec dbms_sqltune.drop_sql_profile(name => '&sql_profile');
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25313300/viewspace-754111/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ORACLE SQL PROFILE使用OracleSQL
- sql profile的使用SQL
- SQL Server profile使用技巧SQLServer
- sql_profile的使用(一)SQL
- 使用SQL Profile進行SQL優化案例SQL優化
- 【SQL Profile】coe_xfr_sql_profile.sql內容SQL
- sql profileSQL
- 檢視SQL PROFILE使用的HINTSQL
- 使用SQL Profile進行SQL最佳化案例SQL
- 使用sql profile固定執行計劃SQL
- sql最佳化:使用sql profile最佳化sql語句SQL
- MySQL Profile檢視SQL的資源使用MySql
- SQL PROFILE 測試SQL
- 使用sql profile實現outline的功能SQL
- sql tuning task和sql profileSQL
- SQL Profile 實驗03SQL
- SQL Profile 實驗02SQL
- SQL Profile 實驗01SQL
- 自動/手動型別sql_profile(dbms_sqltune/coe_xfr_sql_profile.sql)型別SQL
- 使用coe_xfr_sql_profile固定執行計劃SQL
- 使用SQL PROFILE 給出合理的執行計劃SQL
- 使用SQL Profile及SQL Tuning Advisor固定執行計劃SQL
- SQL Profile(第四篇)SQL
- SQL Profile(第三篇)SQL
- SQL Profile(第二篇)SQL
- 1223 result cache,sql profile,sql patchSQL
- SQL Profile(第一篇)SQL
- Oracle profile的使用Oracle
- oracle 通過sql profile為sql語句加hintOracleSQL
- 通過SQL PROFILE自動優化SQL語句SQL優化
- SQL PROFILE修改固定執行計劃SQL
- Oracle Profile 使用詳解Oracle
- sql tuning advisor(STA) 建議 建立sql profileSQL
- [20181119]使用sql profile優化問題.txtSQL優化
- [20220324]toad與sql profile使用問題.txtSQL
- 控制執行計劃之-SQL Profile(一)SQL
- DBMS_SQLTUNE詳解(2)---sql_profileSQL
- 用sql profile來固定執行計劃SQL