Please login or register.

Login with username, password and session length
Advanced search  

News:

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

Maintenance Notice

PHPFreaks has successfully moved to a new Dedicated Server, hosted by Server Powered. Please help support future upgrades by Donating.

Poll

Question: 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 3229 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: 6,053
    • 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 | 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?

aschk

  • Guru
  • Devotee
  • *
  • Offline Offline
  • Posts: 1,046
    • 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,362
    • 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: 10,241
    • 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
Pages: [1]
 

Page created in 0.044 seconds with 21 queries.