tags:

views:

238

answers:

3

According to tips from MySQL performance wiki

Don't use DISTINCT when you have or could use GROUP BY

Can somebody post example of queries where GROUP BY can be used instead of DISTINCT?

+4  A: 
SELECT Code
FROM YourTable
GROUP BY Code

vs

SELECT DISTINCT Code
FROM YourTable
astander
+5  A: 

If you know that two columns from your result are always directly related then it's slower to do this:

SELECT DISTINCT CustomerId, CustomerName FROM (...)

than this:

SELECT CustomerId, CustomerName FROM (...) GROUP BY CustomerId

because in the second case it only has to compare the id, but in the first case it has to compare both fields. This is a MySQL specific trick. It won't work with other databases.

Mark Byers
Those two queries aren't the same, are they? In the second one you're only selecting distinct IDs, not distinct names as well.
DisgruntledGoat
This won't work with any other T-SQL, or it won't be faster with any other one?
Norla
Well the second one doesn't work!!! Any Column not included in the GROUP BY clause requires an aggregation function.
Craig Young
Craig Young: really? http://dev.mysql.com/doc/refman/5.0/en/group-by-hidden-columns.html "MySQL extends the use of GROUP BY so that you can use nonaggregated columns or calculations in the SELECT list that do not appear in the GROUP BY clause." What version of MySQL are you using? Maybe you have turned this feature off? "This extension does not apply if the ONLY_FULL_GROUP_BY SQL mode is enabled."
Mark Byers
@Mark: Lets assume you have the following data for Col1, Col2 ungrouped: {1,First}{1,Second}; If you `select Col1, Col2 from (...) group by Col1` - Which of the 2 possible unaggregated values should be selected from Col2? The query is ambiguous, and as such should **NEVER** be allowed to work.
Craig Young
A: 

The basic rule : Put all the columns from the SELECT clause into the GROUP BY clause

so

SELECT DISTINCT a,b,c FROM D

becomes

SELECT a,b,c FROM D GROUP BY a,b,c
__mme__
Correct me if I'm wrong. Shouldn't the basic rule be, put all non-aggregate columns from the SELECT clause in the GROUP BY clause?
Norla