IN vs = in Where Clauses

被刻印的时光 ゝ 提交于 2019-12-24 10:47:06

问题


I try to create standardized SQL scripts for use in my SSRS reports. If I wish to have a statement such as:

Select * from mytable

and use a SQL Variable (SSRS parameter) in the where clause, is there any speed advantage to using this:

Where field = @MyField

VS.

Where field IN (@MyField)

I know the second option supports both multiple selections and single selections while the first supports only single.

Are there any performance hits if I write all my queries with the IN statement for uniformity?


回答1:


There is no performance hit. The optimizer converts the IN to a series of = operations ORed together.

As a side note, I hope you're not really using SELECT * in your actual queries.




回答2:


For one argument optimizer will change that IN into =, in real execution statement.

As both of this arguments are Sargeble (Joe Stefanelli thanks for attention); the difference in performance may by found where you have sequence value in in clause, for example IN (1,2,3,4,5,6) is slower that between 1 and 6, because for the IN the index has to be read for each value in this case 6 time for between only once.




回答3:


the performance does get slow if you have a large no of item in your IN clause



来源:https://stackoverflow.com/questions/3407577/in-vs-in-where-clauses

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