It seems that MySQL freaks out whenever it seems
something that looks like a derive table and refuses
it to cache. Even a non-UNION query like:
SELECT * FROM
(SELECT * FROM X WHERE A = 5) AS DerivedTable
Won't be cached.
I read a comment in the documentation that if you put
SQL_CACHE in the SELECTs of the parenthesized queries,
it will cache the individual queries:
http://dev.mysql.com/doc/mysql/en/query-cache.html
That's not true. It won't cache even the parenthsized
queries, and the execution time is still the same with
SQL_CACHE and not. I have the query cache configured
to cache every query unless I explicitly tell not to,
so there's nothing in my configuration that prevents
from caching.
This is very frustrating. It seems everything time I
find a solution to an obstacle, I stumble across a
nastier one.