Archive for March 30th, 2011
MySQL: GROUP BY in UNION
From Stack Overflow:
I have a query where I have a custom developed UDF that is used to calculate whether or not certain points are within a polygon (first query in
UNION
) or circular (second query inUNION
) shape.SELECT a.geo_boundary_id, … FROM geo_boundary_vertex a, … … GROUP BY a.geo_boundary_id UNION SELECT b.geo_boundary_id, … FROM geo_boundary b, … … GROUP BY b.geo_boundary_idWhen I run an explain for the query I get
filesort
for both queries within theUNION
.Now, I can split the queries up and use the
ORDER BY NULL
trick to get rid of thefilesort
however when I attempt to add that to the end of aUNION
it doesn't work.How do I get rid of the
filesort
?
In MySQL, GROUP BY
also implies ORDER BY
on the same set of expressions in the same order. That's why it adds an additional filesort
operation to sort the resultset if it does not come out naturally sorted (say, from an index).
This is not always a desired behavior, and MySQL manual suggests adding ORDER BY NULL
to the queries where sorting is not required. This can improve performance of the queries.
Let's create a sample table and see: