Home » Performance » MySQL » DISTINCT vs GROUP BY
DISTINCT vs GROUP BY [message #1319] Sat, 26 May 2007 01:34 Go to next message
mysql_beginner  is currently offline 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 #1322 is a reply to message #1319 ] Sat, 26 May 2007 07:24 Go to previous messageGo to next message
Speeple  is currently offline Speeple
Messages: 91
Registered: August 2006
Member
It'll reduce to the same pathway for both, however I prefer to use "GROUP BY" as it seems more logical to me and increases usability IMO.


Martin Gallagher | Speeple: The latest news
Re: DISTINCT vs GROUP BY [message #1323 is a reply to message #1319 ] Sat, 26 May 2007 07:58 Go to previous messageGo to next message
mysql_beginner  is currently offline mysql_beginner
Messages: 5
Registered: May 2007
Junior Member
I am looking for some definite answer.
Re: DISTINCT vs GROUP BY [message #1325 is a reply to message #1319 ] Sat, 26 May 2007 08:52 Go to previous message
sterin  is currently offline 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]

Previous Topic:MySQL Optimal Settings
Next Topic:InnoDB DELETE/INSERT vs. UPDATE in transaction
Goto Forum:
  



Current Time: Tue Jan 6 18:14:35 EST 2009

Total time taken to generate the page: 0.02129 seconds
.:: Contact :: Home :: MySQL Support by Percona.com ::.

Powered by: FUDforum 2.7.5.
Copyright ©2001-2006 FUD Forum Bulletin Board Software

MySQL Performance | Forum authority Badge