Documentation Home
MySQL 5.7 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 38.9Mb
PDF (A4) - 39.0Mb
PDF (RPM) - 38.1Mb
HTML Download (TGZ) - 10.8Mb
HTML Download (Zip) - 10.8Mb
HTML Download (RPM) - 9.5Mb
Man Pages (TGZ) - 210.9Kb
Man Pages (Zip) - 320.0Kb
Info (Gzip) - 3.5Mb
Info (Zip) - 3.5Mb
Excerpts from this Manual

8.2.1.16 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 12.19.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 8.2.1.15, “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
User comments in this section are, as the name implies, provided by MySQL users. The MySQL documentation team is not responsible for, nor do they endorse, any of the information provided here.
  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.