Skip to Main Content

Write Great SQL Office Hours

Free tips and training every month! Subscribe for reminders and more from Office Hours. FAQ

Header container

August 25

13:00 UTC   Start Times Around the World

Description

Why Is the Optimizer Estimating the Wrong Number of Rows?
The optimizer uses statistics to estimate how many rows it'll fetch from each table. This drives its decisions to on how to access the tables and join them. So the key to a good plan is good row estimates.

So why - even with fully up-to-date stats - does the optimizer sometimes get its estimates so badly wrong?

In this session we investigate two common causes: data skew and correlation. We look at why these lead to inaccurate estimates and what you can do to improve them.

Highlights include:

01:20 - Demo of SQL statements where the optimizer's row estimates are (badly) incorrect
05:40 - Value skew: some values appearing more often than others
06:20 - Frequency histograms
09:25 - Top-frequency histograms
14:25 - Height-balanced histograms
18:00 - Hybrid histograms
23:15 - Range skew: gaps between values
29:45 - Correlation between columns
32:15 - Demo: histogram creation during stats gathering based on column usage
42:40 - Demo: stats for correlated columns
46:00 - Demo: stats on expressions
51:55 - Demo: stats for subqueries
57:10 - Demo: disabling histograms
58:00 - Using table preferences to set non-standard stats gathering options

Subscribe to give us feedback and be notified of upcoming sessions!

Your Experts

Chris Saxon
Chris Saxon, Oracle Developer Advocate for SQL    
Chris Saxon is the quizmaster for the Database Design competitions and an Oracle Developer Advocate for SQL. His job is to help you get the best out of the Oracle Database and have fun with SQL! He's also part of the answer team on Ask TOM and creates YouTube videos at The Magic of SQL channel.
Keith Laker
Keith Laker, Senior Principal Product Manager    
I have been working with Oracle data warehouse technology for over 20 years working on a wide variety of data warehouse projects both as a consultant and an onsite support engineer. I am now part of the Data Warehouse Product Management Team where I am responsible for analytical SQL. I am based in the UK at our Manchester office. A key part of my role is to work with our sales teams to brief our customers on data warehousing and analytical SQL: explaining the wide variety of new and exciting opportunities that our DW and analytical solutions can support. I regularly deliver sales training for data warehousing and analytical SQL across all our sales regions and provide competitive intelligence support across all the major data warehouse vendors.