關於NULL值在索引裡的兩個疑惑
create table wxh_tbd as select * from dba_objects;
Table created.
create index t_s on wxh_tbd(object_id,OBJECT_NAME);
Index created.
select * from wxh_tbd where object_id is null and object_name>'WXH_TBD' order by object_name;
183 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2494076512
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 246 | 50922 | 4 (25)| 00:00:01 |
| 1 | SORT ORDER BY | | 246 | 50922 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS BY INDEX ROWID| WXH_TBD | 246 | 50922 | 3 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | T_S | 6 | | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 246 | 50922 | 4 (25)| 00:00:01 |
| 1 | SORT ORDER BY | | 246 | 50922 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS BY INDEX ROWID| WXH_TBD | 246 | 50922 | 3 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | T_S | 6 | | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------------------
執行計劃部分竟然出現了排序?
這是因為NULL,1 NULL,2,ORACLE不知道誰大誰小,因為前面出現了ORACLE無法判斷大小的值NULL.
explain plan for
2 select * from wxh_tbd where object_id is null and object_name='ss';
2 select * from wxh_tbd where object_id is null and object_name='ss';
Explained.
@display
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 792848615
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 792848615
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 7 | 1449 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| WXH_TBD | 7 | 1449 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | T_S | 3 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 7 | 1449 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| WXH_TBD | 7 | 1449 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | T_S | 3 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
---------------------------------------------------
2 - access("OBJECT_ID" IS NULL AND "OBJECT_NAME"='ss')
filter("OBJECT_NAME"='ss')
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
1343 bytes sent via SQL*Net to client
509 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
filter("OBJECT_NAME"='ss')
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
1343 bytes sent via SQL*Net to client
509 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
執行計劃部分出現了OBJECT_NAME的FILTER操作,但是OBJECT_NAME依然也出現在了ACCESS裡。ORACLE其實根據頁塊能夠精確定位到NULL,SS的記錄(其實只能精確定位到起始頁塊)。執行計劃出現的FILTER操作,只是顯示問題。跟ACCESS的效率是一樣的。
可是下面的filter操作則不是一回事了。
explain plan for
select * from wxh_tbd where object_id is null and object_name like '%ss%';
select * from wxh_tbd where object_id is null and object_name like '%ss%';
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 172 | 3 |
| 1 | TABLE ACCESS BY INDEX ROWID| WXH_TBD | 2 | 172 | 3 |
|* 2 | INDEX RANGE SCAN | TSSAFA | 2 | | 2 |
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 172 | 3 |
| 1 | TABLE ACCESS BY INDEX ROWID| WXH_TBD | 2 | 172 | 3 |
|* 2 | INDEX RANGE SCAN | TSSAFA | 2 | | 2 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
---------------------------------------------------
2 - access("WXH_TBD"."OBJECT_ID" IS NULL)
filter("WXH_TBD"."OBJECT_NAME" LIKE '%ss%')
filter("WXH_TBD"."OBJECT_NAME" LIKE '%ss%')
這個filter效能就比較差了。ORACLE要掃描所有為NULL的葉子塊,從中FILTER出object_name包含ss的條目
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22034023/viewspace-714034/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 關於NULL的兩個計算Null
- 索引裡的NULL值與排序小記索引Null排序
- 關於值物件的理解,疑惑物件
- NULL 值與索引Null索引
- 關於null值的小知識Null
- NULL 值與索引(二)Null索引
- js關於this的疑惑JS
- 關於/dev/null和/dev/zero兩個檔案裝置devNull
- 唯一索引,可以在索引列插入多個null嗎索引Null
- MySQL裡null與空值的辨析MySqlNull
- 關於 self 和 super 在oc 中 的疑惑 與 分析
- 關於ORACLE MYSQL NOT IN和NOT exists需要注意的 NULL值OracleMySqlNull
- 關於struts開發的疑惑
- 關於組合模式的疑惑模式
- 關於NULLNull
- [20231024]NULL值在索引的情況.txtNull索引
- MySQL null值欄位是否使用索引的總結MySqlNull索引
- Oracle 在連線條件裡處理和比較 NULL 值OracleNull
- Go - 關於 protoc 工具的小疑惑Go
- 再說索引與Null值對於Hints及執行計劃的影響索引Null
- mssql sqlserver in 關鍵字在值為null的應用舉例SQLServerNull
- 關於 oracle NULLOracleNull
- 將這兩天關於許可權的討論歸檔在這裡
- NULL在oracle和mysql索引上的區別NullOracleMySql索引
- 關於 ulimit 的兩個天坑MIT
- 關於對DDD應用層的疑惑
- 關於jdon 的事務處理疑惑?
- 關於java領域建模疑惑Java
- ORACLE關於NULL的總結OracleNull
- 將一個物件裡所有的空值屬性設定成null物件Null
- 淺談索引序列之是否可以儲存NULL值?索引Null
- 有關Java Collection API的一個疑惑JavaAPI
- 關於JBoss Group 原始碼存放方式的疑惑原始碼
- 關於Docker中網路效能疑惑Docker
- 關於介面的一些疑惑
- 【C#】-對於Null值的處理方法C#Null
- NULL與索引Null索引
- 關於oracle的jobs的兩個檢視Oracle