Beruflich Dokumente
Kultur Dokumente
Addenda
What is a Histogram? Types of Histograms How to create Histograms How to identify existing Histograms Rules and tips when creating Histograms Q&A
What is a Histogram?
A histogram holds the data distribution of values within a column of a table.
Number of occurrences for a specific value/range Used by CBO to optimize a query. By default DBMS_STATS does not collect histogram stats.
Min/Max Value and # of distinct values NOTE: DBMS_STAT is used to delete histogram data.
Types of Histograms
Two types of Histograms
frequency histograms height-balanced histograms Type of histogram is stored in the HISTOGRAM column of the *tab_col_statistics views (USER/DBA) Value = (HEIGHT BALANCED, FREQUENCY, or NONE)
Frequency Histograms
Each value of the column corresponds to a single bucket of the histogram. Each bucket contains the number of occurrences of that single value.
Height-Balanced Histograms
In a height-balanced histogram, the column values are divided into bands so that each band contains approximately the same number of rows. The useful information that the histogram provides is where in the range of values the endpoints fall.
Height-Balanced Example 1
Data A B C B C C C C C B D E Sorted A B B B C C C C C C D E
Results EPN 0 1 2 3
EPV B C C E
Buckets = 4
Copyright R Wave Solutions 2008 All rights reserved
Height-Balanced
Data Sorted A A B Notice that B C C B Crosses Buckets. B B C C C C This is howC C C C database knows C C which values are B C D D popular E E
Results EPN 0 1 2 3
EPV B C C E
Buckets = 4
Copyright R Wave Solutions 2008 All rights reserved
In 9 select owner, table_name, column_name, count(*) buckets from dba_histograms where endpoint_number not in (0,1) group by owner, table_name, column_name
Copyright R Wave Solutions 2008 All rights reserved
Use method_opt==>'REPEAT
Adding a histogram to an un-indexed column that is used in a where clause can improve performance.
Histogram Example
select sum(t1.d2*t2.d3*t3.d3) from t1, t2, t3 where t1.fk1 = t2.pk1 and t3.pk1 = t2.fk1 and t3.d2 = 35 and t1.d3 = 0; << This column has 2 values in it In this example column T1.D3 has 2 values and is our most selective criterion, but without a histogram CBO assumes an even distribution of 2 distinct values (density = 0.5).
Copyright R Wave Solutions 2008 All rights reserved
Query = 20231
the IO
Copyright R Wave Solutions 2008 All rights reserved
1 SORT AGGREGATE 2088 HASH JOIN 601 TABLE ACCESS BY INDEX ROWID OBJ#(57606) 601 INDEX RANGE SCAN OBJ#(57612) 1000 TABLE ACCESS BY INDEX ROWID OBJ#(57605) 1005 NESTED LOOPS 4 TABLE ACCESS FULL OBJ#(57604) filter("T1"."D3"=0) 1000 INDEX RANGE SCAN OBJ#(57609) access("T1"."FK1"="T2"."PK1")
Copyright R Wave Solutions 2008 All rights reserved
Histograms Opportunities
Any column used in a where clause with skewed data Columns that are not queried all the time Reduced overhead for insert, update, delete
Evenly distributed data in the column Columns that are not queried
Accidentally deleting histogram data is possible. Histogram data is ONLY Collected/Maintained when stats are collected (DBMS_STATS). Changing data in the table does not change data stored in the histogram.
Maintaining Histograms
Always check if histogram exists on table before DBMS_STATS is run.
Use METHOD_OPT FOR ALL COLUMNS REPEAT to prevent deletion of histograms data. Distribution ratio change -> Recollect
Copyright R Wave Solutions 2008 All rights reserved
Creating Histograms
What is the best way to start out creating Histograms? Three popular ways to create.
Salt and Pepper - Create them as you need them. Little Hammer DBMS_STATS using METHOD_OPT AUTO
Should NOT be used after fresh restarted database
Big Hammer DBMS_STATS using SKEWONLY Over Kill Hammer - FOR ALL COLUMNS
Other Comments
It is the Density of a column that the optimizer considers when accessing a table. Changing the number of buckets changes the density. More Buckets DOES NOT mean better density. Using degree != 1 can affect density.
Copyright R Wave Solutions 2008 All rights reserved
Review
What is a Histogram? Two types of Histograms in the database How to create Histograms Data is collected by running DBMS_STATS. Different methods to create histograms Bucket size affects density of column. Density of the column affects CBO.
Copyright R Wave Solutions 2008 All rights reserved