Understanding Histograms in Oracle and How to Manage Them
1. What is a Histogram?
- A histogram in Oracle is metadata that stores the distribution of values in a column.
- Optimizer uses histograms to make better cardinality estimates for queries with skewed data.
- However, sometimes unwanted histograms get created, leading to bad plans and performance regression.
2. Why do histograms appear suddenly?
- Because of METHOD_OPT default setting (FOR ALL COLUMNS SIZE AUTO) in DBMS_STATS or FND_STATS.
- Oracle automatically decides whether to create histograms if it detects skew.
- If stats were gathered with AUTO_SAMPLE_SIZE or with SIZE SKEWONLY.
- If stats gathering job was changed at schema or DB-level (new preference applied).
3. How to Check Histograms on a Table
Check which columns have histograms:
Output example
- NONE → no histogram
- HEIGHT BALANCED or HYBRID → histogram exists
4. How to Compare Histogram Changes Over Time
If you have AWR / DBA_HIST data, you can track changes:
5. How to Remove a Histogram (Fix Bad Plans)
To remove a histogram from a column:
This deletes column stats including the histogram.
6. How to Prevent Histograms During Stats Gathering
Option A – For a specific table
This ensures no histograms will be created.
Option B – For the whole schema
Option C – For the whole database
7. How to Check Preferences (Table / Schema / DB)
Check table-level preferences:
8. Best Practices
- Use SIZE 1 for columns that don’t need histograms.
- For skewed columns that matter, manually decide histogram size.
- After fixing, lock stats:
- EXEC DBMS_STATS.LOCK_TABLE_STATS('SALES', 'SALES_DATA');
- In Oracle EBS environments, always prefer FND_STATS instead of DBMS_STATS.
With these queries, you can detect, compare, delete, and prevent histograms.
Controlling histograms avoids sudden query plan changes and ensures stability.
Post a Comment
Post a Comment