查看awr報告過程發現一個隱式轉換致使的索引失效問題,作作記錄sql
awr中語句實際消耗大40多w邏輯讀ide
代入變量後測試很快測試
SQL> SELECT a.order_prod_amount, b.prod_cost_price FROM user_sess a, PR_DO_T b WHERE a.prod_id = b.prod_id AND a.order_no = '125924325287'; Elapsed: 00:00:00.00 Execution Plan ---------------------------------------------------------- Plan hash value: 4287492176 ------------------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop | ------------------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 30 | 4 (0)| 00:00:01 | | | | 1 | NESTED LOOPS | | 1 | 30 | 4 (0)| 00:00:01 | | | | 2 | TABLE ACCESS BY GLOBAL INDEX ROWID| user_sess | 1 | 21 | 3 (0)| 00:00:01 | ROWID | ROWID | |* 3 | INDEX UNIQUE SCAN | SYS_C008504 | 1 | | 2 (0)| 00:00:01 | | | | 4 | TABLE ACCESS BY INDEX ROWID | PR_DO_T | 2983 | 26847 | 1 (0)| 00:00:01 | | | |* 5 | INDEX UNIQUE SCAN | SYS_C008459 | 1 | | 0 (0)| 00:00:01 | | | ------------------------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - access("A"."ORDER_NO"='125924325287') 5 - access("A"."PROD_ID"="B"."PROD_ID") Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 7 consistent gets 2 physical reads 0 redo size 489 bytes sent via SQL*Net to client 396 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed
按語句sqlid查看歷史執行計劃spa
SQL> SELECT a.order_prod_amount, b.prod_cost_price FROM user_sess a, PR_DO_T b WHERE a.prod_id = b.prod_id AND a.order_no = '871314 SQL> set linesize 200 SQL> select * from table(dbms_xplan.display_cursor('88f4paj5t26xb')); PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------------------------------------- SQL_ID 88f4paj5t26xb, child number 0 ------------------------------------- SELECT a.order_prod_amount,b.prod_cost_price FROM user_sess a, PR_DO_T b WHERE a.prod_id = b.prod_id AND a.order_no = :"SYS_B_0" Plan hash value: 4105848458 ------------------------------------------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop | ------------------------------------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | | | 106K(100)| | | | PLAN_TABLE_OUTPUT ------------------------------------------------------------------------------------------------------------ | 1 | NESTED LOOPS | | 1 | 30 | 106K (1)| 00:21:14 | | | | 2 | PARTITION RANGE ALL | | 1 | 21 | 106K (1)| 00:21:14 | 1 | 53 | |* 3 | TABLE ACCESS FULL | user_sess | 1 | 21 | 106K (1)| 00:21:14 | 1 | 53 | | 4 | TABLE ACCESS BY INDEX ROWID| PR_DO_T | 1 | 9 | 1 (0)| 00:00:01 | | | |* 5 | INDEX UNIQUE SCAN | SYS_C008459 | 1 | | 0 (0)| | | | ------------------------------------------------------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 3 - filter(TO_NUMBER("A"."ORDER_NO")=:SYS_B_0) PLAN_TABLE_OUTPUT ------------------------------------------------------------------------------------------------------------- 5 - access("A"."PROD_ID"="B"."PROD_ID") 24 rows selected.
user_sess明顯走錯了執行計劃,使用了全表掃描code
忽然想到user_sess表字段order_no爲varchar2猜想爲隱式轉換
去掉單引號測試
orm
SQL> SELECT a.order_prod_amount, b.prod_cost_price FROM user_sess a, PR_DO_T b WHERE a.prod_id = b.prod_id AND a.order_no = 125924325287; Elapsed: 00:00:18.46 Execution Plan ---------------------------------------------------------- Plan hash value: 4105848458 ------------------------------------------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop | ------------------------------------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | 30 | 106K (1)| 00:21:14 | | | | 1 | NESTED LOOPS | | 1 | 30 | 106K (1)| 00:21:14 | | | | 2 | PARTITION RANGE ALL | | 1 | 21 | 106K (1)| 00:21:14 | 1 | 53 | |* 3 | TABLE ACCESS FULL | user_sess | 1 | 21 | 106K (1)| 00:21:14 | 1 | 53 | | 4 | TABLE ACCESS BY INDEX ROWID| PR_DO_T | 1 | 9 | 1 (0)| 00:00:01 | | | |* 5 | INDEX UNIQUE SCAN | SYS_C008459 | 1 | | 0 (0)| 00:00:01 | | | ------------------------------------------------------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 3 - filter(TO_NUMBER("A"."ORDER_NO")=125924325287) 5 - access("A"."PROD_ID"="B"."PROD_ID") Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 484974 consistent gets 477021 physical reads 0 redo size 489 bytes sent via SQL*Net to client 396 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed
找開發人員加回蛋引號避免隱式轉換後恢復blog
注:隱式類型轉換number轉換成varchar2時,索引會失效。varchar2轉換爲number時,索引並不失效
索引