 |
|
SQL tuning with histograms
Oracle Tips by Burleson Consulting |
I just returned from a large Oracle conference and I spoke with
several people about an interesting trick to improve Oracle?s
cost-based SQL optimizer (the CBO) by fixing
Oracle cardinality
errors for inter-table join baggage. See my other notes on
SQL tuning with
histograms.
The Oracle CBO needs to be able to accurately estimate the final
number of rows returned by each step of the query. When an SQL
query has a complex WHERE clause it can be very difficult to
estimate the size of the intermediate result sets, especially when
the WHERE clause transforms column values with mathematical
functions.
This is one reason that the ORDERED hint is so popular, but it has
been shown that having liberal column histograms on the table
columns can often aid the optimizer in making better execution
plans.
In sum, histograms are not just for non-unique column values that
are unevenly distributed (skewed), and several noted DBA?s have
suggested that more liberal use of histograms will aid the CBO is
making better decisions. The dbms_stats ?auto? feature detects and
builds column histograms, but it has the shortcoming of being too
conservative in some cases.
Savvy DBA?s are now experimenting with broad-brush histograms, for
all indexes columns. I first heard of this technique from Jeff
Maresh (noted data warehouse consultant), who told me that he has
taken to creating 10-bucket histograms for all data warehouse table
columns. I heard this advice again at the IOUG conference from Arup
Nanda (noted author and DBA of the year) and from Mike Ault.
They are abandoning the use of the ?auto? option and manually
creating 20-bucket histograms across-the-board, and they claim that
it can make a huge difference for databases with lots of multi-table
joins in he SQL.
I?ve not tried this technique yet, but when three experts make the
assertion, I believe that there may be something to the new
technique. The only downside, of course, is the time required to
gather the column histograms and a small amount of additional
storage in the data dictionary.
I go into deeply into the issue of index join cardinality on my new
book
"Oracle
Tuning: The Definitive Reference"