-- 数据类型的隐式转换,导致索引失效
-- 应该把转换写在等式右边
go@XE> create table t (x char(1) constraint t_pk primary key,y date);
Table created.
go@XE> desc t
Name Null? Type
--------------------- -------- ----------------------------------------
X NOT NULL CHAR(1)
Y DATE
go@XE> insert into t values ('5',sysdate);
1 row created.
go@XE> commit;
Commit complete.
go@XE> set autotrace traceonly explain
go@XE> select * from t where x=5;
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 12 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T | 1 | 12 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_NUMBER("X")=5) -- 列X隐式转换
Note
-----
- dynamic sampling used for this statement
-- 强制使用索引,导致索引全扫描,而不是唯一扫描
go@XE> select /*+ index(t,t_pk) */* from t where x=5;
Execution Plan
----------------------------------------------------------
Plan hash value: 1399892806
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 12 | 34 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 12 | 34 (0)| 00:00:01 |
|* 2 | INDEX FULL SCAN | T_PK | 1 | | 26 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter(TO_NUMBER("X")=5)
Note
-----
- dynamic sampling used for this statement
-- 正确的写法
go@XE> select * from t where x='5';
Execution Plan
----------------------------------------------------------
Plan hash value: 1303508680
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 12 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 12 | 1 (0)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | T_PK | 1 | | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("X"='5')
-- 应该把转换写在等式右边
go@XE> create table t (x char(1) constraint t_pk primary key,y date);
Table created.
go@XE> desc t
Name Null? Type
--------------------- -------- ----------------------------------------
X NOT NULL CHAR(1)
Y DATE
go@XE> insert into t values ('5',sysdate);
1 row created.
go@XE> commit;
Commit complete.
go@XE> set autotrace traceonly explain
go@XE> select * from t where x=5;
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 12 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T | 1 | 12 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_NUMBER("X")=5) -- 列X隐式转换
Note
-----
- dynamic sampling used for this statement
-- 强制使用索引,导致索引全扫描,而不是唯一扫描
go@XE> select /*+ index(t,t_pk) */* from t where x=5;
Execution Plan
----------------------------------------------------------
Plan hash value: 1399892806
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 12 | 34 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 12 | 34 (0)| 00:00:01 |
|* 2 | INDEX FULL SCAN | T_PK | 1 | | 26 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter(TO_NUMBER("X")=5)
Note
-----
- dynamic sampling used for this statement
-- 正确的写法
go@XE> select * from t where x='5';
Execution Plan
----------------------------------------------------------
Plan hash value: 1303508680
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 12 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 12 | 1 (0)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | T_PK | 1 | | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("X"='5')
0 件のコメント:
コメントを投稿