12

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 Answers 4

8

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.

6
  • 2
    Those two queries aren't the same, are they? In the second one you're only selecting distinct IDs, not distinct names as well. Dec 11, 2009 at 14:30
  • This won't work with any other T-SQL, or it won't be faster with any other one? Dec 11, 2009 at 14:57
  • 1
    Well the second one doesn't work!!! Any Column not included in the GROUP BY clause requires an aggregation function. Dec 11, 2009 at 14:59
  • 1
    Craig Young: really? 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
    Dec 11, 2009 at 19:14
  • 1
    @CraigYoung: I think the key point is 'directly related' which means that it only works reliably when there is a functional dependency CustomerID ⟶ CustomerName, whereas your example by definition does not have the equivalent functional dependency. In the absence of the functional dependency, you're correct. AFAICT MySQL doesn't actually care (or know?) whether the functional dependency exists; it just assumes it does and pulls matching values for a given grouped-by column 'at random'. Jun 3, 2012 at 7:05
1
SELECT Code
FROM YourTable
GROUP BY Code

vs

SELECT DISTINCT Code
FROM YourTable
1
  • 4
    how is this an answer to the q? Am I missing something?
    – nawfal
    Jun 3, 2012 at 7:05
0

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
1
  • 1
    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? Dec 14, 2009 at 14:54
-2

Example.

Relation customer(ssnum,name, zipcode, address) PK(ssnum). ssnum is social security number.

SQL:

 Select DISTINCT ssnum from customer where zipcode=1234 group by name

This SQL statement returns unique records for those customer's that have zipcode 1234. At the end results are grouped by name.

Here DISTINCT is no not necessary. because you are selecting ssnum which is already unique because ssnun is primary key. two person can not have same ssnum.

In this case Select ssnum from customer where zipcode=1234 group by name will give better performance than "... DISTINCT.......".

DISTINCT is an expensive operation in a DBMS.

1
  • What is the difference in the answer when you have GROUP BY and no GROUP BY? What is the difference in the answer when you have DISTINCT and no DISTINCT (is that 'indistinct'?)? AFAICS, the queries would all yield the same result set (give or take ordering, which doesn't count), though not necessarily all at the same speed. Jun 3, 2012 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.