Skip to Main Content
  • Questions
  • Index size got doubled after rebuild

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Sathish .

Asked: March 06, 2018 - 5:06 am UTC

Last updated: March 06, 2018 - 3:37 pm UTC

Version: 12C

Viewed 1000+ times

You Asked

Hi,

We did index rebuild for the partition table. Index is also partitioned one. Before rebuilding index, the size of the index was 170 GB. But after rebuilding the size got doubled and it was 327GB.

Used below command:

alter index index_name rebuild partition partition_name online parallel 4;

Why the size got doubled? Are we doing any mistakes over here? Please let me know how to resolve this?

Thanks!

and Chris said...

There's no guarantee that rebuilding an index will make it smaller. And it's easy to construct a demo where rebuilding an index makes it larger:

create table t (
  pk not null, stuff not null,
  constraint pk primary key ( pk )
) as
with rws as (
  select level x from dual
  connect by level <= 1000
)
  select rownum pk, lpad('x', 100, 'x') stuff  
  from   rws cross join rws;

exec dbms_stats.gather_table_stats(user, 'T');

select ui.leaf_blocks , us.bytes
from   user_indexes ui
join   user_segments us
on     ui.index_name = us.segment_name
where  ui.index_name = 'PK';

LEAF_BLOCKS   BYTES      
         2088   17825792 

alter session enable parallel ddl;
alter index pk rebuild online parallel 4;

exec dbms_stats.gather_table_stats(user, 'T');

select ui.leaf_blocks , us.bytes
from   user_indexes ui
join   user_segments us
on     ui.index_name = us.segment_name
where  ui.index_name = 'PK';

LEAF_BLOCKS   BYTES      
         2090   17891328 


Richard Foote discusses this in more detail at:

https://richardfoote.wordpress.com/2009/01/13/how-to-rebuild-and-make-an-index-bigger-not-smaller-carry-that-weight/

So the mistake is hoping a rebuild will make the index smaller!

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Index on monotonically increasing values

Rajeshwaran Jeyabal, March 07, 2018 - 11:38 am UTC

The index may have key columns (in the leading portion) that have monotonically increasing values (say like sequence.nextval or sysdate/systimestamp).

Since those monotonically increasing values will always keep on increasing and naturally will have very perfect clustering of data in the leaf blocks.

Have a table with one index on the ID column.

demo@ORA12C> create table t as
  2  select a.*, rownum as id
  3  from all_objects a
  4  where 1 = 0 ;

Table created.

demo@ORA12C> create index t_idx on t(id) ;

Index created.

demo@ORA12C> insert into t
  2  select a.*, rownum
  3  from all_objects a ,
  4      all_users b
  5  where rownum <=1e6;

1000000 rows created.


demo@ORA12C> analyze index t_idx validate structure;

Index analyzed.

demo@ORA12C> select blocks,lf_blks,pct_used from index_stats;

    BLOCKS    LF_BLKS   PCT_USED
---------- ---------- ----------
      2176       1999        100

The index has around 2000 leaf blocks and each leaf block is used to its maximum utilization.

Now let’s rebuild this index and see what happens.

demo@ORA12C> alter index t_idx rebuild nologging;

Index altered.

demo@ORA12C> analyze index t_idx validate structure;

Index analyzed.

demo@ORA12C> select blocks,lf_blks,pct_used from index_stats;

    BLOCKS    LF_BLKS   PCT_USED
---------- ---------- ----------
      2304       2226         90


The index got increased in size by adding 225 more leaf blocks to its structure.

However rebuilding those index with PCTFREE 0, we are back to square one.

demo@ORA12C> alter index t_idx rebuild nologging PCTFREE 0;

Index altered.

demo@ORA12C> analyze index t_idx validate structure;

Index analyzed.

demo@ORA12C> select blocks,lf_blks,pct_used from index_stats;

    BLOCKS    LF_BLKS   PCT_USED
---------- ---------- ----------
      2048       1999        100

More to Explore

Design

New to good database design? Check out Chris Saxon's full fundamentals class.