The oracle documentation says that during altering an index clauses shrink space compact
and coalesce
are quite similar and could be replaced by each other, but Tom found some differences in the behavior.
Since coalesce is not available in standart edition of Oracle Database, I suppose there're some benefits in using it.
So, what are the differences? Can I perform shrink space compact
on a dynamically changing index?
Oracle Segment Shrink Space option will consolidate the fragmented free space below the high-water mark and compact the data. The high-water mark will be moved back, and the reclaimed space will be released. After the shrink operation, data in the segment will be contained in a smaller number of blocks.
Coalescing an index performs an in-place reorganization of the index data. It combines adjacent leaf blocks into a single leaf block and puts the newly empty leaf blocks on the free list of the index segment. The freed up index leaf blocks are reused by the database during ...
You can specify the shrink space clause to reduce space usage in not only an index, or index partition/subpartition, but also in a table. For the primary keys of an index organized table, you must use the alter table statement instead and specify the coalesce clause.
The above answer is false. There are basically 4 options.
1 - ALTER INDEX COALESCE
2 - ALTER INDEX SHRINK SPACE
3 - ALTER INDEX SHRINK SPACE COMPACT
4 - ALTER INDEX REBUILD
Options 1 and 3 do NOT free up blocks. They just free up space in existing blocks. Coalesce does a little bit worse job, there will be more blocks with only 25-50% free space, while with shrink space compact, there will be more blocks with 75-100% free space. The total number of blocks, however, stay the same. For example, an index with 200 blocks with coalesce, and after deleting 1/5 of the rows randomly, will have ~1/5 of the index blocks have 25-50% free space while the rest remain full.
On the other hand, shrink space and rebuild do free up the blocks, and merge them into existing ones, thus reducing the total number of blocks. I think the only difference is speed. When you delete only 5% from a large table, there's no reason to rebuild the entire index, and it will be very slow. However, shrink space might be a little bit faster here, because it does not rebuild the entire index, just reorganizes the blocks.
Obviously the fastest choice would be coalescing or shrinking space with compact option.
First of all, indexes generally do not need to be frequently rebuilt. They generally grow to a steady size and stay there, and rebuilding them produces only a temporary benefit to queries that is then counterbalanced by increased load in modifying them due to an increased rate of block splits. So don't forget that the best optimisation for a process is to eliminate it completely -- if you think you have a need for frequent rebuilds then post a question and maybe the cause can be explained and a different approach be found.
Anyway, coalesce reduces the number of blocks that are holding index data, thus freeing up blocks completely so that they can be re-used for new index entries. The freed blocks are still allocated to the index, though. This can prevent indexes from growing too large.
Shrink does something similar but moves the populated blocks to allow freed blocks at the "end" of the index segment to be deallocated from it. Thus the index segment actually gets smaller. This requires an exclusive lock on the table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With