2012年7月5日 星期四

[SQL] GROUP BY VS DISTINCT

Distinct 會等於或比快 Group by ,快的話在於,distinct的欄位剛好用到key



If you don't, then use DISTINCT.
GROUP BY in MySQL sorts results. You can even do:
SELECT u.profession FROM users u GROUP BY u.profession DESC
and get your professions sorted in DESC order.
DISTINCT creates a temporary table and uses it for storing duplicates. GROUP BY does the same, but sortes the distinct results afterwards.
So
SELECT DISTINCT u.profession FROM users u
is faster, if you don't have an index on profession.


http://stackoverflow.com/questions/581521/whats-faster-select-distinct-or-group-by-in-mysql

沒有留言:

張貼留言