Search code examples
sqlsql-serverwhere-in

Does SQL performance degrade as the number elements in an "IN" clause increases?


I have a query like this,

SELECT Name FROM Customers WHERE Id IN (1,4,3,6,7)

There might be millions of customers in the DataBase. Will there be an efficiency problem with this query ? When the number of Ids inside IN statement are more ? If so, Why and Any workaround ?

I Use SQLServer. Below is my table Structure

Id|Name|Designation

Id is the primary key -non clustered index.


Solution

  • Assuming you have properly indexed the Id column, there should be no problem. That is the correct method, and if it does not work, you need a new database. (Millions shouldn't be an issue with most regular pieces of software; if you make it to multiple billions you might need to investigate clustered databases).