1. Environment:
SCOTT test01p @> @ ver1
PORT_STRING VERSION BANNER CON_ID
------------------------------ ------ ------------------------------------------ -------- ---------- --------------------------------------
IBMPC / 12.2.0.1.0 the Oracle Enterprise Database 12c WIN_NT64-9.1.0 Edition Release 12.2.0.1.0 - 64bit Production 0
Create Table T AS SELECT. 1 ID1, ID2 rownum, 'Test' by name from Dual Connect Level <1E4;
INSERT INTO values T (1e4,1e4, 'ABCD');
the commit;
Create index if_t_id2 ON T (decode (ID1,. 1, to_number (null), ID2));
- // analysis table omitted.
- // simple instructions to_number (null) to ensure the return data type is NULL value type number.
Test 2:
SCOTT @ test01p> SELECT column_name, data_type from user_tab_cols WHERE table_name = 'T';
COLUMN_NAME the DATA_TYPE
-------------------- ------- ---
ID1 NUMBER
ID2 NUMBER
NAME CHAR
SYS_NC00004 $ NUMBER
- // add a hidden field SYS_NC00004 $ return data type is the type of number..
SCOTT @ test01p> = All the ALTER STATISTICS_LEVEL the session the SET;
the Session the Altered.
SCOTT @ test01p> the SELECT WHERE decode from T * (ID1,. 1, to_number (null), ID2) = 1E4;
IDl ID2 NAME
---------- ---------- ------- -------------
10000 10000 ABCD
SCOTT test01p @> @ DPC '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 7srfk2yjdxx49, child number 0
-------------------------------------
select * from t where decode(id1, 1,to_number(null), id2) = 1e4
Plan hash value: 1601196873
--------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
--------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 10 (100)| | 1 |00:00:00.01 | 31 |
|* 1 | TABLE ACCESS FULL| T | 1 | 100 | 1200 | 10 (0)| 00:00:01 | 1 |00:00:00.01 | 31 |
--------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 / T@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(DECODE("ID1",1,NULL,"ID2")=10000)
--//你可以发现实际上filter(DECODE("ID1",1,NULL,"ID2")=10000).
SCOTT@test01p> select * from user_ind_expressions where index_name = 'IF_T_ID2'
2 @prxx
==============================
INDEX_NAME: IF_T_ID2
TABLE_NAME: T
COLUMN_EXPRESSION: DECODE ( "ID1", 1, NULL, "ID2 ")
COLUMN_POSITION: 1
PL / SQL Procedure Completed successfully.
- // you can find inconsistencies function index I created and saved expression.
- // try to rewrite a look at it?
SCOTT @ test01p> t from the SELECT * decode WHERE (ID1,. 1, null, ID2) = 1E4;
IDl ID2 NAME
---------- ---------- ------------- -------
10000 10000 ABCD
SCOTT test01p @> @ DPC '' ''
PLAN_TABLE_OUTPUT
----------------------------- --------
SQL_ID by0609fp41hy2, Child Number The 0
------------------------------------ -
select * from t where decode(id1, 1,null, id2) = 1e4
Plan hash value: 1130968923
------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 2 (100)| | 1 |00:00:00.01 | 2 |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T | 1 | 1 | 13 | 2 (0)| 00:00:01 | 1 |00:00:00.01 | 2 |
|* 2 | INDEX RANGE SCAN | IF_T_ID2 | 1 | 1 | | 1 (0)| 00:00:01 | 1 |00:00:00.01 | 1 |
------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 / T@SEL$1
2 - SEL$1 / T@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
2 - Access (. "T" "SYS_NC00004 $" = 10000)
- // so you can use the index, and so there is an obvious error, introduced under the previous blog Yangtingkun, such as the return type is NULL character is not clear. specify
- // The default type is varchar2 type and the actual number now is because SYS_NC00004 $ type is the type NUMBER...
3. continue to test:
-? // if the index rebuild online do
SCOTT @ test01p> alter index IF_T_ID2 rebuild Online;
Index Altered.
SCOTT @ test01p> SELECT * WHERE decode from T (ID1,. 1, null, ID2) = 1E4;
IDl ID2 NAME
---------- ---------- --------------------
10000 10000 ABCD
SCOTT test01p @> @ DPC '' ''
PLAN_TABLE_OUTPUT
---------------- ---------------------
SQL_ID by0609fp41hy2, Child Number The 0
----------------------- --------------
select * from t where decode(id1, 1,null, id2) = 1e4
Plan hash value: 1601196873
--------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
--------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 11 (100)| | 1 |00:00:00.01 | 31 |
|* 1 | TABLE ACCESS FULL| T | 1 | 1 | 13 | 11 (10)| 00:00:01 | 1 |00:00:00.01 | 31 |
-------------------------------------------------- -------------------------------------------------- ----------------
Query Block the Name / Object Alias (Operation IDENTIFIED by the above mentioned id):
--------------------- ----------------------------------------
1 - SEL $ 1 / T @ SEL $ 1
Predicate Information (IDENTIFIED Operation by the above mentioned id):
------------------------------------------ ---------
. 1 - filter (the TO_NUMBER (the DECODE ( "IDl",. 1, NULL, the TO_CHAR ( "ID2"))) = 10000)
- // Note the filter conditions, an implicit conversion occurs the front added the TO_NUMBER.
SCOTT @ test01p> SELECT column_name, data_type from user_tab_cols WHERE table_name = 'T';
COLUMN_NAME DATA_TYPE
-------------------- --------------------
ID1 NUMBER
ID2 NUMBER
CHAR NAME
SYS_NC00004 $ VARCHAR2
- // After rebuilding the index, the implied contrast to the situation in front of the field SYS_NC00004 $ data type has changed, become varchar2 type.
- // To ensure the use of the index should be written as follows:
SCOTT @ test01p> select * from t where decode (id1 , 1, null, id2) = to_char (1e4);
Zhengzhou infertility hospital: http: //www.xbzztj.com/
ID1 ID2 NAME
---------- ---------- --------------------
10000 10000 abcd
SCOTT@test01p> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 63zyt23ufr2xa, child number 0
-------------------------------------
select * from t where decode(id1, 1,null, id2) = to_char(1e4)
Plan hash value: 1130968923
------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 2 (100)| | 1 |00:00:00.01 | 2 |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T | 1 | 1 | 13 | 2 (0)| 00:00:01 | 1 |00:00:00.01 | 2 |
|* 2 | INDEX RANGE SCAN | IF_T_ID2 | 1 | 1 | | 1 (0)| 00:00:01 | 1 |00:00:00.01 | 1 |
------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id): http://www.chacha8.cn/detail/1132398216.html
-------------------------------------------------------------
1 - SEL$1 / T@SEL$1
2 - SEL$1 / T@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("T"."SYS_NC00004$"='10000')