For those who were unaware, in MySQL the following statements are currently identical:
SELECT MAX(Population), CountryName FROM City GROUP BY CountryName;
SELECT MAX(Population), CountryName FROM City GROUP BY CountryName ORDER BY CountryName;
That is to say that regardless of whether or not you asked for
it, whenever you chose to GROUP BY
, you will also
have data sorted and returned in that order too.
The problem with this, is that it can result in worse performing
queries. Sorting either reduces the number of execution plans
possible, or requires an additional step to sort the data. Which
is why many DBAs advocate writing group by queries with ORDER BY
NULL
. i.e.
SELECT MAX(Population), CountryName FROM City GROUP BY CountryName;
Should …
[Read more]