Do Oracle SQL DBs Remove duplicate entries in a where-in clause?

半腔热情 提交于 2019-12-12 03:52:04

问题


I apologize if this is a duplicate question: I've searched but finding many non-related topics/questions.

For instance

select * 
from table_of_things 
where id in (1, 2, 3, 4, 5, 6, 1, 2, 1)

Will Oracle pre-processing remove the duplicates before performing the query? How can I find out? I imagine in large lists the performance loss can be huge and it would be good to remove duplicates myself before querying if not.


回答1:


Like you, I couldn't find any specific documentation entry that specifies if the expression list gets simplified for performance reasons. It must be considered an implementation detail. But based on observation, it does appear like the optimizer performs the optimization.

On Oracle 12c, I ran an example query much like yours with autotrace turned on, and it listed the filter predicates it used. As expected, the list of predicates was simplified to eliminate the duplicates.

Use autotrace on your query if you want to check it out for yourself.




回答2:


Sort of. Or, more relevantly, it is a totally unimportant question how the logic is implemented.

IN follows simple logic. It evaluates to true when the left operand is in the list following the IN. It evaluates to equally true when the operand is in the list multiple times.

In practice, I'm guessing that Oracle does remove duplicates during the compiling phase. But, the definition of IN is the same regardless of whether or not duplicates are present.



来源:https://stackoverflow.com/questions/37666282/do-oracle-sql-dbs-remove-duplicate-entries-in-a-where-in-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!