Today I saw a query like this:
SELECT d FROM t;
Performance was terrible. I ran out of patience after several minutes and killed the thread.
I changed the query to this:
(SELECT d FROM t) UNION ALL (SELECT NULL LIMIT 0);
It completed in under 3 seconds.
Can you explain how a no-op UNION so dramatically improved performance? (I couldn’t have, without help from Jesper Krogh and James Day).
The “presence of any column larger than 512 bytes in the SELECT list, if UNION or UNION ALL is used,” prevents use of an in-memory temporary table. That means the temporary table will be created directly as MyISAM on the disk, and will keep the VARCHAR format, so it will be much smaller. Converting the field to TEXT would have the same effect.

Leave a Reply