0% found this document useful (0 votes)
9 views

Indexes For Performance

Uploaded by

zkmatu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Indexes For Performance

Uploaded by

zkmatu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Maintaining In-Production Indexes for Performance

Objectives

• List index types and their attributes


• Create indexes for performance
• Describe index defragmentation
• Calculate the deleted leaf row ratio
• Rebuild, coalesce, and shrink indexes
• Understand best practices for index defragmentation
• Monitor index usage

Index Types and When to Use Them

Index Type When to Use It


B-tree Indexes - For columns used in WHERE clauses
- Join columns
- Make specific queries efficient
Composite Index - For columns used together in WHERE clauses
Function-based
- For columns used in functions in WHERE clauses
Index
Bitmap Index - For read-only or read-mostly tables
Partitioned Index - For large indexes and/or partitioned tables
IOT - Data is always (or mainly) retrieved by index-key columns
Domain Index - Designed for a specialized domain (e.g., spatial, image processing)

Obtaining Information About Indexes:


-- To obtain information about existing indexes and their columns:

SELECT TABLE_NAME, INDEX_NAME, STATUS, VISIBILITY


FROM USER_INDEXES
WHERE TABLE_NAME = 'EMP';

SELECT INDEX_NAME, COLUMN_NAME, COLUMN_POSITION


FROM USER_IND_COLUMNS
WHERE TABLE_NAME = 'EMP'
ORDER BY INDEX_NAME, COLUMN_POSITION;

SELECT INDEX_NAME, BLEVEL, LEAF_BLOCKS, DISTINCT_KEYS, CLUSTERING_FACTOR,


NUM_ROWS, STALE_STATS, LAST_ANALYZED
FROM USER_IND_STATISTICS
WHERE TABLE_NAME = 'EMP'
ORDER BY INDEX_NAME;
Index Defragmentation

• Causes: Index defragmentation is caused by deleting rows from the underlying table.
• Tools:
o Rebuilding the indexes
o Coalescing the indexes
o Shrinking the indexes

Calculating the Deleted Leaf Row Ratio

To retrieve the percentage of deleted leaf rows:

ANALYZE INDEX <index_name> VALIDATE STRUCTURE;

SELECT NAME, ROUND(DEL_LF_ROWS/LF_ROWS*100,2) RECLAIMABLE_SPACE_PCT


FROM INDEX_STATS;

Rebuilding the Indexes


Syntax:

ALTER INDEX <index name> REBUILD [ONLINE] [TABLESPACE <tbs name>] [LOGGING | NOLOGGING];

• Pros:
o Allows tablespace relocation and changing physical attributes.
o Can be implemented offline and online (acquires a lock on the index at switch
time).
• Cons:
o Requires free disk space equal to the index size.

Coalescing the Indexes


Syntax:

ALTER INDEX <index name> COALESCE;

• Pros:
o Always performed online without locking the index.
o Does not require more disk space.
• Cons:
o Cannot relocate to a different tablespace or change physical attributes.
o Does not deallocate free space.
o More overhead when deleted entries represent a high percentage of the index.
Shrinking the Indexes
Syntax:

ALTER INDEX <index name> SHRINK SPACE [COMPACT];

• Pros:
o Can adjust the HWM (requires index lock).
o If COMPACT is not used, deallocates disk space.
• Cons:
o Cannot relocate to a different tablespace or change physical attributes.
o Cannot be used with bitmap or function-based indexes.

Comparison Between Index Defragmentation Solutions

Operation Rebuilding the Indexes Coalescing the Indexes Shrinking the Indexes
Always online (locks
Create a new index
Can be executed offline/online Always online (no locks) involved when COMPACT
from existing index
is not used)
Efficient with slightly
Combine adjacent Efficient with highly (>20%) fragmented Efficient with slightly
(<20%) fragmented
leaf blocks indexes fragmented indexes
indexes
Reorganize blocks
under the HWM
Requires more disk space Requires less disk space Requires less disk space
then moves the
HWM
Can move the
index to different Yes No No
tablespace
Deallocate disk Disk space does not
Deallocate disk space Deallocate disk space
space change

Index Defragmentation Best Practices

• In normal cases, index defragmentation is not needed.


• Consider defragmentation in the following cases:
o Index becomes highly fragmented (>20%).
o Deleted indexed values are not reused.
o Index clustering factor becomes out of sync.
• Consider coalescing before rebuilding (or shrinking).
• Rebuilding is needed when:
o Relocating the index to a different tablespace or changing physical attributes.
o Reallocating space used by deleted entries.
Monitoring Index Usage
-- To monitor index usage:

ALTER INDEX <index_name> MONITORING USAGE;

SELECT INDEX_NAME, USED, MONITORING


FROM V$OBJECT_USAGE
WHERE INDEX_NAME = '<index_name>';

You might also like