sql server 'in' or 'or' - which is fastest

前端 未结 3 1490
自闭症患者
自闭症患者 2021-01-05 01:20

Ok, so I have a query:

select distinct(a)
from mytable
where
b in (0,3)

What is going to be faster, the above or

select di         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-05 01:33

    Hopefully in this simple example it won't make any difference which version you use (as the query optimiser should turn them into equivalent queries under the hood), however there's a fair chance it's going to be dependent on the indexes you have on mytable. I would suggest that you run both queries in Sql Server Management Studio after having turned on "Include Actual Execution Plan", and compare the results to determine which query has the lowest "cost" in your scenario.

    To do this:

    1. Put your query(s) into a new Sql Sever Management Studio query window
    2. Right click on the window in the space you've typed into
    3. Click "Include Actual Execution Plan"
    4. Run your query as you would usually

    The bottom "results" half of the window will now have a 3rd tab showing, "Execution Plan" which should contain two "flowcharts", one for the first query and another for the second. If the two are identical, then Sql Server has treated the two queries as equivalent and therefore you should choose whichever form you and/or your colleagues prefer.

提交回复
热议问题