shrink 與rebuild對索引高度的影響對比

beatony發表於2012-08-07

shrink 與rebuild對索引高度的影響對比

 

測試思路:
建立一個測試表t1,並建立索引,插入資料後,再將資料刪除,然後對錶和索引進行shrink操作,觀察索引高度是否發生變化:

SQL> create table t1 as select * from dba_objects;

Table created.

SQL> create index idx_t1 on t1(object_id,object_name) pctfree 90 tablespace idx_2k;

Index created.

SQL> analyze index idx_t1 validate structure;

Index analyzed.

SQL> select name,height,lf_blks,lf_rows,br_blks,br_rows from index_stats;

NAME HEIGHT LF_BLKS LF_ROWS BR_BLKS BR_ROWS
-------------------------------------------------- ---------- ---------- ---------- ---------- ----------
IDX_T1 3 9552 9552 57 9551

SQL> select count(*) from t1;

COUNT(*)
----------
9552

SQL> delete from t1;

9552 rows deleted.

SQL> commit;

Commit complete.

SQL> analyze index idx_t1 validate structure;

Index analyzed.

SQL> select name,height,lf_blks,lf_rows,br_blks,br_rows from index_stats;

NAME HEIGHT LF_BLKS LF_ROWS BR_BLKS BR_ROWS
-------------------------------------------------- ---------- ---------- ---------- ---------- ----------
IDX_T1 3 9552 1608 57 9551

SQL> alter table t1 enable row movement;

Table altered.

----不加cascade,只對表shrink,索引的高度,leaf block等不變。

SQL> alter table t1 shrink space;

Table altered.

SQL> analyze index idx_t1 validate structure;

Index analyzed.

SQL> select name,height,lf_blks,lf_rows,br_blks,br_rows from index_stats;

NAME HEIGHT LF_BLKS LF_ROWS BR_BLKS BR_ROWS
-------------------------------------------------- ---------- ---------- ---------- ---------- ----------
IDX_T1 3 9552 1608 57 9551

----加cascade,對索引shrink,索引高度不變,leaf block,branch block減少,空間被回收,但維持高度不變

SQL> alter table t1 shrink space cascade;

Table altered.

SQL> analyze index idx_t1 validate structure;

Index analyzed.

SQL> select name,height,lf_blks,lf_rows,br_blks,br_rows from index_stats;

NAME HEIGHT LF_BLKS LF_ROWS BR_BLKS BR_ROWS
-------------------------------------------------- ---------- ---------- ---------- ---------- ----------
IDX_T1 3 1 1 2 0

----rebuild索引,高度降低。

SQL> alter index idx_t1 rebuild;

Index altered.

SQL> analyze index idx_t1 validate structure;

Index analyzed.

SQL> select name,height,lf_blks,lf_rows,br_blks,br_rows from index_stats;

NAME HEIGHT LF_BLKS LF_ROWS BR_BLKS BR_ROWS
-------------------------------------------------- ---------- ---------- ---------- ---------- ----------
IDX_T1 1 1 0 0 0

因此,相對於rebuild來說,在降低高水位的同時,shrink操作對索引的影響較小,沒有降低索引高度,避免了root block分裂帶來的風險。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22578826/viewspace-740171/,如需轉載,請註明出處,否則將追究法律責任。

相關文章