"IN" clause limitation in Sql Server

ecfsfe2w  于 2023-04-19  发布在  SQL Server
关注(0)|答案(5)|浏览(219)

Does anybody know what is the limit for the number of values one can have in a list of expressions (to test for a match) for the IN clause?

c7rzv4ha

c7rzv4ha1#

Yes, there is a limit, but Microsoft only specifies that it lies "in the thousands" :
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.

Looking at those errors in details, we see that this limit is not specific to IN but applies to query complexity in general:

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.

z9smfwbn

z9smfwbn2#

It is not specific but is related to the query plan generator exceeding memory limits. I can confirm that with several thousand it often errors but can be resolved by inserting the values into a table first and rephrase the query as

select * from b where z in (select z from c)

where the values you want in the in clause are in table c. We used this successfully with an in clause of 1-million values.

guykilcj

guykilcj3#

Depending on the database engine you are using, there can be limits on the length of an instruction.

SQL Server has a very large limit:

Maximum Capacity Specifications for SQL Server

So, for large IN clauses, it's better to create a temp table, insert the values and do a JOIN. It works faster also.

There is a limit, but you can split your values into separate blocks of in()

Select * 
From table 
Where Col IN (123,123,222,....)
or Col IN (456,878,888,....)

use a table valued parameter in 2008, or some approach described here

Arrays and Lists in SQL Server 2005

wlp8pajw

wlp8pajw4#

Depending on how you execute the query (JDBC, Hiberante, some kind of SQL-GUI) and when using literal values instead of a sub-query where X in (1, 2, 3...) - you may also encounter the following error:
Too many parameters were provided in this RPC request. The maximum is 2100.

So the limit would be 2100 (or even less when other parameters are present, too) in those cases.

yfwxisqw

yfwxisqw5#

The SQL Server JDBC driver has a limit of 2100 and throws a SQLServerException:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:265) ~[mssql-jdbc-10.2.1.jre8.jar:na]
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1676) ~[mssql-jdbc-10.2.1.jre8.jar:na]
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:615) ~[mssql-jdbc-10.2.1.jre8.jar:na]
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:537) ~[mssql-jdbc-10.2.1.jre8.jar:na]
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7730) ~[mssql-jdbc-10.2.1.jre8.jar:na]
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:3786) ~[mssql-jdbc-10.2.1.jre8.jar:na]
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:268) ~[mssql-jdbc-10.2.1.jre8.jar:na]
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:242) ~[mssql-jdbc-10.2.1.jre8.jar:na]
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:456) ~[mssql-jdbc-10.2.1.jre8.jar:na]

相关问题