oracle索引碎片分析、空间重用和整理,对索引频繁的update,delete操作会产生index frag,影响索引效率,增加索引io。
对索引频繁的update,delete操作会产生index frag,,影响索引效率,增加索引io。
1、索引碎片分析
产生测试索引碎片:
scott @devcedb>select count(*) from obj;
count(*)
----------
124256
scott @devcedb>create index ind_obj_id on obj(object_id);
index created.
scott @devcedb>delete obj where rownum
49999 rows deleted.
scott @devcedb>commit;
commit complete.
索引碎片分析:
scott @devcedb>analyze index ind_obj_id validate structure;
index analyzed.
--注意一点,就是该命令有一个坏处,就是在运行过程中,会锁定整个表,从而阻塞其他session对表进行插入、更新和删除等操作。这是因为该命令的主要目的并不是用来填充index_stats视图的,其主要作用在于校验索引中的每个有效的索引条目都对应到表里的一行,同时表里的每一行数据在索引中都存在一个对应的索引条目。为了完成该目的,所以在运行过程中要锁定整个表,同时对于很大的表来说,运行该命令需要耗费非常多的时间。
scott @devcedb>select name,blocks,del_lf_rows_len,lf_rows_len,(del_lf_rows_len/lf_rows_len)*100,(del_lf_rows/lf_rows)*100 from index_stats;
name blocks del_lf_rows_len lf_rows_len (del_lf_rows_len/lf_rows_len)*100 (del_lf_rows/lf_rows)*100
------------------------------ ---------- --------------- ----------- --------------------------------- -------------------------
ind_obj_id 384 766085 1906952 40.1732713 40.2394062
索引碎片比率:(del_lf_rows_len/lf_rows_len)*100,如果百分比超过20%就说明索引碎片比率很高了。需要整理碎片。
索引和表数据是级联关系的,当删除表数据的时候,索引条目不会被自动删除,而是在该条目上打上一个删除(d)的标识位,具体后面会说明,索引的block数量是不会改变的,空叶块不会被删除。所以当index fast full scan和index full scan的时候,这些空索引块也会被加载到内存中,增加了io。索引空叶块多,极大影响了索引扫描的效率。否则索引碎片对效率的影响不是很大。如下:
scott @devcedb>select object_id from obj where object_id
41377 rows selected.
elapsed: 00:00:01.13
execution plan
----------------------------------------------------------
plan hash value: 2777403740
-----------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
-----------------------------------------------------------------------------------
| 0 | select statement | | 55341 | 702k| 79 (2)| 00:00:01 |
|* 1 | index fast full scan| ind_obj_id | 55341 | 702k| 79 (2)| 00:00:01 |
-----------------------------------------------------------------------------------
sys as sysdba@devcedb>select count(*) from x$bh where obj='102822';
count(*)
---------- ---从x$bh查询缓存blocks,要用dba_objects.data_object_id
268 ---该索引加载到buffer pool的数据块数,该索引分配有384 blocks,266 leaf_blocks
那么索引空块会不会被重用呢?下面测试说明:
scott @devcedb>select count(*) from obj2;
count(*)
----------
62144
scott @devcedb>create unique index uni_ind_obj2_id on obj2(id);
index created. --该索引分配有256 blocks,130 leaf_blocks
scott @devcedb>delete obj2 where rownum
15536 rows deleted.
scott @devcedb>commit;
commit complete.
scott @devcedb>analyze index uni_ind_obj2_id validate structure;
index analyzed.
scott @devcedb>select name,blocks,del_lf_rows_len,lf_rows_len,(del_lf_rows_len/lf_rows_len)*100,(del_lf_rows/lf_rows)*100 from index_stats;
name blocks del_lf_rows_len lf_rows_len (del_lf_rows_len/lf_rows_len)*100 (del_lf_rows/lf_rows)*100
------------------------------ ---------- --------------- ----------- --------------------------------- -------------------------
uni_ind_obj2_id 256 240063 938727 25.5732497 25.5732626
scott @devcedb>insert into obj2 select seq_obj2.nextval id,a.* from dba_objects a;
15537 rows created.
scott @devcedb>commit;
commit complete.
scott @devcedb>analyze index uni_ind_obj2_id validate structure;
index analyzed.
scott @devcedb>select name,blocks,del_lf_rows_len,lf_rows_len,(del_lf_rows_len/lf_rows_len)*100,(del_lf_rows/lf_rows)*100 from index_stats;
name blocks del_lf_rows_len lf_rows_len (del_lf_rows_len/lf_rows_len)*100 (del_lf_rows/lf_rows)*100
------------------------------ ---------- --------------- ----------- --------------------------------- -------------------------
uni_ind_obj2_id 256 7180 938727 .764865611 .764882473
sys as sysdba@devcedb>select index_name,leaf_blocks,blevel from dba_indexes where index_name=upper('uni_ind_obj2_id');
index_name leaf_blocks blevel
------------------------------ ----------- ----------
uni_ind_obj2_id 130 1
这里我们看到,索引的碎片降低了,而且leaf_blocks的数量没有增加,说明空叶块被重用了。
当删除表里的一条记录时,其对应于索引里的索引条目并不会被物理的删除,只是做了一个删除标记(这可以通过dump 索引数据块alter system dump datafile # block #;可以看到类似”row#0[443] flag: ---d-, lock: 2“。)当一个空叶块被重用的时候,当第一条数据插入该索引叶块之前,oracle清空该空叶块上所有打上d标识位的索引条目,然后重用该索引块。如下:
scott @devcedb>select name,blocks,del_lf_rows_len,lf_rows_len,(del_lf_rows_len/lf_rows_len)*100,(del_lf_rows/lf_rows)*100,used_space,btree_space,pct_used from index_stats;
name blocks del_lf_rows_len lf_rows_len (del_lf_rows_len/lf_rows_len)*100 (del_lf_rows/lf_rows)*100 used_space btree_space pct_used
------------------------------ ---------- --------------- ----------- --------------------------------- ------------------------- ---------- ----------- ----------
ind_obj_id 256 307878 835601 36.8450971 36.8625788 837792 1279392 66
scott @devcedb>insert into obj select a.* from dba_objects a where rownum
4 rows created.
scott @devcedb>commit;
commit complete.
scott @devcedb>analyze index ind_obj_id validate structure;
index analyzed.
scott @devcedb>select name,blocks,del_lf_rows_len,lf_rows_len,(del_lf_rows_len/lf_rows_len)*100,(del_lf_rows/lf_rows)*100,used_space,btree_space,pct_used from index_stats;
name blocks del_lf_rows_len lf_rows_len (del_lf_rows_len/lf_rows_len)*100 (del_lf_rows/lf_rows)*100 used_space btree_space pct_used
------------------------------ ---------- --------------- ----------- --------------------------------- ------------------------- ---------- ----------- ----------
ind_obj_id 256 306312 834091 36.7240505 36.7303962 836282 1279392 66
我们关注下used_space和btree_space
used_space--total space that is currently being used in the b-tree
btree_space --total space currently allocated in the b-tree
重新分析该索引后我们注意到used_space反而降低了,btree_space不变(当然我们也可以看到lf_rows减少了),在这4条数据重用一个空索引块后,释放的空间大于使用的空间,该空叶块被重用了。
半空叶块是如何重用的呢?
我们构想一下,一个有两个叶块的index,第一个叶块包含1到10(不包含6),第二个叶块11到20,这时候我们删除表中2,4,11的数据,分析下索引后,分三种情况:1)插入键之前删除的键值,插入2,索引叶块1标识为d的两条索引记录会被清空,重新插入键值为2的记录,索引空间被重用,btree_space不变,used_space降低,lf_rows减1。2)插入一个属于原来被删除键值范围内的值,插入6,我们会发现和情况1相同。3)插入比之前键值更大的值,假定叶块2空间满了,会新增一个叶块。
所以说经常被删除或更新index键值,以后几乎不再会被插入时,空间的重用率很低,碎片产生的就越快。