Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, atul.

Asked: May 04, 2002 - 10:40 pm UTC

Last updated: September 25, 2008 - 3:34 pm UTC

Version: 8.0.4

Viewed 1000+ times

You Asked

Sir,

I know we can easily rebuild nonunique indexes but i want to know can we rebuild primary key indexes or we have to drop and recreate those
indexes?

Thanks.
atul

and Tom said...

You can rebuild them as well -- BUT YOU RARELY, IF EVER NEED TO.

rebuilding indexes is 99% a waste of time. You should work to find the 1% that might sometime need it and ignore the rest.



Rating

  (20 ratings)

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

Comments

why it is a waste of time

A reader, May 05, 2002 - 2:39 pm UTC

Hi

Why rebulding index is a waste of time? If we have a index with level 6 instead of 3 dont we have to perform more I/O? I from Oracle Documentation that rebulding index is recommended but I see the opposite here :o

Tom Kyte
May 05, 2002 - 6:03 pm UTC

When is the last time you saw one with a level of 6? (and did it go down to 3..) Rebuilding an index is only recommended when it NEEDS IT. My point is, I see people BLINDLY rebuidling ALL indexes every day, week, month -- whatever. Just an utter waste of time for most (might even be counter productive!).

Find the 1% that you need to rebuild and do them when they need to. Not as a part of a standard operating procedure (time to rebuld all of the indexes.. what a waste)



agree

A reader, May 05, 2002 - 7:21 pm UTC

I agree, i thought you mean rebuilding index is a waste of time, i dont do it everyday anyway just when I see the level becomes 4 upwards

Is it true for Bit map indexes too

pawan, May 05, 2002 - 11:03 pm UTC

From the various answers in this forum and also after testing it myself i agree that rebuilding indexes is "mostly" a waste of time. However, recently one of our production jobs which used to take approximately 40 minutes to complete did not complete in 10 hours and after lot of playing around and finally rebuilding the BIT map index we had on the Table key the process finally started completing in reasonable amount of time. So my question is - Does the condition in rebuilding Bit map and B tree indexes differ

Tom Kyte
May 06, 2002 - 7:16 am UTC

Bitmaps are a little different -- they are seriously affected by incremental loads and in general stand to gain from a rebuild after some incremental loads.

When to rebuild

sam, December 23, 2002 - 11:02 am UTC

Tom,

In one of your response you said.

You will rebuild an index in response to IDENTIFIED
and DEGRADED performance -- period. You will not rebulid indexes based on a schedule. Period.

Is there any statistical way to know the response is bad, since response is very relative term. What I mean is, is there a script which can tell me potential problem related to Indexes??

Thanks


Tom Kyte
December 23, 2002 - 11:19 am UTC

watch statspack - look at the top sql, especially by buffer gets. If one starts creeping up there over time, could be an index that needs looking at.

dbms_stats.gather_table_stats after rebuild a table -- 9.2.0.3

Baqir Hussain, May 09, 2003 - 6:55 pm UTC

Tom,
I plan to rebuild a table as follow in 9.2.0.3:

* rename table
* drop constraint
* create table
* insert from rnamed table
* drop indexes
* recreate indexes
* put back constraint

When running the following script, show no entries in either of the columns:

SELECT INDEX_NAME "NAME", NUM_ROWS, DISTINCT_KEYS "DISTINCT",
LEAF_BLOCKS, CLUSTERING_FACTOR "CF", BLEVEL "LEVEL",
AVG_LEAF_BLOCKS_PER_KEY "ALFBPKEY"
FROM DBA_INDEXES
WHERE owner = 'ORACLE' and table_name = 'FOO'
ORDER BY INDEX_NAME

My questions are :

1. After rebuilding a table do I have to run dbms_stats.gather_table_stats on this table??
-- (even though the sql is executing very well without running dbms_stats on this table in a test environment)
2. Do I have to run dbms_stats.gather_table_stats every time when a new index is created for a table?

Apprecaite your response in advance

Thanks


Tom Kyte
May 09, 2003 - 7:16 pm UTC

1) if you need statistics, of course. is the original table having stats? then this one should.

2) no, you can gather index stats.

A reader, May 09, 2003 - 7:25 pm UTC

>>When is the last time you saw one with a level of 6? (and did it go down to 3..)

How to find out the Level? Any query or info?

Tom Kyte
May 10, 2003 - 7:30 am UTC

look in user_indexes after you analyze it.

Rebuilding or Coalesce

Vivek Sharma, February 03, 2004 - 12:05 pm UTC

Dear Tom,

I have gained a very good knowledge from this site and have leanrt better that Rebuilding is wastage of time. Even I have stopped rebuilding and rather coalesce them. I have also seen a performance gain after coalescing.

