I have query like this:
SELECT `table_1`.* from `table_1`
INNER JOIN `table_2` [...]
INNER JOIN `table_3` [...]
WHERE `table_1`.`id` IN(
SELECT `id` FROM [...]
)
AND [more conditions]
When I use EXPLAIN, there is 'DEPENDENT SUBQUERY' at the end, but I want this subquery to be performed first, before other conditions.
Is is possible?
A subquery is known as the inner query, and the query that contains subquery is known as the outer query. The inner query executed first gives the result to the outer query, and then the main/outer query will be performed. MySQL allows us to use subquery anywhere, but it must be closed within parenthesis.
Answer: D. The sub-query always executes before the execution of the main query. Subqueries are completed first. The result of the subquery is used as input for the outer query.
Subqueries can be nested so that the innermost subquery returns a value or values to be used by the next outer query. Then, that subquery's value or values are used by the next outer query, and so on. Evaluation always begins with the innermost subquery and works outward.
An ORDER BY command cannot be used in a subquery, although the main query can use an ORDER BY. The GROUP BY command can be used to perform the same function as the ORDER BY in a subquery. Subqueries that return more than one row can only be used with multiple value operators such as the IN operator.
SELECT `table_1`.*
FROM `table_1`
INNER JOIN
`table_2` [...]
INNER JOIN
`table_3` [...]
WHERE `table_1`.`id` IN
(
SELECT `id`
FROM [...]
)
AND [more conditions]
If the inner table is properly indexed, the subquery here is not being "performed" at all in a strict sense of word.
Since the subquery is a part of an IN
expression, the condition is pushed into the subquery and it's transformed into an EXISTS
.
In fact, this subquery is evaluated on each step:
EXISTS
(
SELECT NULL
FROM [...]
WHERE id = table1.id
)
You can actually see it in the detailed description provided by EXPLAIN EXTENDED
.
That's why it's called DEPENDENT SUBQUERY
: the result of each evaluation depends on the value of table1.id
. The subquery as such is not correlated, it's the optimized version that is correlated.
MySQL
always evaluates the EXISTS
clause after the more simple filters (since they are much easier to evaluate and there is a probability that the subquery won't be evaluated at all).
If you want the subquery to be evaluated all at once, rewrite the query as this:
SELECT table_1.*
FROM (
SELECT DISTINCT id
FROM [...]
) q
JOIN table_1
ON table_1.id = q.id
JOIN table_2
ON [...]
JOIN table_3
ON [...]
WHERE [more conditions]
This forces the subquery to be leading in the join, which is more efficient if the subquery is small compared to table_1
, and less efficient if the subquery is large compared to table_1
.
If there is an index on [...].id
used in the subquery, the subquery will be performed using an INDEX FOR GROUP-BY
.
this is a known bug in mysql: http://bugs.mysql.com/bug.php?id=25926
a useful workaround is to push down the subquery into another select * from (subquery) as dt
type subquery.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With