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
- MySQL中IS NULL、IS NOT NULL、!=不能用索引?胡扯!MySqlNull索引
- 為什麼索引無法使用is null和is not null索引Null
- Oracle vs PostgreSQL,研發注意事項(12) - NULL與索引OracleSQLNull索引
- NULL列時,如何使得IS NULL或者IS NOT NULL可以使用索引來提高查詢效率Null索引
- undefined與null與?. ??UndefinedNull
- [20231024]NULL值在索引的情況.txtNull索引
- null與indexNullIndex
- NULL在oracle和mysql索引上的區別NullOracleMySql索引
- MySQL null值欄位是否使用索引的總結MySqlNull索引
- 資料庫聚簇索引——not null條件對唯一鍵索引成為聚簇索引的影響資料庫索引Null
- JavaScript undefined與null區別JavaScriptUndefinedNull
- [20200317]NULL與排序輸出.txtNull排序
- in、exists與索引索引
- order by與索引索引
- mysql中null與“空值”的坑MySqlNull
- in、exists操作與null的一點總結Null
- 【NULL】Oracle null值介紹NullOracle
- 淺談聚簇索引與非聚簇索引索引
- 匿名類 與 索引重建索引
- PHP7 ?? 與 ?: 的作用和區別(null合併運算子, null條件運算子)PHPNull
- 淺談MySQL的B樹索引與索引優化MySql索引優化
- Hashtable/HashMap與key/value為null的關係HashMapNull
- JS中判斷null、undefined與NaN的方法JSNullUndefinedNaN
- [20200326]繫結變數抓取與NULL值.txt變數Null
- 《PostgreSQL》 索引與最佳化SQL索引
- InnoDB索引與底層原理索引
- MongoDB慢查詢與索引MongoDB索引
- ES 文件與索引介紹索引
- Elasticsearch之索引模板index template與索引別名index aliasElasticsearch索引Index
- sql server 索引闡述系列七 索引填充因子與碎片SQLServer索引
- PropertyChanged == nullNull
- MySQL NULLMySqlNull
- JS 應用篇(一):Undefined與Null的區別JSUndefinedNull
- TreeSet的null值與元素型別的約束Null型別
- MySQL null值儲存,null效能影響MySqlNull
- 2>/dev/null和>/dev/null 2>&1和2>&1>/dev/null的區別devNull
- MYSQL timestamp NOT NULL插入NULL的報錯問題MySqlNull