NULL與索引
JEL@JEL >create table t (x int,y int);
Table created.
JEL@JEL >create unique index i_t on t(x,y);
Index created.
JEL@JEL >insert into t values (1,1);
1 row created.
JEL@JEL >insert into t values (1,null);
1 row created.
JEL@JEL >insert into t values (null,1);
1 row created.
JEL@JEL >insert into t values (null,null);
1 row created.
JEL@JEL >insert into t values (null,1);
insert into t values (null,1)
*
ERROR at line 1:
ORA-00001: unique constraint (JEL.I_T) violated
JEL@JEL >insert into t values (null,null);
1 row created.
如上知,(null,1)與(null,1)是相同的,而(null,null)與(null,null)不同
JEL@JEL >set autotrace on
JEL@JEL >select * from t where x is null;
X Y
---------- ----------
1
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3 | 9 | 17 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T | 3 | 9 | 17 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("X" IS NULL)
如上計劃執行全表掃描
對列y加not null約束
JEL@JEL >truncate table t;
Table truncated.
JEL@JEL >alter table t modify y int not null;
Table altered.
JEL@JEL >insert into t values (1,1);
1 row created.
JEL@JEL >insert into t values (null,1);
1 row created.
JEL@JEL >exec dbms_stats.gather_table_stats('JEL','T');
PL/SQL procedure successfully completed.
JEL@JEL >set autotrace on
JEL@JEL >select * from t where x is null;
X Y
---------- ----------
1
Execution Plan
----------------------------------------------------------
Plan hash value: 2616361825
-------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 5 | 1 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| I_T | 1 | 5 | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("X" IS NULL)
如上走索引了
結論,對於複合索引,只有當索引鍵中至少一個列定義為not null時,查詢才會使用索引
Table created.
JEL@JEL >create unique index i_t on t(x,y);
Index created.
JEL@JEL >insert into t values (1,1);
1 row created.
JEL@JEL >insert into t values (1,null);
1 row created.
JEL@JEL >insert into t values (null,1);
1 row created.
JEL@JEL >insert into t values (null,null);
1 row created.
JEL@JEL >insert into t values (null,1);
insert into t values (null,1)
*
ERROR at line 1:
ORA-00001: unique constraint (JEL.I_T) violated
JEL@JEL >insert into t values (null,null);
1 row created.
如上知,(null,1)與(null,1)是相同的,而(null,null)與(null,null)不同
JEL@JEL >set autotrace on
JEL@JEL >select * from t where x is null;
X Y
---------- ----------
1
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3 | 9 | 17 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T | 3 | 9 | 17 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("X" IS NULL)
如上計劃執行全表掃描
對列y加not null約束
JEL@JEL >truncate table t;
Table truncated.
JEL@JEL >alter table t modify y int not null;
Table altered.
JEL@JEL >insert into t values (1,1);
1 row created.
JEL@JEL >insert into t values (null,1);
1 row created.
JEL@JEL >exec dbms_stats.gather_table_stats('JEL','T');
PL/SQL procedure successfully completed.
JEL@JEL >set autotrace on
JEL@JEL >select * from t where x is null;
X Y
---------- ----------
1
Execution Plan
----------------------------------------------------------
Plan hash value: 2616361825
-------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 5 | 1 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| I_T | 1 | 5 | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("X" IS NULL)
如上走索引了
結論,對於複合索引,只有當索引鍵中至少一個列定義為not null時,查詢才會使用索引
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29337971/viewspace-1063527/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- NULL 值與索引Null索引
- 索引與null(一):單列索引索引Null
- 索引與null(二):組合索引索引Null
- NULL 值與索引(二)Null索引
- 索引裡的NULL值與排序小記索引Null排序
- MySQL中IS NULL、IS NOT NULL、!=不能用索引?胡扯!MySqlNull索引
- 為什麼索引無法使用is null和is not null索引Null
- not null與check is not nullNull
- Oracle vs PostgreSQL,研發注意事項(12) - NULL與索引OracleSQLNull索引
- 索引失效系列——說說is null索引Null
- NULL列時,如何使得IS NULL或者IS NOT NULL可以使用索引來提高查詢效率Null索引
- mysql探究之null與not nullMySqlNull
- 唯一索引,可以在索引列插入多個null嗎索引Null
- undefined與null與?. ??UndefinedNull
- MySQL案例-TIMESTAMP NOT NULL與NULLMySqlNull
- null與indexNullIndex
- null與substrNull
- NULL與排序Null排序
- 謂詞條件是is null走索引嗎?Null索引
- ORACLE 索引和MYSQL INNODB 輔助索引對NULL的處理區別Oracle索引MySqlNull
- 再說索引與Null值對於Hints及執行計劃的影響索引Null
- NULL在oracle和mysql索引上的區別NullOracleMySql索引
- 資料庫索引欄位請不要為NULL資料庫索引Null
- oracle全文索引之FILTER_1_NULL_FILTEROracle索引FilterNull
- 如何讓table表的null列由不走索引變為可走索引Null索引
- Null 與 “” 的區別Null
- “NOT_IN”與“NULL”的邂逅Null
- MySQL null值欄位是否使用索引的總結MySqlNull索引
- 淺談索引序列之是否可以儲存NULL值?索引Null
- 資料列not null對索引影響一例Null索引
- 表為多列為null的表之索引示例Null索引
- 關於NULL值在索引裡的兩個疑惑Null索引
- 資料庫聚簇索引——not null條件對唯一鍵索引成為聚簇索引的影響資料庫索引Null
- ((NULL) null).printNULL();((NULL) null).printnull();Null
- oracle全文索引之SECTION GROUP_1_NULL_SECTION_GROUPOracle索引Null
- JavaScript undefined與null區別JavaScriptUndefinedNull
- undefined與null的區別UndefinedNull
- count_sum_distinct與nullNull