Search code examples
iossqliteselect

SQLite error: too many terms in compound SELECT


When I insert too many data into sqlite database file, an error "too many terms in compound SELECT" occurs. I use "insert into ... select ... union select ... union ...". I know it's too many select statements, but my question is: What's the maximum number of terms in a compound SELECT statement?


Solution

  • From the official documentation at http://www.sqlite.org/limits.html:

    A compound SELECT statement is two or more SELECT statements connected by operators UNION, UNION ALL, EXCEPT, or INTERSECT. We call each individual SELECT statement within a compound SELECT a "term".

    The code generator in SQLite processes compound SELECT statements using a recursive algorithm. In order to limit the size of the stack, we therefore limit the number of terms in a compound SELECT. The maximum number of terms is SQLITE_MAX_COMPOUND_SELECT which defaults to 500. We think this is a generous allotment since in practice we almost never see the number of terms in a compound select exceed single digits.

    The maximum number of compound SELECT terms can be lowered at run-time using the sqlite3_limit(db,SQLITE_LIMIT_COMPOUND_SELECT,size) interface.