1. 索引分析
--分析索引
analyze table 索引名;
--重构索引
alter index 索引名;
2. 索引失效
隐式转换导致索引失效.这一点应当引起重视.也是开发中经常会犯的错误.
由于表的字段tu_mdn定义为varchar2(20),但在查询时把该字段作为number类型?以where条件传给Oracle,这样会导致索引失效.
错误的例子:select from test where tu_mdn=13333333333;
正确的例子:select from test where tu_mdn=’13333333333’;对索引列进行运算导致索引失效,我所指的对索引列进行运算包括(+,-,,/,! 等)
错误的例子:select from test where id-1=9;
正确的例子:select * from test where id=10;- 使用Oracle内部函数导致索引失效.对于这样情况应当创建基于函数的索引.
错误的例子:select from test where round(id)=10; 说明,此时id的索引已经不起作用了
正确的例子:首先建立函数索引,create index test_id_fbi_idx on test(round(id));然后 select from test where round(id)=10; 这时函数索引起作用了 - 以下使用会使索引失效,应避免使用;
- 使用 <> 、not in 、not exist、!=
- like “%_” 百分号在前(可采用在建立索引时用reverse(columnName)这种方法处理)
- 单独引用复合索引里非第一位置的索引列。应总是使用索引的第一个列,如果索引是建立在多个列上, 只有在它的第一个列被where子句引用时,优化器才会选择使用该索引。
- 字符型字段为数字时在where条件里不添加引号.