The two queries express the same question. Apparently the query optimizer chooses two different execution plans. My guess would be that the
distinct approach is executed like:
business_keyvalues to a temporary table
group by could be executed like:
business keyin a hashtable
The first method optimizes for memory usage: it would still perform reasonably well when part of the temporary table has to be swapped out. The second method optimizes for speed, but potentially requires a large amount of memory if there are a lot of different keys.
Since you either have enough memory or few different keys, the second method outperforms the first. It's not unusual to see performance differences of 10x or even 100x between two execution plans.
~ Answered on 2011-10-30 10:44:02