Please login or register.

Login with username, password and session length
Advanced search  

News:

We are constantly trying to improve PHP Freaks and these forums, so feel free to go to the PHPFreaks Comments/Suggestions board and point out anything you'd like to see different!

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 3507 times)

0 Members and 1 Guest are viewing this topic.

sphinx9999

  • Irregular
  • Offline Offline
  • Posts: 30
    • 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?
Logged

Daniel0

  • Administrator
  • Freak!
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 8,567
    • View Profile
    • WWW
Re: DISTINCT or GROUP BY
« Reply #1 on: February 14, 2008, 10:41:32 AM »
It depends on what you're trying to do.
Logged
Rules | PHP manual | FAQ/Code Snippet Repository | How To Ask Questions The Smart Way | PHP Freaks Tutorials | Stop using "or die()"! | Latest PHP Freaks Content
Need any custom coding done?
Quote from: ober
wtf... what were the quotes??  And why, besides CV, would anyone ever quote me?

How many Microsoft employees does it take to switch a lightbulb? None, they just redefined "dark" to be the new lightness standard.

aschk

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

jaymc

  • Devotee
  • Offline Offline
  • Gender: Male
  • Posts: 1,470
    • 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
Logged
I would love to change the world, but they won't give me the source code

freenity

  • Enthusiast
  • Offline Offline
  • Posts: 66
    • View Profile
    • WWW
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

fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Insane!'
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 11,596
    • 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.
Logged
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

goldfiles

  • Irregular
  • Offline Offline
  • Posts: 28
    • View Profile
    • WWW
Re: DISTINCT or GROUP BY
« Reply #6 on: April 15, 2008, 11:36:18 PM »
Group By usually
Logged
TextAdMarket - http://www.TextAdMarket.com
Buy & Sell Text ADs

abs0lut

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

PHP Freaks Forums

 
 
 

Page created in 0.054 seconds with 23 queries.