But Tom, one of our Oracle Consultant visited our firm and after checking the Database insists that the Indexes are fragmented and hence rebuild the indexes. He says that since the Indexed Columns are frequently updated the fragmented has increased and rebuilding can improve the performance. I have argued with his findings that it will be just wastage of time and may degrade the performance. The amount of redo generated next day will be significabtly higher but my management wants to give it a try and wants me to rebuild the indexes.

What do you suggests ? Do you think that if a indexed columns are frequently updated it can cause fragmentation issue. But can it not be resolved by Coalescing ?

I will appreciate your early suggestions on it.

Thanks and Regards
Vivek



Tom Kyte
February 03, 2004 - 1:54 pm UTC

I never say always -- rebuilding CAN be beneficial in some extreme cases. for B*trees it is rare rare rare. For bitmaps and text indexes -- less rare (i rebuild my text index on asktom every now and again -- when I notice the estimated counts getting way off)

collect statspack for a couple of days before the rebuild. do this during peak and make sure you see queries that would be affected by this rebuild.

then, do it again after the rebuild.

Now, compare the two and have the consultant show how much improvement they've provided.

Be really interesting to make their fee contigent on a positive result :)


Ask them for the metrics they've used to prove the indexes are "fragmented" -- being really interesting to see their thought process as well.

REbuilding of index or recreate which is benefitial?

nn, April 16, 2004 - 2:02 am UTC

My Query is regarding should we go rebuild or recreate in case of DB Reorganisation

Tom Kyte
April 16, 2004 - 7:30 am UTC

rebuild is better

a rebuild can read the exist index (avoid a sort)

a rebuild does not drop and create (two step), it is a single command. Hence, you will not accidently "lose" an index during a reorg (as happens frequently)

a rebuild does require about 2x the space (both indexes exist).


And then there is coalesce which uses the existing index, frees up space in leaf blocks, is a single command, and works in place as well.... (no 2x)

Coalescing of Indexes

Vivek Sharma, July 23, 2004 - 6:06 am UTC

Dear Tom,

Oracle 9i Documentation
</code> http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96521/indexes.htm <code>

says about Coalescing of Indexes that

"In situations where you have B-tree index leaf blocks that can be freed up for reuse, you can merge those leaf blocks using the following statement". They have give a Illustrative diagram in which two blocks which are 50% full are merged into 1 (assuming pctfree 0).

My question is, How do I find out the situation of Indexes where there is a scope of freeing up of leaf Blocks ?

Thanks and Regards

Tom Kyte
July 23, 2004 - 8:57 am UTC

you can analyze the index to get average space utilization - but averages won't be a really "good thing" over all.

you sort of want to know "how is the index used". for example

a) index is on a column populated by a sequence.
b) sequence only increases
c) we delete 99% of the data from the low value to the high value over time (eg: sort of a queue table)

so at one point you had the values 1..1000 in the table -- over time you delete 99% of that leave 1, 101, 201, ... 901.

That would be a good candidate for a coalesce due to the nature of the way you use it. the left hand side would compact nicely -- the right hand side (still dense, have not processed rows 1001..100000000 yet) won't be touched (read but not modified).

Averages would not tell you this however, the average space would be "very well packed". Your knowledge would be the thing that tells you.




In your openion what are the 1%

Khalid Al-Mansour, December 12, 2006 - 7:04 am UTC

Dear Tom you said
rebuilding indexes is 99% a waste of time. You should work to find the 1% that
might sometime need it and ignore the rest.

in your openion what are the causes of 1%?
Regards,
Khalid.

Tom Kyte
December 12, 2006 - 7:16 am UTC

Automatic index rebuilding

Karteek, February 11, 2007 - 12:56 pm UTC

Does Index build automatically as we insert records into...?.

Suppose...

-we have 2 million records initially
-created index on this above table
-we insert around 100K to 200K records every month
-do we need to rebuild this index after we insert the data or -oracle automatically does that timely..?

Best Regards,
Karteek
Tom Kyte
February 12, 2007 - 10:30 am UTC

the index, given you just insert, will almost certainly be just fine - forever.

presuming:

it is a b*tree index, not a bitmap index
OR
it is a bitmap index and you direct path load the 100/200k records in bulk, not slow by slow (row by row) in conventional path inserts.

rebuild b*tree indexes,

A reader, February 15, 2007 - 10:54 am UTC

Say I have an index on a table that has 1 million rows. The number of leaf blocks is around 100,000.

Now I delete 25% of the data from the table. The entries in the leaf blocks too will be deleted. In that case may be some leaf blocks will not have any entries at all or some may have fewer entries.

