0

What would be better way to implement such view so that the query doesn't take too long.

select * from table
where ID in (
SELECT ID FROM table
GROUP BY ID
HAVING COUNT(ID) > 1
)

Our server will need to run this every 10 mins. I thought of Indexing ID but wasn't sure if that would be the right way to go.

2
  • 1
    Are you using MySQL or SQL Server? You've tagged both. I'm assuming the former since that's where the performance problems seem t be... Commented Aug 12, 2013 at 22:27
  • 1
    As a rule of thumb, JOIN performs better as it is explained below. I wanted to add that you definitively need an index on ID. Commented Aug 12, 2013 at 22:33

1 Answer 1

2
select t.* from table t
join
(
SELECT ID FROM table
GROUP BY ID
HAVING COUNT(ID) > 1
) a
on a.id=t.id
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks everyone! With our DBA gone and me left in charge, you all came to the rescue!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.