Poll

Which is best to use when extracting one column, DISTINCT or GROUP BY?

Distinct
 1 (20%)
Group by
 1 (20%)
No difference
 3 (60%)

Total Members Voted: 5

Author Topic: DISTINCT or GROUP BY  (Read 3738 times)

0 Members and 1 Guest are viewing this topic.

Offline sphinx9999Topic starter

  • Irregular
    • View Profile
DISTINCT or GROUP BY
« on: February 14, 2008, 05:56:25 AM »
Which do you think is better, DISTINCT or GROUP BY?
Code: [Select]
SELECT DISTINCT id FROM phrase WHERE campaign LIKE 'proper%'
or
Code: [Select]
SELECT id FROM phrase WHERE campaign LIKE 'proper%' GROUP BY advert_id
I've asked this question before and received the following answer from a reliable source: "When there's just one column it doesn't matter". Do you agree?

Offline Daniel0

  • Former Admin
  • Staff Alumni
  • 'Insane!'
  • *
  • Gender: Male
  • ^bb|[^b]{2}$
    • View Profile
    • degeberg.com
Re: DISTINCT or GROUP BY
« Reply #1 on: February 14, 2008, 10:41:32 AM »
It depends on what you're trying to do.

Offline aschk

  • Guru
  • Devotee
  • *
    • View Profile
Re: DISTINCT or GROUP BY
« Reply #2 on: February 14, 2008, 10:47:36 AM »
GROUP BY

Offline jaymc

  • Addict
  • Gender: Male
    • View Profile
Re: DISTINCT or GROUP BY
« Reply #3 on: February 15, 2008, 10:46:54 AM »
Benchmark it ;-)

phpmyadmin is good, it tells you time taken to execute a query
I would love to change the world, but they won't give me the source code

Offline freenity

  • Enthusiast
    • View Profile
    • Gaming With PHP
Re: DISTINCT or GROUP BY
« Reply #4 on: February 26, 2008, 12:52:08 PM »
I know old topic, but have a look at this: http://gwphp.feudal-times.net/?p=5
benchmarked and everything :)

Guess who wins

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Insane!'
  • *
  • Gender: Male
    • View Profile
Re: DISTINCT or GROUP BY
« Reply #5 on: February 27, 2008, 12:50:21 PM »
This is mainly because distinct is a filesort operation which group by has the chance to use an index or some temporary filesorting on smaller sets along the way.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline goldfiles

  • Irregular
    • View Profile
    • TextAdMarket
Re: DISTINCT or GROUP BY
« Reply #6 on: April 16, 2008, 12:36:18 AM »
Group By usually
TextAdMarket - http://www.TextAdMarket.com
Buy & Sell Text ADs

Offline abs0lut

  • Enthusiast
    • View Profile
Re: DISTINCT or GROUP BY
« Reply #7 on: August 24, 2008, 07:54:09 AM »
i will not use distinct anymore

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.