【翻譯自mos文章】SYS_OP_C2C 導致的全表掃描(fts)/全索引掃描,sys_op_c2cfts
SYS_OP_C2C 導致的全表掃描(fts)/全索引掃描
參考原文:
SYS_OP_C2C Causing Full Table/Index Scans (Doc ID 732666.1)
適用於:
Oracle Database - Enterprise Edition - Version 10.1.0.2 to 12.1.0.1 [Release 10.1 to 12.1]
Information in this document applies to any platform.
This problem can occur on any platform.
癥狀:
1)正在執行一個帶有綁定變數的查詢
2)綁定變數經由application(.net, j2ee等)使用 "string" 類型的綁定變數來綁定。
3)該查詢錯誤的執行了全表掃描/索引掃描,而沒有使用索引唯一掃描或者索引範圍掃描
4)使用advanced 選項查看explain plan, sqltxlain or 10053 trace,你會注意到在"Predicate Information"部分 會顯示一個 "filter(SYS_OP_C2C)".
e.g select * from table(dbms_xplan.display_cursor(&sql_id,null,'ADVANCED'));
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(SYS_OP_C2C("COL1")=:B1) <=== filter operation occurring
原因:
"string" 綁定變數 與 table 中的該 column 使用了不同的資料類型
這意味著 當執行這個查詢的時候,需要把資料進行一個隱式類型轉換。 SYS_OP_C2C 是一個隱式函數(implicit function),該函數用於欄位(列)在nchar和char之間轉換
解決方案:
1. 建立一個基於函數的索引。
e.g create index <index_name> on <table_name> (SYS_OP_C2C(<column>));
或者:
2.讓綁定變數定義的資料類型與該列的資料類型一致。
A java example where this can occurs is when defaultNChar=TRUE. This will cause strings to bind as NVARCHAR2 causing the predicate that are subset datatypes to be converted to NVARCHAR2.
e.g. -Doracle.jdbc.defaultNChar=true
<connection-property name="defaultNChar">true</connection-property>