索引的增删改
1、创建索引:语法 create [unique] index index_name on table_name(column_name,olumn_name1,...)
解析:其中 unique:表示索引上的值必须是唯一的,称为唯一索引;
index_name :要创建的索引名;table_name:指定要为哪个表创建索引;
cloumn_name:表示对某列创建索引,我们也可以对多列创建索引;这种索引称为组合索引。
2、修改索引:语法alter [unique] index table_name rebuild 。
3、删除索引:语法DROP INDEX [schema.]indexname。
总结:
1、判断是否需要重建索引:
SQL>analyze index index_name validate structure;
SQL> select height,DEL_LF_ROWS/LF_ROWS from index_stats;
( 或 Select index_name,blevel from dba_indexes where blevel>=4 );
说明 : 当查询出来的 height>=4 或者 DEL_LF_ROWS/LF_ROWS>0.2 的场合 , 该索引考虑重建 ;
2 、重建索引方法 :
方法一、
Alter index index_name rebuild tablespace tablespace_name;
优点:是快速重建索引的一种有效的办法,可以用来将一个索引移到新的表空间。
缺点:重建期间需要额外空间。
方法二、
alter index index_name coalesce;
优点:重建期间不需要额外空间。
缺点:coalesce选项不能用来将一个索引转移到其他表空间。
索引失效的原因:
1. 由于表的字段tu_mdn定义为varchar2(20),但在查询时把该字段作为number类型以where条件传给Oracle,这样会导致索引失效。
2. 对索引列进行运算导致索引失效,我所指的对索引列进行运算包括(+,-,*,/,! 等)。
错误的例子:select * from test where id-1=9;
正确的例子:select * from test where id=10;
3. 使用 oracle内部函数导致索引失效。
错误的例子:select * from test where round(id)=10;
正确的例子:创建索引的时候就加入函数;create index test_id on test (round(id));然后 select * from test where round(id)=10; 就正确了。
4. 使用<>、not in 、 not exits查询的结果集大于15%就不会走索引。
5. 单独的使用>、<或者 like '%_'百分号在前面。
6. 单独引用复合索引里非第一位置的索引列。
7. 当变量采用的是times变量,而表的字段采用的是date变量时.或相反情况。