EXISTS-to-IN optimization
This feature is still in development. The target version is MariaDB 10.0.1 Current variant of the code is at lp:maria-captains/maria/10.0-exists2in
Contents
MySQL (including MySQL 5.6) has only one execution strategy for EXISTS subqueries. The strategy is essentially the straightforward, "naive" execution, without any rewrites.
MariaDB 5.3 has introduced a rich set of optimizations for IN subuqeries. Now, it makes sense to convert an EXISTS subquery into an IN so that the new optimizations can be used.
EXISTS
will be converted into IN
in two cases:
- Trivially correlated EXISTS subqueries
- Semi-join EXISTS
we will now describe these two cases in detail
Trivially-correlated EXISTS subqueries
Often, EXISTS subquery is correlated, but the correlation is trivial. The subquery has form
EXISTS (SELECT ... FROM ... WHERE outer_col= inner_col AND inner_where)
and "outer_col" is the only place where the subquery refers to outside fields. In this case, the subquery can be re-written into uncorrelated IN:
outer_col IN (SELECT ... FROM ... WHERE inner_where)
(NULL
values require some special handling, see below). For uncorrelated IN subqueries, MariaDB is able a cost-based choice between two execution strategies:
- IN-to-EXISTS (basically, convert back into EXISTS)
- Materialization
That is, converting trivially-correlated EXISTS
into uncorrelated IN
gives query optimizer an option to use Materialization strategy for the subquery.
Currently, EXISTS->IN conversion works only for subqueries that are at top level of the WHERE clause, or are under NOT operation which is directly at top level of the WHERE clause.
Semi-join EXISTS subqueries
SELECT ... FROM ... WHERE EXISTS (SELECT ...)
looks similar to semi-join subqueries. It satisfies the main semi-join property that rows from the outer select that have no matches inside the subquery do not get into the query output.
Semi-join optimizer offers a rich set of execution strategies for both correlated and uncorrelated subqueries. The set includes FirstMatch strategy which is an equivalent of how EXISTS suqueries are executed, so we do not lose any opportunities when converting an EXISTS subqery into a semi-join.
In theory: it makes sense to convert all kinds of EXISTS subqueries: convert both correlated and uncorrelated ones, convert irrespectively of whether the subquery has inner=outer equality.
In practice: the subquery will be converted only if it has inner=outer equality. Both correlated and uncorrelated subqueries are converted.
Handling of NULL values
TODO: rephrase this:
- IN has complicated NULL-semantics. NOT EXISTS doesn't.
- EXISTS-to-IN adds IS NOT NULL before the subquery predicate, when required
Control
The optimization is controlled by exists_to_in
flag in
@@optimizer_switch
. Currently, the optimization is OFF by default.
Limitations
EXISTS-to-IN doesn't handle
- subqueries that have GROUP BY, aggregate functions, or HAVING clause
- subqueries are UNIONs
- a number of degenerate edge cases