| http://www.sqlsky.com/ |
|
|
由于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 SQL> create index i_t on t(username); Index created. SQL> set autotrace trace explain Execution Plan -------------------------------------------------------------------------- Predicate Information (identified by operation id): 1 - filter("USERNAME"='EYGLE')Note SQL> set linesize 120 Execution Plan ------------------------------------------------------------------------------------ Predicate Information (identified by operation id): 2 - access("USERNAME"='EYGLE')Note |
当索引字段允许为Null时,Oracle放弃此索引:
SQL> alter table t modify (username null); Table altered. SQL> select /*+ index(t,i_t) */ * from t; Execution Plan -------------------------------------------------------------------------- Note |
当该字段为Not Null时,索引可以被强制使用:
SQL> alter table t modify (username not null); Table altered. SQL> select /*+ index(t,i_t) */ * from t; Execution Plan ------------------------------------------------------------------------------------ Note |
这就是Null值对于索引及查询的影响.
原文地址:http://www.eygle.com/archives/2006/02/index_null_hints_explain.html