alter index coalesce和alter index rebuild的区别:
alter index coalesce是合并同一branch的leaf block。
而alter index rebuild是重新建立一个indexes,一般需要2倍的index大小的空间。而且需要排序。
以下是证明alter index coalesce的过程:
×××××××× ×××××××××××××××××alter index coalesce××××××××××××××××××××××
SQL>
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle9i Enterprise Edition Release 9.2.0.6.0 - 64bit Production
PL/SQL Release 9.2.0.6.0 - Production
CORE 9.2.0.6.0 Production
TNS for Solaris: Version 9.2.0.6.0 - Production
NLSRTL Version 9.2.0.6.0 - Production
SQL> drop table t2;
Table dropped.
SQL> create table t2(col1 number);
Table created.
SQL> create unique index t2_inx on t2(col1);
Index created.
SQL> alter table t2 add constraint t2_pk primary key(col1);
Table altered.
SQL> begin
2 for i in 500001..601000 loop
3 insert into t2 values(i);
4 end loop;
5 end;
6 /
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
SQL> begin
2 sys.dbms_stats.gather_table_stats('SYSTEM', 'T2', ESTIMATE_PERCENT => 20, c
ascade=> true);
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> col index_name for a10;
SQL> select index_name, blevel, leaf_blockS, distinct_keys
2 clustering_factor, num_rows
3 from user_indexes
4 where table_name LIKE 'T2%';
INDEX_NAME BLEVEL LEAF_BLOCKS CLUSTERING_FACTOR NUM_ROWS
---------- ---------- ----------- ----------------- ----------
T2_INX 1 190 101000 101000
SQL> declare
2 cursor mm is select col1 from t2;
3 begin
4 for cur_1 in mm loop
5 if mod(cur_1.col1, 2) = 0 then
6 delete from t2 where col1=cur_1.col1;
7 end if;
8 end loop;
9 commit;
10 end;
11 /
PL/SQL procedure successfully completed.
SQL> begin
2 sys.dbms_stats.gather_table_stats('SYSTEM', 'T2', ESTIMATE_PERCENT => 20, c
ascade=> true);
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> select index_name, blevel, leaf_blockS, distinct_keys
2 clustering_factor, num_rows
3 from user_indexes
4 where table_name LIKE 'T2%';
INDEX_NAME BLEVEL LEAF_BLOCKS CLUSTERING_FACTOR NUM_ROWS
---------- ---------- ----------- ----------------- ----------
T2_INX 1 190 50500 50500
SQL> alter index t2_inx coalesce;
Index altered.
SQL> begin
2 sys.dbms_stats.gather_table_stats('SYSTEM', 'T2', ESTIMATE_PERCENT => 20, c
ascade=> true);
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> select index_name, blevel, leaf_blockS, distinct_keys
2 clustering_factor, num_rows
3 from user_indexes
4 where table_name LIKE 'T2%';
INDEX_NAME BLEVEL LEAF_BLOCKS CLUSTERING_FACTOR NUM_ROWS
---------- ---------- ----------- ----------------- ----------
T2_INX 1 189 50500 50500
SQL> declare
2 cursor mm is select col1 from t2;
3 begin
4 for cur_1 in mm loop
5 if mod(cur_1.col1, 3) = 0 then
6 delete from t2 where col1=cur_1.col1;
7 end if;
8 end loop;
9 commit;
10 end;
11 /
PL/SQL procedure successfully completed.
SQL> alter index t2_inx coalesce;
Index altered.
SQL> begin
2 sys.dbms_stats.gather_table_stats('SYSTEM', 'T2', ESTIMATE_PERCENT => 20, c
ascade=> true);
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> select index_name, blevel, leaf_blockS, distinct_keys
2 clustering_factor, num_rows
3 from user_indexes
4 where table_name LIKE 'T2%';
INDEX_NAME BLEVEL LEAF_BLOCKS CLUSTERING_FACTOR NUM_ROWS
---------- ---------- ----------- ----------------- ----------
T2_INX 1 95 33666 33666
SQL>
×××××××× ×××××××××××××××××alter index coalesce××××××××××××××××××××××