on 2012 May 15 1:43 PM
having a query like this:
select * from T where ID in (1,1,1,1,1)
does it have any negative effect, if inside the IN list an entry appears multiple times like in the example above.
In a word, no. The predicates ID in (1,1,1,1)
and ID in (1)
are semantically equivalent.
The server software automatically eliminates duplicates in IN-lists where the size of the list is "sufficiently small" that we can incur the overhead of the sort (to eliminate the duplicate values) at compile time. This can only be done if all of the IN-list elements are known at optimization time - they must either be literal values, variables, or hostvars.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What about IN with a subquery that returns duplicate values - like this one:
select * from T where ID in ((select SomeNonUniqueColumn from T1))
Can I conclude that the optimizer will eliminate duplicates in "SomeNonUniqueColumn" as well (treating this somewhat like "select distinct SomeNonUniqueColumn")?
The optimizer has a number of rewrite and execution-time strategies for computing the result of nested queries, and it knows that, semantically, it doesn't matter if the subquery contains DISTINCT or not. So it doesn't really matter if you include DISTINCT or not in the original statement.
Indeed, I second the motion. Duplicates in an IN clause are NOT a problem.
However, I can say from experience that an IN clause on large number of items performs worse than an equivalent join ..
So ..
instead of doing:
select * from DataTable where ID in ( select ID from LookupTable )
try this instead:
select dt.* from ( select distinct ID from LookupTable ) x, DataTable dt where x.ID = dt.id
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
75 | |
10 | |
10 | |
10 | |
10 | |
9 | |
8 | |
7 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.