oracle INTERNAL_FUNCTION
當執行計劃出現INTERNAL_FUNCTION的時候索引肯定失效,這裡可以理解為隱士轉換。
我們來看看官方文件,oracle叫他
我們做一下測試:
SQL> create table testdt (dt date);
Table created.
SQL> insert into testdt values(sysdate);
1 row created.
SQL> set autotrace on
SQL> select * from testdt where dt=TO_TIMESTAMP('1-jan-2000', 'dd-mon-yyyy');
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 3876087351
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 9 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TESTDT | 1 | 9 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(INTERNAL_FUNCTION("DT")=TIMESTAMP' 2000-01-01
00:00:00.000000000')
SQL> select * from testdt where dt=to_date('1-jan-2000', 'dd-mon-yyyy');
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 3876087351
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 9 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TESTDT | 1 | 9 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("DT"=TO_DATE(' 2000-01-01 00:00:00', 'syyyy-mm-dd
hh24:mi:ss'))
雖然語法沒有問題但是要注意
date 型別用to_date
SQL> select * from testdt where to_char(dt,'YYYY')='2016';
DT
---------
05-AUG-16
Execution Plan
----------------------------------------------------------
Plan hash value: 3876087351
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 9 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TESTDT | 1 | 9 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_CHAR(INTERNAL_FUNCTION("DT"),'YYYY')='2016')
我們可以用如下代替:
SQL> select * from testdt where dt>=to_date('2016','yyyy') ;
DT
---------
05-AUG-16
Execution Plan
----------------------------------------------------------
Plan hash value: 3876087351
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 9 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TESTDT | 1 | 9 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("DT">=TO_DATE('2016','yyyy'))
Note
-----
- dynamic sampling used for this statement (level=2)
當然情況還有很多這裡不在舉例,但是遇到這種情況一定要從資料是否匹配入手去檢查
我們來看看官方文件,oracle叫他
Function Calls
For example, consider the following query:
EXPLAIN PLAN FOR SELECT SUM(quantity_sold) FROM sales WHERE time_id = TO_TIMESTAMP('1-jan-2000', 'dd-mon-yyyy');
Because time_id is of type DATE and Oracle must promote it to the TIMESTAMP type to get the same data type, this predicate is internally rewritten as:
TO_TIMESTAMP(time_id) = TO_TIMESTAMP('1-jan-2000', 'dd-mon-yyyy')
The execution plan for this statement is as follows:
-------------------------------------------------------------------------------------------- |Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop | -------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 11 | 6 (17)| 00:00:01 | | | | 1 | SORT AGGREGATE | | 1 | 11 | | | | | | 2 | PARTITION RANGE ALL| | 10 | 110 | 6 (17)| 00:00:01 | 1 | 16 | |*3 | TABLE ACCESS FULL | SALES | 10 | 110 | 6 (17)| 00:00:01 | 1 | 16 | -------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - filter(INTERNAL_FUNCTION("TIME_ID")=TO_TIMESTAMP('1-jan-2000',:B1)) 15 rows selected
我們做一下測試:
SQL> create table testdt (dt date);
Table created.
SQL> insert into testdt values(sysdate);
1 row created.
SQL> set autotrace on
SQL> select * from testdt where dt=TO_TIMESTAMP('1-jan-2000', 'dd-mon-yyyy');
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 3876087351
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 9 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TESTDT | 1 | 9 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(INTERNAL_FUNCTION("DT")=TIMESTAMP' 2000-01-01
00:00:00.000000000')
SQL> select * from testdt where dt=to_date('1-jan-2000', 'dd-mon-yyyy');
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 3876087351
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 9 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TESTDT | 1 | 9 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("DT"=TO_DATE(' 2000-01-01 00:00:00', 'syyyy-mm-dd
hh24:mi:ss'))
雖然語法沒有問題但是要注意
date 型別用to_date
timestamp型別用to_timestamp
再看如下的例子:SQL> select * from testdt where to_char(dt,'YYYY')='2016';
DT
---------
05-AUG-16
Execution Plan
----------------------------------------------------------
Plan hash value: 3876087351
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 9 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TESTDT | 1 | 9 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_CHAR(INTERNAL_FUNCTION("DT"),'YYYY')='2016')
我們可以用如下代替:
SQL> select * from testdt where dt>=to_date('2016','yyyy') ;
DT
---------
05-AUG-16
Execution Plan
----------------------------------------------------------
Plan hash value: 3876087351
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 9 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TESTDT | 1 | 9 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("DT">=TO_DATE('2016','yyyy'))
Note
-----
- dynamic sampling used for this statement (level=2)
當然情況還有很多這裡不在舉例,但是遇到這種情況一定要從資料是否匹配入手去檢查
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2122996/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ORACLE基礎之oracle鎖(oracle lock mode)詳解Oracle
- [Oracle]Oracle良性SQL建議OracleSQL
- cx_Oracle 連線 OracleOracle
- 【Oracle】Oracle logminer功能介紹Oracle
- OracleOracle
- Oracle案例12——NBU Oracle恢復Oracle
- Oracle vs PostgreSQL DBA(21)- Oracle VPDOracleSQL
- 「Oracle」Oracle 資料庫安裝Oracle資料庫
- [Oracle] -- 配置Oracle環境變數Oracle變數
- Oracle案例13—— OGG-01163 Oracle GoldenGate Delivery for Oracle, reprpt01.prmOracleGo
- 4.1.6 Oracle Restart 與 Oracle Data Guard 整合OracleREST
- oracle資料庫與oracle例項Oracle資料庫
- 「Oracle」Oracle資料庫基本概念Oracle資料庫
- 「Oracle」Oracle 資料庫基本概念Oracle資料庫
- 「Oracle」Oracle高階查詢介紹Oracle
- Oracle系列Oracle
- oracle user$Oracle
- Oracle TimestampOracle
- 序章-oracleOracle
- ORACLE AUDITOracle
- Spotlight for OracleOracle
- Oracle exceptionOracleException
- Oracle 阻塞Oracle
- oracle plsqlOracleSQL
- oracle 字串Oracle字串
- Oracle JoinsOracle
- oracle RACOracle
- oracle 3Oracle
- Oracle TablesOracle
- oracle opsOracle
- Oracle CursorOracle
- Oracle CoherenceOracle
- oracle程序Oracle
- oracle 文件Oracle
- Oracle:SCNOracle
- Oracle 索引Oracle索引
- Oracle:RBOOracle
- ORACLE AWROracle
- Oracle 19c Concepts(01):Introduction to Oracle DatabaseOracleDatabase