索引与Null值对Hints及执行计划的影响
添加时间: 2008-4-23 22:57:05 作者: Oracle指导 阅读次数:4 来源: http://www.d9soft.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值对于索引及查询的影响.
上下文章:
上一篇文章: dbv能用来检查日志文件吗 下一篇文章: 用Oracle和Python武装你的头脑(4)
相关文章:

