SQL Server: Select in vs or?

2019-01-24 00:23发布

Which is faster?

SELECT UserName
FROM dbo.UserTable
WHERE UserID in (1,3,4)

SELECT UserName
FROM dbo.UserTable
WHERE UserID = 1 
      OR UserID = 3
      OR UserID = 4

3条回答
不美不萌又怎样
2楼-- · 2019-01-24 00:50

Actually it is the same.

If you display the estimated execution plan you will see that it is performing the same action.

查看更多
做自己的国王
3楼-- · 2019-01-24 00:54

This is relevant when it comes to producing SQL via Linq. There are some instances where the sql created is in the form of: field='xxx' OR field='yyy'.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-24 01:07

Due to Sql Server's optimization of queries these will run at the same speed since they are logically equivalent.

i favor the IN syntax for brevity and readability though.

查看更多
登录 后发表回答