Natural Data Clustering: Why Nested Loops Win So Often May, 2008 ©2008 Dan Tow, All rights reserved SingingSQL.

Slides:



Advertisements
Similar presentations
Youre Smarter than a Database Overcoming the optimizers bad cardinality estimates.
Advertisements

Examples of Physical Query Plan Alternatives
Tuning Oracle SQL The Basics of Efficient SQLThe Basics of Efficient SQL Common Sense Indexing The Optimizer –Making SQL Efficient Finding Problem Queries.
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
Presented By Akin S Walter-Johnson Ms Principal PeerLabs, Inc
Understanding SQL Server Query Execution Plans
SQL Server performance tuning basics
Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director.
Query Optimization Reserves Sailors sid=sid bid=100 rating > 5 sname (Simple Nested Loops) Imperative query execution plan: SELECT S.sname FROM Reserves.
A Non-Blocking Join Achieving Higher Early Result Rate with Statistical Guarantees Shimin Chen* Phillip B. Gibbons* Suman Nath + *Intel Labs Pittsburgh.
1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
EXECUTION PLANS By Nimesh Shah, Amit Bhawnani. Outline  What is execution plan  How are execution plans created  How to get an execution plan  Graphical.
Query Execution, Concluded Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 18, 2003 Some slide content may.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Query Optimization Chapters 14.
Slide: 1 Presentation Title Presentation Sub-Title Copyright 2010 Robert Haas, EnterpriseDB Corporation. Creative Commons 3.0 Attribution. The PostgreSQL.
Recent-Rows-First Case Study February, 2010 ©2010 Dan Tow, All rights reserved SingingSQL Presents SingingSQL.
Query Evaluation. An SQL query and its RA equiv. Employees (sin INT, ename VARCHAR(20), rating INT, age REAL) Maintenances (sin INT, planeId INT, day.
1 Relational Query Optimization Module 5, Lecture 2.
1 Overview of Storage and Indexing Chapter 8 (part 1)
Chapter 8 Physical Database Design. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Overview of Physical Database.
1DBTest2008. Motivation Background Relational Data Warehousing (DW) SQL Server 2008 Starjoin improvement Testing Challenge Extending Enterprise-class.
Executing Explain Plans and Explaining Execution Plans Craig Martin 01/20/2011.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query Evaluation Chapter 12: Overview.
Getting SQL Right the First Try (Most of the Time!) May, 2008 ©2007 Dan Tow, All rights reserved SingingSQL Presents.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Advanced Databases: Lecture 8 Query Optimization (III) 1 Query Optimization Advanced Databases By Dr. Akhtar Ali.
When Good Optimizers Make Bad Choices (Sometimes) March, 2005 ©2005 Dan Tow All rights reserved SingingSQL Presents.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
Module 5 Planning for SQL Server® 2008 R2 Indexing.
Query Processing. Steps in Query Processing Validate and translate the query –Good syntax. –All referenced relations exist. –Translate the SQL to relational.
11-1 Improve response time of interactive programs. Improve batch throughput. To ensure scalability of applications load vs. performance. Reduce system.
Star Transformations Tony Hasler, UKOUG Birmingham 2012 Tony Hasler, Anvil Computer Services Ltd.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
SQL Tuning – Reaching Recent Data Fast Copyright ©2008 by Dan Tow
© IBM Corporation 2005 Informix User Forum 2005 John F. Miller III Explaining SQLEXPLAIN ®
BlinkDB: Queries with Bounded Errors and Bounded Response Times on Very Large Data ACM EuroSys 2013 (Best Paper Award)
1 Chapter 9 Tuning Table Access. 2 Overview Improve performance of access to single table Explain access methods – Full Table Scan – Index – Partition-level.
David Konopnicki –1997, Rev. MS Optimizing Join Statements To choose an execution plan for a join statement, the optimizer must choose: ä Access.
Sorting and Joining.
1 Overview of Query Evaluation Chapter Outline  Query Optimization Overview  Algorithm for Relational Operations.
Eugene Meidinger Execution Plans
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Execution Plans Detail From Zero to Hero İsmail Adar.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Storage and Indexing Chapter 8.
The PostgreSQL Query Planner Robert Haas PostgreSQL East 2010.
How is data stored? ● Table and index Data are stored in blocks(aka Page). ● All IO is done at least one block at a time. ● Typical block size is 8Kb.
Tuning Oracle SQL The Basics of Efficient SQL Common Sense Indexing
Module 11: File Structure
Ripple Joins for Online Aggregation
Overview of Query Optimization
Introduction to Execution Plans
Examples of Physical Query Plan Alternatives
The Key to the Database Engine
Lecture 12 Lecture 12: Indexing.
Top Tips for Better TSQL Stored Procedures
Execution Plans Demystified
Introduction to Execution Plans
Overview of Query Evaluation
Score a (row) goal and beat a query optimizer
Relational Query Optimization
Execution plans Eugene
Diving into Query Execution Plans
A – Pre Join Indexes.
Introduction to Execution Plans
Query Optimization Techniques
Introduction to Execution Plans
Performance Tuning ETL Process
All about Indexes Gail Shaw.
Presentation transcript:

Natural Data Clustering: Why Nested Loops Win So Often May, 2008 ©2008 Dan Tow, All rights reserved SingingSQL Presents SingingSQL Presents:

Nested Loops vs. Hash Joins Joining A to B, assuming A has a better filter than B (almost certainly true for a correct nested-loops join order): Nested Loops: For each A, find matching Bs following the indexed join key. Hash Join: Reach B as if with a single-table query, then line up A-to-B matches on the hash of the join keys.

Common Cases to Consider Nested-loops join reaches a large fraction of a small joined-to table: Optimizer tends to prefer a hash join, correctly, but both alternatives are well-cached and fast. Nested loops obviously reach a tiny fraction of a large joined-to table: Optimizer tends to prefer a nested-loops join, correctly, and this is much faster than the hash-join alternative.

Common Cases to Consider Nested loops reach a tiny fraction of a large joined-to table, but the optimizer greatly over- estimates that fraction: Optimizer would prefer a nested-loops join, if it knew more, and this is much faster than the hash-join alternative. –This tends to happen for “exceptions” reports, where two conditions are almost mutually exclusive, but the optimizer’s assumption of statistically independent conditions yields the over-estimate. –As humans, we can and should expect reasonable rowcounts in reports, so we should often expect this case.

A Rare Case to Consider Nested loops reach a large fraction of a large joined-to table: Optimizer will strongly (and usually correctly) prefer a hash join to a full table scan, taking into account the benefit of multi- block reads, and avoiding multiple logical I/Os to the same blocks. –When the join order is correct, this almost always implies a huge set of rows being returned by the query. –Such large return rowsets are very rarely useful for human consumption, but can reasonably occur in middleware or conversion processes.

Common Cases to Consider Nested loops reach a medium-sized fraction of a large joined-to table: Optimizer will mildly prefer a hash join to a full table scan, taking into account the benefit of multi-block reads, and avoiding multiple logical I/Os to the same blocks. –In this common case, nested-loops are frequently preferable to the hash-join choice of the optimizer, largely owing to self-caching in queries of naturally clustered tables.

A Business Database, from 10,000 Meters High Tables that grow the largest track frequent business events, such as product orders, customer calls, account payments, et cetera. The application exists to drive correct business actions and choices, and these in turn depend on recent events. Therefore, most data queried from large tables concerns recent events, and was recently added to those tables, clustered at the top.

A Business Database, from 10,000 Meters High Event-type tables rarely stand alone – they usually join to several other events-related tables tracking related events and event details. Joins to large tables are therefore usually from recent event-type data in one table to related, recent, event-type data in another table.

Related, Event-Type Heap Tables Recent Master rows Recent Detail rows

A Well-Clustered Range Will Likely Join to a Well-Clustered Range! Recent Master rows Recent Detail rows

Nested-Loops Costs to Consider We can reach 30,000 rows or more per second with nested loops to cached data, a trivial time compared to the magnitude of the output, unless a bad join order reaches tables in the wrong order! (What application must produce multiple 30,000- plus-row reports per minute, even in the worst case?) Where nested-loops costs matter, it is because physical I/O time is high, or the join order is wrong.

Nested-Loops Index Physical I/O If the join key is an ordinary sequence- generated ID, the join-key index will naturally cluster in the order the rows were created. In this case, the query need only cache a few blocks to read thousands of rows from the recent end of the table – index-block I/O costs are trivial thanks to self-caching early in the query execution, even if these blocks are not yet cached.

Nested-Loops Table Physical I/O In the ideal case (which is surprisingly close to the common case!), the joined-to table is accessed roughly in row-creation order. Read-ahead in the disk subsystem effectively converts reads like this into multi- block reads. These two effects lead to physical I/O much like a fractional table scan of only the top of the heap table (which is likely largely cached, anyway).

Physical Demonstration of Co-clustered Performance Two million-row tables, joined one-to-one, matching rows created in the same order, reading the most-recent 1% of the rows: Join TypeTime/s LIO PIO Nested-Loops Hash

Physical Demonstration of Co-clustered Performance Two million-row tables, joined one-to-one, matching rows created in the same order, reading the most-recent 1% of the rows: Join TypeTime/s LIO PIOCBO “Cost” Nested-Loops Hash

Physical Demonstration of Non-Co-clustered Performance Two million-row tables, joined one-to-one, joined-to matching rows created in scattered order, reading the most-recent 1% of the driving-table rows: Join TypeTime/s LIO PIO Nested-Loops Hash

Physical Demonstration of Non-Co-clustered Performance Two million-row tables, joined one-to-one, joined-to matching rows created in random order, reading the most-recent 1% of the driving-table rows: Join TypeTime/s LIO PIOCBO “Cost” Nested-Loops Hash

Comparing Co-Clustered and Non-Co-clustered Performance Join TypeTime/s LIO PIOCBO “Cost” Co-clustered Nested Loops Non-co- clustered NL Hash (both) K 1.6K- 1.8K 3737

Comparing Co-Clustered and Non-Co-clustered Performance Join TypeTime/s LIO PIOCBO “Cost” Co-clustered Nested Loops Non-co- clustered NL Hash (both) K 1.6K- 1.8K 3737 Cost function predicts relative runtimes well in non-co-clustered case! Co-clustered case is more important, though, and greatly favors nested loops!

Conclusions The join method to small tables matters little, but tends to slightly favor hash joins for queries returning substantial rowcounts. Hash joins to large tables can win in rare cases where sensible queries return (or summarize) very high rowcounts.

Conclusions Hash joins to large tables can win in cases where the join order is wrong, or where join-key indexes are missing or disabled through join-key type conversions or other index-disabling functions. (In the above case, the better solution is to enable the optimum nested-loops plan!)

Conclusions Nested loops almost always win when the CBO prefers them. Hint-forced nested loops usually win when the CBO underestimates the combined selectivity of multiple filters (when mutually-exclusive conditions violate the assumption of statistically independent conditions).

Conclusions Hint-Forced nested loops in the best join order usually win in the common case of joins from recent rows to related recent rows (joins between naturally co-clustered tables). Hash joins to large full table scans are rarely better than the best nested-loops plan.

Questions?