您的位置:首页 > 数据库 > Oracle

浅谈oracle中重建索引_(ZT)

2010-04-08 14:20 169 查看
当我们创建索引时,oracle会为索引创建索引树,表和索引树通过rowid(伪列)来定位数据。当表里的数据发生更新时,oracle会自动维护索引树。但是在索引树中没有更新操作,只有删除和插入操作。
例如在某表id列上创建索引,某表id列上有值“101”,当我将“101”更新为“110”时,oracle同时会来更新索引树,但是oracle先将索引树中的“101”标示为删除(实际并未删除,只是标示一下),然后再将“110”写到索引树中。
如果表更新比较频繁,那么在索引中删除标示会越来越多,这时索引的查询效率必然降低,所以我们应该定期重建索引。来消除索引中这些删除标记。
一般不会选择先删除索引,然后再重新创建索引,而是rebuild索引。在rebuild期间,用户还可以使用原来的索引,并且rebuild新的索引时也会利用原来的索引信息,这样重建索引会块一些。

}‑r8e7{7~ m0这个实验来察看索引中的删除标记,并且如何重建索引。

试验环境:oracle 8.1.7

一、创建表、插入记录和创建索引ITPUB个人空间G d@K*C)e0J
SQL> create table ind (id number,name varchar2(100));
表已创建。
SQL> create or replace procedure sp_insert_ind
4L­J.U9Z Z.lq0 2 is
"Sh#g+w%qnJ,z0 3 beginITPUB个人空间 lK H Uu­}
4 for i in 1..10000 loop
)b1l.U‑Qa%oamE0 5 insert into ind values(i,to_char(i)||'aaaaaaaaaa');ITPUB个人空间+?[1]q ^ n
Q%B T
6 end loop;
}g9E3Z'_ C0 7 end;
B(@~;aY @;T~ Tz0 8 /

过程已创建。
SQL> exec sp_insert_ind
PL/SQL 过程已成功完成。

"]5/8F$[kJ­Ya0SQL> create index ind_id_idx on ind(id);
索引已创建。

二、收集索引信息
--收集信息,没有更新数据字典,所以没有信息
L p&R2{7O{#}~0SQL> select lf_rows,lf_rows_len,del_lf_rows,del_lf_rows_len from index_stats;

未选定行
ITPUB个人空间2b/J E6o/,S'M.Y`
--更新数据字典
HYiY ~0SQL> ANALYZE INDEX ind_id_idx VALIDATE STRUCTURE;

索引已分析
ITPUB个人空间#N&v^wP9}
--参数含义:ITPUB个人空间/KAJ{+g
--LF_ROWS Number of values currently in the indexITPUB个人空间;E.h h%wyj P-?
--LF_ROWS_LEN Sum in bytes of the length of all values
l G-AY*}0--DEL_LF_ROWS Number of values deleted from the index
&v7Gr#s-kqe0--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
W1~4V5i/LO!qt&i0---------- ----------- ----------- ---------------
Wep0j q0 10000 149801 0 0
ITPUB个人空间+i-F­} X Z,`4A u5Y
--察看索引中已经标示为删除的行除以总共的行的数量,目前为0
X o6Z$l:Y2h0SQL> SELECT (DEL_LF_ROWS_LEN/LF_ROWS_LEN) * 100 AS index_usage FROM index_stats;

INDEX_USAGE ITPUB个人空间)_w[1]t%@ s t
----------- ITPUB个人空间bv:T W m7G{H2|-e
0

三、更新索引,并且重新察看信息ITPUB个人空间W$P/O-u,} C5{%H
ITPUB个人空间n Z‑KW Vk hh'J
--更新表中1000行记录,这时会更新索引树ITPUB个人空间;d­y8](i%X&^8| G&Ln
SQL> update ind set id=id+1 where id>9000;
已更新1000行。

K e5U+K:U9go`Q0SQL> ANALYZE INDEX ind_id_idx VALIDATE STRUCTURE;
索引已分析
ITPUB个人空间,I A;p­W"^
--总共行的数量增加了1000行,并且标示为删除了1000行记录ITPUB个人空间 o*P,N GQ1c)J
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
9[­j qt{­Vy)p‑C0---------- ----------- ----------- ---------------
B c%m2J }0 11000 164792 1000 14990

3a nL qU~y‑X0--察看索引中已经标示为删除的行除以总共的行的数量,目前为 9.09631536,这个值如果查过20,肯定要重建索引了。
ps fSqQ
Q1@n0SQL> SELECT (DEL_LF_ROWS_LEN/LF_ROWS_LEN) * 100 AS index_usage FROM index_stats;

INDEX_USAGE
vG J)_7B e/l0-----------
2Rs].{6j+I09.09631536

四、重建索引
--重建索引
a c2sey9Lhik|0SQL> alter index ind_id_idx rebuild;
索引已更改。
SQL> select lf_rows,lf_rows_len,del_lf_rows,del_lf_rows_len from index_stats;
未选定行

---以下信息又基本回到从前
!U­` S-^8E1w*q4}0SQL> 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 ITPUB个人空间J'g"y+V }9J M
---------- ----------- ----------- ---------------
D A1Kn!B!Vd W O0 10000 149802 0 0

SQL> SELECT (DEL_LF_ROWS_LEN/LF_ROWS_LEN) * 100 AS index_usage FROM index_stats;
INDEX_USAGE ITPUB个人空间/OZj z1H;a/t2G
-----------
y­h#U"`)S];K0 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: