2007年8月6日星期一

《Expert one on one Oracle》- 索引- 笔记-2

位图索引(略)

基于函数的索引(略)

使用索引常见问题:

a. 能否在视图上创建索引?只能在视图所基于的基表上创建。

b. B树索引不存储完全为NULL的条目,对于索引至少其中有一列定义为NOT NULL的时候,查询才能使用索引。

对于表T1,create table t1( x int, y int),创建索引create unique index t_inx(x,y)之后,如果执行select * from t1 where x is null,执行计划显示不会利用索引。
如果对于表T2,create table t1( x int, y int not null),创建索引create unique index t_inx(x,y)之后,如果执行select * from t2 where x is null,执行计划显示不会利用索引。

c. 为何不使用索引

查询的列超出索引的列的范围。例如,在T(x,y)上创建索引,而实际的查询为Select x, y, z from t where x = 5;那么,由于查询的z列必须访问数据块才能得到,这种情况下,有可能不使用索引而效率更高。
索引的列包含NULL值。执行Select count(*) from T查询,在索引表上建有B树索引。对于NULL值,不在索引中记录,所以不能通过索引来计算count。而会通过全表扫描的方式。
列上建有索引,但是查询的时候在列上使用了函数。例如:select * from t where f(indexed_column)=value
错误的使用条件。例如,对于建有索引的字符列,这列中只包含数字,使用如下查询将不会使用索引,select * from t where indexed_column = 5,将会被转换成select * from t where to_number(indexed_column) = 5。还有,对于这种TRUNC(DATE_COL) = TRUNC(SYSDATE)条件,改写成date_col between trunc(sysdate) and trunc(sysdate)+1‐1/(1*24*60*60)。
表未分析,或者统计数据错误。

没有评论: