SQL IN list limits
The per-engine ceilings on IN lists and the standard ways around them.
- 카테고리
- 조건부
- Returns
boolean- In this category
- 17 of 43
구문
x IN (SELECT id FROM temp_keys) | x = ANY (ARRAY[...])
엔진 지원
| 엔진 | Support | 메모 |
|---|---|---|
| SQL Server | Different name or syntax | 2100 parameter limit per request |
| PostgreSQL | 예 | no hard limit, but giant lists plan and cache badly |
| MySQL | 예 | Supported under this name. |
| SQLite | Different name or syntax | SQLITE_MAX_VARIABLE_NUMBER: default 999 before 3.32, then 32766 |
| Oracle | Different name or syntax | ORA-01795: maximum 1000 expressions in a list |
예시
PostgreSQL
SELECT * FROM t WHERE id = ANY($1::bigint[]);
SQL Server
CREATE TABLE #keys (id INT PRIMARY KEY); INSERT INTO #keys VALUES (1), (2); SELECT * FROM orders o JOIN #keys k ON o.id = k.id;
알아 두면 좋은 정보
- Oracle counts expressions: 1000 literals max. Because the list is usually generated, ORA-01795 tends to surface only when production data first pushes it past 1000.
- SQL Server's 2100 limit counts every parameterized value in the request, so other parameters eat into it.
- SQLite's cap is on bound variables; it jumped from 999 to a 32766 default in 3.32.
- The durable fix is a temp table plus IN (subquery) or a join; on PostgreSQL, x = ANY($1::bigint[]) squeezes the list into one parameter.
Questions about IN
Why does Oracle fail with ORA-01795?
More than 1000 expressions in one IN list; split the list or load the keys into a table.
What is the SQLite limit on IN?
Bound variables: 999 before 3.32, 32766 by default after, overridable at compile time.
Best fix for huge ID lists?
A temp table with a primary key and a join: the planner gets statistics instead of a wall of literals.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.