Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.
SELECT id
FROM user
WHERE id
IN ( 2,3,4,7,9,21,23,45,324,12,43,254 )
UNION
SELECT DISTINCT user_id
FROM workgroup_users
WHERE workgroup_id
IN ( 1,345,34,654,213,123,5,2,3,4 )

I know we should not use the IN clause because it degrades performance in index columns. I want id from user and users which belong to one of the listed workgroups. Below are the table structures:

workgroup_users
 user_id
 workgroup_id

user
 id
 username
share|improve this question
    
Show us the CREATE TABLE statements so we know the datatypes, foreign keys and indexes available. It would also help if you posted the EXPALIN plan of the query you have. –  ypercube Aug 21 '13 at 8:05
    
What's wrong with your query ? Doesn't it return what you want ? It seems OK to me. –  user1598390 Aug 21 '13 at 8:55

1 Answer 1

you can use UNION ALL instead of UNION as you have specified a DISTINCT. UNION ALL won't need to deduplicate the rows. It might help

share|improve this answer
    
No, then he might have different results (duplicates). He can remove the DISTINCT from the second SELECT though. –  ypercube Aug 21 '13 at 6:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.