Documentation Home
MySQL 5.7 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 35.5Mb
PDF (A4) - 35.5Mb
PDF (RPM) - 34.5Mb
EPUB - 8.7Mb
HTML Download (TGZ) - 8.5Mb
HTML Download (Zip) - 8.5Mb
HTML Download (RPM) - 7.3Mb
Eclipse Doc Plugin (TGZ) - 9.3Mb
Eclipse Doc Plugin (Zip) - 11.5Mb
Man Pages (TGZ) - 201.6Kb
Man Pages (Zip) - 306.9Kb
Info (Gzip) - 3.3Mb
Info (Zip) - 3.3Mb
Excerpts from this Manual

9.2.1.17 DISTINCT Optimization

DISTINCT combined with ORDER BY needs a temporary table in many cases.

Because DISTINCT may use GROUP BY, learn how MySQL works with columns in ORDER BY or HAVING clauses that are not part of the selected columns. See Section 13.20.3, “MySQL Handling of GROUP BY”.

In most cases, a DISTINCT clause can be considered as a special case of GROUP BY. For example, the following two queries are equivalent:

SELECT DISTINCT c1, c2, c3 FROM t1
WHERE c1 > const;

SELECT c1, c2, c3 FROM t1
WHERE c1 > const GROUP BY c1, c2, c3;

Due to this equivalence, the optimizations applicable to GROUP BY queries can be also applied to queries with a DISTINCT clause. Thus, for more details on the optimization possibilities for DISTINCT queries, see Section 9.2.1.16, “GROUP BY Optimization”.

When combining LIMIT row_count with DISTINCT, MySQL stops as soon as it finds row_count unique rows.

If you do not use columns from all tables named in a query, MySQL stops scanning any unused tables as soon as it finds the first match. In the following case, assuming that t1 is used before t2 (which you can check with EXPLAIN), MySQL stops reading from t2 (for any particular row in t1) when it finds the first row in t2:

SELECT DISTINCT t1.a FROM t1, t2 where t1.a=t2.a;

User Comments
  Posted by Mark on August 21, 2006
I have had some trouble with DISTINCT and ORDER BY, for example I could not get:
SELECT DISTINCT id,city FROM clients WHERE city like "k%" ORDER BY id DESC;
to work, as in it would not be correctly ordered.
However, doing this seems to work:
SELECT DISTINCT id,city FROM (clients inner join clients AS c on clients.id=c.id) WHERE city like "k%" ORDER BY id DESC;

I think it has to do with the way distinct is optimized, and if a a field marked in the distinct clause is a primary key, it will ignore it because it is distinct already and then it will ignore the order by. Sure, it doesn't make sense to use DISTINCT if you know you have a primary key. But if you have code where only sometimes this happens, and you don't want to completely recode everything this may be of help to you.
-Mark
  Posted by Galt Barber on August 26, 2006
Select count(distinct somefield) from sometable is very slow in all versions of mysql.
The distinct part is equivalent to:
select somefield from sometable group by somefield
although the latter sometimes runs even faster than distinct. To get reasonable speed under mysql5, please use this equivalent which uses a subquery:

select count(*) from (select distinct somefield from sometable group by somefield) as somelabel;

This is quite speedy.

(I don't know why count distinct is so slow when distinct itself isn't that slow.)
  Posted by Jim Grejoy on May 19, 2016
Might Help with some complex sql queries

SELECT timeFrame.length "year", place.info "place", supervisor.title "title", supervisor.branch "branch", meeting.date "Date"
FROM timeFrame, supervisor, place, meeeting
WHERE supervisor.title = meeting.title AND
supervisor.branch = interview.branch AND
timeFrame.timeID = meeting.timeID AND
supervisor.placeID= timeFrame.place AND
supervisor.placeID= place.placeID;
  Posted by Jim Grejoy on May 19, 2016
also

SELECT Place, count(*) FROM Testing WHERE SUBSTRING(Place,2,1) IN ('c', 'u','s') GROUP BY Place HAVING count(*)>=2 ORDER BY count(*)asc, Place DESC;

SELECT CONCAT(TESTING.name, ', ', TESTING.section ,', ', location, ', ', code) AS "Employer Info"
FROM TESTING, secondary
WHERE TESTING.companyname = secondary.companyname and
TESTING.section = secondary.section
and array='y'
ORDER BY code ASC, TESTING.companyname DESC, TESTING.section ASC;

SELECT disks FROM store WHERE disks NOT IN (SELECT cds FROM amazon);

SELECT SUBSTR(code, 1, 4) AS Time,
ROUND(MIN(min1)) "Minimum Value"
FROM Testing
GROUP BY Time;

SELECT info, COUNT(places) as 'Amount of places'
FROM testing LEFT JOIN secondTable ON testing.code = secondTable.location WHERE
INSTR(info, 'possible')
GROUP BY info;
Sign Up Login You must be logged in to post a comment.