| DISTINCT vs GROUP BY [message #1319] |
Sat, 26 May 2007 01:34  |
mysql_beginner Messages: 5 Registered: May 2007 |
Junior Member |
|
|
Is there any performance difference between DISTINCT and GROUP BY?
I have a table articles and I want to get unique users who posted the articles. userid has an index. What should I use DISTINCT or GROUP BY? Is there any difference between their performance? I am concerned about the performance.
I need to get only userid from articles. Which one is better?
SELECT DISTINCT userid FROM articles WHERE posted_date BETWEEN '2007-01-01' AND CURDATE();
OR
SELECT userid FROM articles WHERE posted_date BETWEEN '2007-01-01' AND CURDATE() GROUP BY userid;
EXPLAIN is exactly the same for both.
|
|
|
|
|
| Re: DISTINCT vs GROUP BY [message #1325 is a reply to message #1319 ] |
Sat, 26 May 2007 08:52  |
sterin Messages: 323 Registered: March 2007 Location: Sweden |
Senior Member |
|
|
There is no performance difference.
Since, as Speeple said, mysql is performing the exact same execution to perform the two queries.
So essentially it is just a matter of syntax in this case, the end result is the same.
[Updated on: Sat, 26 May 2007 08:55]
|
|
|