Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-07-2013, 02:35 PM   PM User | #1
sitechooser
New Coder

 
Join Date: Jan 2013
Posts: 16
Thanks: 6
Thanked 0 Times in 0 Posts
sitechooser is an unknown quantity at this point
More Efficient Query Needed

Hi All

Does anyone have an idea how to run this query...

Code:
SELECT * 
FROM [TableName] 
WHERE [ColumnName] IN
(
      SELECT [ColumnName] 
      FROM [TableName] 
      GROUP BY [ColumnName] 
      HAVING COUNT(*) > 1 
)
ORDER BY [ColumnName]
without the sub query?

The sub query option seems to be taking ages to run. Is there a more efficient query that does the same thing?

Thanks in advance for any help.
sitechooser is offline   Reply With Quote
Old 03-07-2013, 03:32 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
SELECT [ColumnName]
FROM [TableName]
GROUP BY [ColumnName] <= Not needed because that is the only thing your getting
HAVING COUNT(*) > 1 <= This makes no sense to me because count() tells you the number of rows. Do you mean where the value of the ColumnName column is large then 1?
sunfighter is offline   Reply With Quote
Old 03-07-2013, 07:55 PM   PM User | #3
sitechooser
New Coder

 
Join Date: Jan 2013
Posts: 16
Thanks: 6
Thanked 0 Times in 0 Posts
sitechooser is an unknown quantity at this point
More efficient query

Sorry, it would help if I explained what I'm trying to do!

The query is supposed to search a column (name) for duplicate values and then return all data in the table which has duplicate values in that column

Any ideas?
sitechooser is offline   Reply With Quote
Old 03-07-2013, 08:24 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,198
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Nope, I think you have it right.

Though doing it like this *MIGHT* be more efficient:
Code:
SELECT T1.*
FROM table AS T1,
     ( SELECT column, COUNT(column) AS cnt
       FROM table 
       GROUP BY column
       HAVING cnt > 1
    ) AS T2
WHERE T1.column = T2.column
ORDER BY T1.column
Or even possibly (though I doubt it)
Code:
SELECT T1.*
FROM table AS T1,
     ( SELECT column, COUNT(column) AS cnt
       FROM table 
       GROUP BY column
    ) AS T2
WHERE T1.column = T2.column AND T2.cnt > 1
ORDER BY T1.column
May not matter, but you could try it.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 03-07-2013, 08:25 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,198
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
It will, of course, make a *HUGE* difference if column is indexed.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
sitechooser (03-07-2013)
Old 03-07-2013, 10:21 PM   PM User | #6
sitechooser
New Coder

 
Join Date: Jan 2013
Posts: 16
Thanks: 6
Thanked 0 Times in 0 Posts
sitechooser is an unknown quantity at this point
Magic!

I ran the query on an 80MB table and it returned in 6.5 minutes. I then made the column concerned an index, ran the same query and it returned in 7.6 seconds! Bit of a difference

Thanks a million for your help, got a critical meeting tomorrow morning and you'll make me look like superman.
sitechooser is offline   Reply With Quote
Old 03-07-2013, 11:26 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,198
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
LOL! The cardinal rule of database design: When it's slow, index it.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:31 AM.


Advertisement
Log in to turn off these ads.