Announcement

Announcement Module
Collapse
No announcement yet.

DISTINCT vs GROUP BY

Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • DISTINCT vs GROUP BY

    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.

  • #2
    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.

    Comment


    • #3
      I am looking for some definite answer.

      Comment


      • #4
        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.

        Comment

        Working...
        X