浅谈Oracle 重建索引

      当咱们建立索引时,oracle会为索引建立索引树,表和索引树经过rowid(伪列)来定位数据。当表里的数据发生更新时,oracle会自动维护索引树。可是在索引树中没有更新操做,只有删除和插入操做。 html

       例如在某表id列上建立索引,某表id列上有值“101”,当我将“101”更新为“110”时,oracle同时会来更新索引树,可是oracle先将索引树中的“101”标示为删除(实际并未删除,只是标示一下),而后再将“110”写到索引树中。 oracle

       若是表更新比较频繁,那么在索引中删除标示会愈来愈多,这时索引的查询效率必然下降,因此咱们应该按期重建索引。来消除索引中这些删除标记。 oop

       通常不会选择先删除索引,而后再从新建立索引,而是rebuild索引。在rebuild期间,用户还可使用原来的索引,而且rebuild新的索引时也会利用原来的索引信息,这样重建索引会块一些。 ui

 

这个实验来察看索引中的删除标记,而且如何重建索引。 spa

试验环境:oracle 8.1.7 3d

 

1、建立表、插入记录和建立索引 orm

 

SQL> create table ind (id number,name varchar2(100)); htm

表已建立。 索引

SQL> create or replace procedure sp_insert_ind get

  2 is 

  3 begin 

  4 for i in 1..10000 loop 

  5 insert into ind values(i,to_char(i)||'aaaaaaaaaa'); 

  6 end loop; 

  7 end; 

  8 / 

过程已建立。 

SQL> exec sp_insert_ind 

PL/SQL 过程已成功完成。 

 

SQL> create index ind_id_idx on ind(id); 

索引已建立。 

 

2、收集索引信息 

 

--收集信息,没有更新数据字典,因此没有信息 

SQL> select lf_rows,lf_rows_len,del_lf_rows,del_lf_rows_len from index_stats; 

未选定行 

 

--更新数据字典 

SQL> ANALYZE INDEX ind_id_idx VALIDATE STRUCTURE; 

索引已分析 

 

--参数含义: 

--LF_ROWS Number of values currently in the index 

--LF_ROWS_LEN Sum in bytes of the length of all values 

--DEL_LF_ROWS Number of values deleted from the index 

--DEL_LF_ROWS_LEN Length of all deleted values 

SQL> select lf_rows,lf_rows_len,del_lf_rows,del_lf_rows_len from index_stats; 

   LF_ROWS LF_ROWS_LEN DEL_LF_ROWS DEL_LF_ROWS_LEN 

---------- ----------- ----------- --------------- 

     10000 149801 0 0 

 

--察看索引中已经标示为删除的行除以总共的行的数量,目前为0 

SQL> SELECT (DEL_LF_ROWS_LEN/LF_ROWS_LEN) * 100 AS index_usage FROM index_stats; 

INDEX_USAGE 

----------- 

          0 

 

3、更新索引,而且从新察看信息 

 

--更新表中1000行记录,这时会更新索引树 

SQL> update ind set id=id+1 where id>9000; 

已更新1000行。 

 

SQL> ANALYZE INDEX ind_id_idx VALIDATE STRUCTURE; 

索引已分析 

 

--总共行的数量增长了1000行,而且标示为删除了1000行记录 

SQL> select lf_rows,lf_rows_len,del_lf_rows,del_lf_rows_len from index_stats; 

   LF_ROWS LF_ROWS_LEN DEL_LF_ROWS DEL_LF_ROWS_LEN 

---------- ----------- ----------- --------------- 

     11000 164792 1000 14990 

 

--察看索引中已经标示为删除的行除以总共的行的数量,目前为 9.09631536,这个值若是查过20,确定要重建索引了。 

SQL> SELECT (DEL_LF_ROWS_LEN/LF_ROWS_LEN) * 100 AS index_usage FROM index_stats; 

INDEX_USAGE 

----------- 

9.09631536 

 

4、重建索引 

 

--重建索引 

SQL> alter index ind_id_idx rebuild; 

索引已更改。 

SQL> select lf_rows,lf_rows_len,del_lf_rows,del_lf_rows_len from index_stats; 

未选定行 

 

---如下信息又基本回到从前 

SQL> ANALYZE INDEX ind_id_idx VALIDATE STRUCTURE; 

索引已分析 

SQL> select lf_rows,lf_rows_len,del_lf_rows,del_lf_rows_len from index_stats; 

   LF_ROWS LF_ROWS_LEN DEL_LF_ROWS DEL_LF_ROWS_LEN 

---------- ----------- ----------- --------------- 

     10000 149802 0 0 

SQL> SELECT (DEL_LF_ROWS_LEN/LF_ROWS_LEN) * 100 AS index_usage FROM index_stats; 

INDEX_USAGE 

----------- 

          0 

 

 如下来自百度文库:http://wenku.baidu.com/view/bdacc60603d8ce2f0066232c.html 提供者:Yin_sky

相关文章
相关标签/搜索