索引與Null值對于Hints及執行計劃的影響
2024-07-21 02:32:50
供稿:網友
由于B*Tree索引不存儲Null值,所以在索引字段答應為空的情況下,某些Oracle查詢不會使用索引.很多時候,我們看似可以使用全索引掃描(Full Index Scan)的情況,可能Oracle就會因為Null值的存在而放棄索引.在此情況下即使使用Hints,Oracle也不會使用索引,其根本原因就是因為Null值的存在.我們看以下測試.在username字段為Not Null時,Index Hints可以生效.SQL> create table t as select username,passWord from dba_users;Table created.SQL> desc t
Name Null? Type
----------------------------------------- -------- ----------------------------
USERNAME NOT NULL VARCHAR2(30)
PASSWORD VARCHAR2(30)SQL> create index i_t on t(username);Index created.SQL> set autotrace trace eXPlain
SQL> select * from t where username='EYGLE';Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873--------------------------------------------------------------------------
Id Operation Name Rows Bytes Cost (%CPU) Time
--------------------------------------------------------------------------
0 SELECT STATEMENT 1 34 2 (0) 00:00:01
* 1 TABLE access FULL T 1 34 2 (0) 00:00:01
--------------------------------------------------------------------------PRedicate Information (identified by operation id):
--------------------------------------------------- 1 - filter("USERNAME"='EYGLE')Note
-----
- dynamic sampling used for this statementSQL> set linesize 120
SQL> select /*+ index(t,i_t) */ * from t where username='EYGLE';Execution Plan
----------------------------------------------------------
Plan hash value: 2928007915------------------------------------------------------------------------------------
Id Operation Name Rows Bytes Cost (%CPU) Time
------------------------------------------------------------------------------------
0 SELECT STATEMENT 1 34 2 (0) 00:00:01
1 TABLE ACCESS BY INDEX ROWID T 1 34 2 (0) 00:00:01
* 2 INDEX RANGE SCAN I_T 1 1 (0) 00:00:01
------------------------------------------------------------------------------------Predicate Information (identified by operation id):
--------------------------------------------------- 2 - access("USERNAME"='EYGLE')Note
-----
- dynamic sampling used for this statement
當索引字段答應為Null時,Oracle放棄此索引:SQL> alter table t modify (username null);Table altered.SQL> select /*+ index(t,i_t) */ * from t;Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873--------------------------------------------------------------------------
Id Operation Name Rows Bytes Cost (%CPU) Time
--------------------------------------------------------------------------
0 SELECT STATEMENT 27 918 2 (0) 00:00:01
1 TABLE ACCESS FULL T 27 918 2 (0) 00:00:01
--------------------------------------------------------------------------Note
-----
- dynamic sampling used for this statement 當該字段為Not Null時,索引可以被強制使用:SQL> alter table t modify (username not null);Table altered.SQL> select /*+ index(t,i_t) */ * from t;Execution Plan
----------------------------------------------------------
Plan hash value: 3593393735------------------------------------------------------------------------------------
Id Operation Name Rows Bytes Cost (%CPU) Time
------------------------------------------------------------------------------------
0 SELECT STATEMENT 27 918 2 (0) 00:00:01
1 TABLE ACCESS BY INDEX ROWID T 27 918 2 (0) 00:00:01
2 INDEX FULL SCAN I_T 27 1 (0) 00:00:01
------------------------------------------------------------------------------------Note
-----
- dynamic sampling used for this statement
這就是Null值對于索引及查詢的影響.原文地址:http://www.eygle.com/archives/2006/02/index_null_hints_explain.Html