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?
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?
SELECT Code
FROM YourTable
GROUP BY Code
vs
SELECT DISTINCT Code
FROM YourTable
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.
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