If I run a SQL on the table that performs INDEX RANGE SCAN, it may look for around 100 leaf blocks (that satisfy my query condition).

Now, I rebuild the index. the number of leaf blocks may come down to 75,000.

Now the same query with INDEX RANGE SCAN may scan 50 blocks. In this case do you agree the INDEX RANGE SCAN or even FAST FULL SCAN and FULL SCAN will be faster after rebuilding the indexes?

I agree there will not be any kind of positive performance impact for a unique index scan.

Please clarify.

Thanks,
Tom Kyte
February 16, 2007 - 10:51 am UTC

if you delete and don't reinsert sure - but if you delete and then tend to put the data back in there - well, what then?


insert data back

A reader, February 16, 2007 - 11:18 am UTC

If we insert data back then sure it will make use of the existing leaf blocks.

But I have a question here: if I delete (say id between 1 and 10000) and insert brand new set of data (say id between 50000 and 60000) will the new set of data go to a new leaf blocks or will it try to settle in the existing leaf blocks that have more room due to deleted records?

Here assuming index is on ID column.

thanks,

Tom Kyte
February 17, 2007 - 11:04 am UTC

Index after DELETE

Karteek, April 18, 2007 - 2:18 pm UTC

Hi Tom,

Is it possible to alter index to get automatically adjusted after we DELETE the records. If we insert data into, then index automatically grows and maintains balance as well. Can't this be possible in case of DELETE - shrinking and balancing...

------------------------------------------------------------------------------------------------------------------------
Tom, in this asktom site, is there an option to set so that a mail automatically be sent to the reviewer as soon as you write a follow-up to his/her review.
------------------------------------------------------------------------------------------------------------------------

Thank you,
Karteek
Tom Kyte
April 18, 2007 - 2:26 pm UTC

what kind of delete are you talking about here - you do not in general need to rebuild

no, there is not.

rebuilding index rarely

Karteek, April 18, 2007 - 9:46 pm UTC

Tom,

your words...

"You can rebuild them as well -- BUT YOU RARELY, IF EVER NEED TO"

Could you please share those RARE scenarios where we need to rebuild the indexes, assuming that index is still in valid state.

Thanks,
Karteek

sorry...not right place

Karteek, April 18, 2007 - 9:53 pm UTC

Sorry Tom... somehow my above question posted here, while trying to post it on other page. However you wrote that line in another follow-up.

my apologies for the confusion.

- Karteek

Authentication

A reader, April 19, 2007 - 11:27 am UTC

Tom,

Link you have given above is prompting for login id and password. Never encoutered this. sorry if this question causes inconvenience to you.

Thank you!
Tom Kyte
April 19, 2007 - 12:40 pm UTC

Joe Bloggs, May 28, 2008 - 4:45 am UTC

Tom, does your advice about not re-building indexes on a regular basis also apply to Oracle 7.3.4 (or can't you remember that far back)?

Yes, believe it or not, there is a place on this planet still using it !
Tom Kyte
May 28, 2008 - 9:37 am UTC

All the way back to version 2 if you like. There was no version 1 of Oracle.


Indexes on table only inserted

Rich, September 25, 2008 - 9:34 am UTC

Hi Tom,

What about an index based on a table which only has inserts done in it. How the index would behave then? Would it be a good candidate for a rebuild?

Rich
Tom Kyte
September 25, 2008 - 3:34 pm UTC

almost certainly not (unless it was a bitmap index).

Assuming a b*tree index, a normal b*tree index, almost certainly not. There is one edge case where that would not be true, but almost always "no"

There are three cases to consider:

a) randomly inserted data, like last name. On an employee table, you do not hire everyone with the name starting with A, then B then C and so on - you hire people with random last names and the index is hit all over the place. That index will always be about 20-40% 'empty' as we do a 60/40 block split when a block fills up. If you rebuilt it - it would be "smaller" for a very very very short period of time - during which it would expend it's energies getting fat again (lots of splits). A rebuild would give you nothing - you would not reclaim space for more than a very short period, the index would spend a lot of time and energy getting fat again.

b) monotomically changing data (a date field populated by sysdate, a sequence populated field) Here we would do 90/10 block splits - the index would always be inserted into on the right hand side but the left hand side would be 90% full - well packed.


c) a variation on (B) - this is the edge case. The field is indexed, the values IN GENERAL are monotomic - but not always, not perfectly. So, instead of always increasing (or decreasing) in value as inserted, the arrive slightly out of order. Now instead of a 90/10 split - we do the 60/40 split. we leave the blocks on the left 40% empty - and will never use that space since the values in general are increasing.

This index MIGHT benefit from a coalesce, maybe.