ORACLE索引

索引的增删改

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,这样会导致索引失效。

      错误的例子:select * from test where tu_mdn=13333333333;
      正确的例子:select * from test where tu_mdn='13333333333';

 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变量时.或相反情况。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值