Say I have a query with subqueries in CASE...WHEN...THEN like this (simplified, as the real query I'm working on is little harder on the eyes):
SELECT
CASE
WHEN (subquery1) = 1
THEN (subquery2)
WHEN (subquery1) > 1 AND (subquery3) = 1
THEN (subquery4)
END
FROM foo
All 4 subqueries are dependent subqueries.
Do all 4 subqueries get executed? Cause when I did an EXPLAIN all the subqueries are included in the plan.
Or do only subquery1 get executed first, and if that condition is met, subquery2 will be executed? If not, subquery3 will be executed to examine if the condition is met, and so on... When I re-wrote the query as a stored procedure to only execute the only relevant subqueries this way, there was a performance increase (time reduction). I'm curious to know if the original query was taking more time since it executes all the subqueries, regardless of whether the previous condition have been met.
I tried turn on the general log but the subqueries don't get logged individually. They're logged together as the whole query, so I can't tell which subquery was actually executed. Also tried looking at the MySQL docs on SUBQUERY but I haven't found anything yet.