日期:2014-05-16  浏览次数:20507 次

深入理解Oracle索引(5):反向索引的定义、缺点和适用场景
       ㈠ 定义
          
         
立一个反向索引将把每个列的键值(each column key value)按字节反向过来,对于组合键,列的顺序被保留,但每个列的字节都作了反向
          例如:
          表的某一列内容
          ……
          1234
          1235
          1236
          1237
          ……
          
          建立正向索引
          ……
          1234
          1235
          1236
          1237
          ……
          这四行放在同一个leaf block中。
          如果事务A查询1234这行,同时事务B查询1235这行。那么就会在这个leaf block上发生I/O争用
          
          
          建立反向索引
          ……
          4321
          5321
          6321
          7321
          ……
          这四行放在四个不同leaf block中
          如果事务A查询1234这行,同时事务B查询1235这行。是分别在两个leaf block上进行,不会发生I/O争用
          
          很多事务访问同一个块,对同一个块并发操作产生的I/0竞争
          反向索引能作为避免热点块的一个方法
          
       ㈡ 查找
          

          user_indexes.index_type


scott@ORCL> create index idx_rev on emp(sal) reverse;

Index created.

scott@ORCL> select index_name,index_type from user_indexes where index_name='IDX_REV';

INDEX_NAME                     INDEX_TYPE
------------------------------ ---------------------------
IDX_REV                        NORMAL/REV


       ㈢ 它有什么缺点?
          
          ① if you use reverse key index,index range scan will not work
          ② 当应用需要获取一段范围的数据时,reverse key index将不会被使用,因为键值不是连续的排列的。在这种情况下,CBO将会选择全表扫描
          
          测试:

hr@ORCL> drop table t purge;

Table dropped.

hr@ORCL> create table t (a number,b varchar2(20));

Table created.

hr@ORCL> ed   
Wrote file afiedt.buf

  1  begin
  2    for i in 1..20000
  3    loop
  4      insert into t values(i,to_char(sysdate,'yyyymmddhhmmss'));
  5      commit;
  6    end loop;
  7* end;
hr@ORCL> /

PL/SQL procedure successfully completed.

hr@ORCL> create index idx_t on t (a) reverse;

Index created.

hr@ORCL> set autot on exp

hr@ORCL> select * from t where a >=19989 and a <=19990;

         A B
---------- --------------------
     19989 20130224060219
     19990 20130224060219


Execution Plan
----------------------
Plan hash value