Categories: Optimizer | Subquery Optimizations

6.0 Subquery Optimization Cheatsheet

Contents


This page describes only what you can already try in the code. For the list of all subquery optimization tasks, including incomplete and planned items, see Subquery_Works.

[edit] Semi-join optimizations

Semi-join subqueries are IN-subqueries that are AND-parts of the WHERE/ON clause:

 SELECT ... WHERE outer_expr IN (SELECT inner_expr FROM ... WHERE ... ) AND ... 

or

 SELECT ... WHERE (outer_expr1, ... outer_exprN) IN 
   (SELECT inner_expr1, ... inner_exprN FROM ... WHERE ...) AND ... 

The subquery must satisfy these conditions:

The subquery may or may not be correlated (new optimizations provide efficient handling for both kinds), it may also have DISTINCT or LIMIT clauses.

[edit] Strategies for handling semi-join subqueries

If a subquery meets semi-join criteria, MySQL will convert it to semi-join and then make a cost-based choice from these options.:

[edit] How semi-joins are reflected in EXPLAIN

EXPLAIN EXTENDED + SHOW WARNINGS shows the rewritten query. If a subquery was converted to semi-join you will see that then subquery predicate is gone and subquery's tables and WHERE clause were merged into parent's subquery's join list and WHERE.

[edit] Materialization

Materialization handles all kinds of IN-subqueries that are uncorrelated:

 ... outer_expr [NOT] IN (SELECT inner_expr FROM /* some uncorrelated subquery */ ) ... 

or

 ...  (outer_expr1, ... outer_exprN) [NOT] IN 
       (SELECT inner_expr1, ... inner_exprN FROM /* some uncorrelated subquery */ ) 

At the moment, materialization doesn't stand out much in EXPLAIN output: you'll see select_type change from DEPENDENT SUBQUERY to SUBQUERY.

[edit] Turning new optimizations on/off

The new @@optimizer_switch variable allows to turn new optimizations on and off. The value is comma-separated list of any subset of these keywords: "no_materialization", "no_semijoin", "no_loosescan", "no_firstmatch". no_semijoin implies no_loosescan and no_firstmatch.

To switch to MySQL 5.1 behavior, use this command:

 mysql> set @@optimizer_switch='no_semijoin,no_materialization';

To switch everything back on, use this:

 mysql> set @@optimizer_switch='';

[edit] Further reading

This page is a short summary. For a full overview of 6.0 optimizations, see

  1. New_Optimizer_Features_in_MySQL_6.0 MySQL University session, or
  2. New Subquery Optimizations in MySQL 6.0 slides from MySQL User Conference 2008 (this is the most up to date source) (but it doesn't cover WL#3985)

Also see Category:Subquery_Optimizations.

Retrieved from "http://forge.mysql.com/wiki/6.0_Subquery_Optimization_Cheatsheet"

This page has been accessed 6,798 times. This page was last modified 20:12, 30 December 2008.

Find

Browse
MySQLForge
Main Page
Current events
Recent changes
Random page
Help
Edit
Edit this page
Editing help
This page
Discuss this page
Post a comment
Printable version
Context
Page history
What links here
Related changes
My pages
Special pages
New pages
File list
Statistics
Bug reports
More...