对于一些大表创建索引,可以使用dbms_space.create_index_cost对索引占用的空间进行提前估算
SQL> exec dbms_stats.gather_table_stats(user,'T1227');
PL/SQL procedure successfully completed.
SQL> variable used_bytes number
SQL> variable alloc_bytes number
SQL> exec dbms_space.create_index_cost('create index t1227_index on t1227(object_name)',:used_bytes,:alloc_bytes );
PL/SQL procedure successfully completed.
SQL> print:used_bytes
USED_BYTES
----------
1797425
SQL> print:alloc_bytes
ALLOC_BYTES
-----------
3145728
测试在t1227表object_name字段建立索引,大约需要3145728个字节的存储空间(used_bytes官方文档中的描述The number of bytes representing the actual index data 这个变量我查不到资料是如何计算的和具体作用是什么,有了解的同学望不吝指教)
SQL> select table_name,column_name from dba_ind_columns where index_name='T1227_INDEX';
TABLE_NAME
------------------------------
COLUMN_NAME
----------------------------------------
T1227
OBJECT_NAME
SQL> select sum(bytes) from dba_segments where segment_name='T1227_INDEX';
SUM(BYTES)
----------
3145728
对比估算的空间需求和实际索引大小,结果是一致的。