05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

482<br />

CHAPTER 11 ■ INDEXES<br />

Myth: Space Is Never Reused in an Index<br />

This is a myth that I would like to dispel once <strong>and</strong> for all: space is reused in an index. The myth<br />

goes like this: you have a table, T, in which there is a column, X. At some point, you put the<br />

value X=5in the table. Later you delete it. The myth is that the space used by X=5will not be<br />

reused unless you put X=5back into the index later. The myth states that once an index slot is<br />

used, it will be there forever <strong>and</strong> can be reused only by the same value. A corollary to this is the<br />

myth that free space is never returned to the index structure, <strong>and</strong> a block will never be reused.<br />

Again, this is simply not true.<br />

The first part of the myth is trivial to disprove. All we need to do is to create a table<br />

like this:<br />

ops$tkyte@ORA10GR1> create table t ( x int, constraint t_pk primary key(x) );<br />

Table created.<br />

ops$tkyte@ORA10GR1> insert into t values (1);<br />

1 row created.<br />

ops$tkyte@ORA10GR1> insert into t values (2);<br />

1 row created.<br />

ops$tkyte@ORA10GR1> insert into t values (9999999999);<br />

1 row created.<br />

ops$tkyte@ORA10GR1> analyze index t_pk validate structure;<br />

Index analyzed.<br />

ops$tkyte@ORA10GR1> select lf_blks, br_blks, btree_space<br />

2 from index_stats;<br />

LF_BLKS BR_BLKS BTREE_SPACE<br />

---------- ---------- -----------<br />

1 0 7996<br />

So, according to the myth, if I delete from T where X=2, that space will never be reused<br />

unless I reinsert the number 2. Currently, this index is using one leaf block of space. If the<br />

index key entries are never reused upon deletion, <strong>and</strong> I keep inserting <strong>and</strong> deleting <strong>and</strong> never<br />

reuse a value, this index should grow like crazy. Let’s see:<br />

ops$tkyte@ORA10GR1> begin<br />

2 for i in 2 .. 999999<br />

3 loop<br />

4 delete from t where x = i;<br />

5 commit;<br />

6 insert into t values (i+1);<br />

7 commit;<br />

8 end loop;<br />

9 end;<br />

10 /

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!