sql_profile fixed SQL execution plan

Use sql_profile fixed SQL execution plan 

1 Chedan

Recently, a fixed time every night, there will be a lot of databases latch: cache buffer chains waiting. View found by the original SQL statement is wrong execution plan, 240G of the table, a full table scan, causing hot block contention. Fixed by the Plan of Implementation of the SQL statement so that it can properly use index scans, the problem can be solved. The following is a procedure.

2 using the SQL PROFILE fixed execution plan

 

2.1 View original statement execution plan

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 ;
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 | the LIST PARTITION 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 | - go here to find a full table scan, the whole table has 240G, causing performance problems 
--------------------------- -------------------------------------------------- ----------------------------------- 

Statistics 
-------------- -------------------------------------------- 
1 recursive This Calls 
0 db Block the gets 
54997 the gets consistent - serious logical reads, SQL statements, poor performance 
580 PHYSICAL reads 
0 redo size 
20,489,108 bytes Sent Via SQL * Net to Client 
438 116 bytes Received from Via SQL * Net Client 
39 786 SQL * Net roundtrips to / from Client 
0 sorts (Memory) 
0 sorts (Disk) 
596762 rows Processed 
the SQL> SET OFF AUTOTRACE 
the SQL> SET ALTER CURRENT_SCHEMA = ICDMIP the session;

Session altered.

2.2 specify the SQL better use of selective index

Viewed in advance, the conditions PARTID can use the index PK_MIP_OPERATIONPROCESS. To try, after using this index, how to execute the plan.

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);
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> a )
1* select * from table(dbms_xplan.display(null,null,'outline'))
SQL> /
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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已使用相关索引
------------------------------------------------------------------------------------------------------------------------------
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.

2.3 generating sql profile

Note that in the second step the relationship between contrast 'Outline Data' to the data in the present step 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 /

PL/SQL procedure successfully completed.

2.4 verify whether the entry into force sql profile

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
------------------------------------------------------------------------------------------------------------------------------
| 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 the this statement-- note that when into effect, there will be prompted to sql profile has been used 
19 the Selected rows. 
SQL> Exit

note
Remove sql profile method exec dbms_sqltune.drop_sql_profile (name => '& sql_profile');

Author: halberd.lee

Created: 2019-06-22 Sat 18:35

Validate

Guess you like

Origin www.cnblogs.com/halberd-lee/p/11069911.html