Is a series of OR or a single IN SQL statement better?

I’m using MS SQL 2008 R2. I have been using a series of OR and have an issue where my app periodically give the following error:

Exception message: Timeout expired. The timeout period elapsed prior to obtaining
a connection from the pool. This may have occurred because all pooled connections
were in use and max pool size was reached. 

My SQL skills are fairly basic but I have a situation where I need to make selections from a database. Originally the selections were simple (either a single filter or select all)

Select column_names... From Table Where column = value

Or

Select column_names... From Table

Now I need to select where column could be one of a series of items. Which would be best to use

Select column_names... From Table Where column = value1 Or column = value2 Or column = ValueX...

Or

Select column_names... From Table Where column In (value1, value2, valueX...) 

5

SQL Server will typically optimize the IN to the same as the multiple OR statements. Take this for example:

use AdventureWorks;
go

select BusinessEntityID
from HumanResources.Employee
where BusinessEntityID = 153
or BusinessEntityID = 25
or BusinessEntityID = 37;

select BusinessEntityID
from HumanResources.Employee
where BusinessEntityID in (153, 25, 37);

Looking at the execution plan I can see that SQL Server treated them as like statements and used the same execution for them both:

enter image description here

Ultimately, though, you need to do the same exercise in your environment with your queries and your data (if you have differing plans for these different variations, feel free to post the post execution plans in your question and I can analyze). And of course like all things relational data, you need to ensure that you have the proper querying and indexing in place in order to optimize the data retrieval.

The other side of that is readability/maintainability. Consistency is king here for development teams, and if this is a solo code base then I’d tack on personal preference to that. IN is a lot less verbose with characters, and the typical choice.

Just a note, though, if these conditional checks get out of control (hundreds of hundreds of elements in the IN clause, I’ve seen it before way too often) you might need to step back at what you are really doing and refactor your logic.

Your actual error is because of a different cause though…

It is worth noting though that your actual error message is a connection timeout, not a command timeout. It looks as though you’ve exhausted all pooled connections in your connection pool for the application. Take a look at this blog post I wrote on connection pooling. That should explain why you’re getting that error and how to fix. It is very possible that other sessions are running too long, and not closing or disposing of the connection to return it back to the pool. You would most definitely need to take a spearheaded approach in troubleshooting your connection timeout issues due to exhausted connection pooling.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật