Please, what is the limit of items that can be in a list for an SQL query using a list for WHERE condition
SELECT field1, field2, field3 from Table WHERE id IN ('val1', 'val2', 'val3', ... 'valN')
What is the limit to N?
Please, what is the limit of items that can be in a list for an SQL query using a list for WHERE condition
SELECT field1, field2, field3 from Table WHERE id IN ('val1', 'val2', 'val3', ... 'valN')
What is the limit to N?
Explicitly including an extremely large number of values (many thousands of values separated by commas) within the parentheses, in an IN clause can consume resources and return errors 8623 or 8632. To work around this problem, store the items in the IN list in a table, and use a SELECT subquery within an IN clause.
Error 8623:
The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions. Please simplify the query. If you believe you have received this message in error, contact Customer Support Services for more information.
Error 8632:
Internal error: An expression services limit has been reached. Please look for potentially complex expressions in your query, and try to simplify them.
Using IN for anything more than one or two values can be very expensive on resources. As Zaynul said a join is far more efficient.
The next step up from there for speed is to use the WHERE EXISTS
functionality. The advantages are well documented, if you have a quick google around you'll find lots of examples and the reasons why its quicker. I won't repeat everyone elses comments here.
As an example have a look at this quick code:
DECLARE @SourceTable AS TABLE (ID INT NOT NULL, SomeText NVARCHAR(255));
DECLARE @AllowedTable AS TABLE (ID INT NOT NULL);
INSERT INTO @SourceTable
VALUES (1, 'Apples')
, (2, 'Oranges')
, (3, 'Bananas')
, (4, 'Pears');
INSERT INTO @AllowedTable (ID)
VALUES (1)
, (3);
SELECT S.ID
, S.SomeText
FROM @SourceTable S
WHERE EXISTS (SELECT 1 FROM @AllowedTable A WHERE A.ID = S.ID);
The where exists looks for the presence of a record rather than content of a record so limits reads, and you also don't end up with the problems of duplicate lines that can "appear from nowhere" when the data isn't quite what you expect.