📄 limits.tcl
字号:
## Run this script to generate the limits.html output file#set rcsid {$Id: limits.tcl,v 1.3 2007/06/18 12:22:43 drh Exp $}source common.tclheader {Implementation Limits For SQLite}puts {<h2>Limits In SQLite</h2><p>"Limits" in the context of this article means sizes orquantities that can not be exceeded. We are concernedwith things like the maximum number of bytes in aBLOB or the maximum number of columns in a table.</p><p>SQLite was originally designed with a policy of avoidingarbitrary limits.Of course, every program that runs on a machine with finitememory and disk space has limits of some kind. But in SQLite, those limitswere not well defined. The policy was that if it would fitin memory and you could count it with a 32-bit integer, thenit should work.</p><p>Unfortunately, the no-limits policy has been shown to createproblems. Because the upper bounds where not welldefined, they were not tested, and bugs (including possiblesecurity exploits) where often found when pushing SQLite toextremes. For this reason, newer versions of SQLite havewell-defined limits and those limits are tested as part ofthe test suite.</p><p>This article defines what the limits of SQLite are and how theycan be customized for specific applications. The default settingsfor limits are normally quite large and adequate for almost everyapplication. Some applications may what to increase a limit hereor there, but we expect such needs to be rare. More commonly,an application might want to recompile SQLite with much lowerlimits to avoid excess resource utilization in the event ofbug in higher-level SQL statement generators or to help thwart attackers who inject malicious SQL statements.</p>}proc limititem {title text} { puts "<li><p><b>$title</b></p>\n$text</li>"}puts {<ol>}limititem {Maximum length of a string or BLOB} {<p>The maximum number of bytes in a string or BLOB in SQLite is definedby the preprocessor macro SQLITE_MAX_LENGTH. The default valueof this macro is 1 billion (1 thousand million or 1,000,000,000).You can raise or lower this value at compile-time using a command-line option like this:</p><blockquote>-DSQLITE_MAX_LENGTH=123456789</blockquote><p>The current implementation will only support a string or BLOBlength up to 2<small><sup>31</sup></small>-1 or 2147483647. Andsome built-in functions such as hex() might fail well before thatpoint. In security-sensitive applications it is best not totry to increase the maximum string and blob length. In fact,you might do well to lower the maximum string and blob lengthto something more in the range of a few million if that ispossible.</p><p>During part of SQLite's INSERT and SELECT processing, the completecontent of each row in the database is encoded as a single BLOB.So the SQLTIE_MAX_LENGTH parameter also determines the maximumnumber of bytes in a row.</p>}limititem {Maximum Number Of Columns} {<p>The SQLITE_MAX_COLUMN compile-time parameter is used to set an upperbound on:</p><ul><li>The number of columns in a table</li><li>The number of columns in an index</li><li>The number of columns in a view</li><li>The number of terms in the SET clause of an UPDATE statement</li><li>The number of columns in the result set of a SELECT statement</li><li>The number of terms in a GROUP BY or ORDER BY clause</li><li>The number of values in an INSERT statement</li></ul><p>The default setting for SQLITE_MAX_COLUMN is 2000. You can change itat compile time to values as large as 32676. You might be able toredefine this value to be as large as billions, though nobody has evertried doing that so we do not know if it will work. On the other hand, thereare people who will argue that a well-normalized database designwill never need a value larger than about 100.</p><p>In most applications, the number of columns is small - a few dozen.There are places in the SQLite code generator that use algorithmsthat are O(N²) where N is the number of columns. So if you redefine SQLITE_MAX_COLUMN to be areally huge number and you generate SQL that uses a large number ofcolumns, you may find that <a href="capi3ref.html#sqlite3_prepare_v2">sqlite3_prepare_v2()</a>runs slowly.}limititem {Maximum Length Of An SQL Statement} {<p>The maximum number of bytes in the text of an SQL statement is limited to SQLITE_MAX_SQL_LENGTH which defaults to 1000000. Youcan redefine this limit to be as large as the smaller of SQLITE_MAX_LENGTHand 1073741824. </p><p>If an SQL statement is limited to be a million bytes in length, thenobviously you will not be able to insert multi-million byte stringsby embedding them as literals inside of INSERT statements. Butyou should not do that anyway. Use host parameters for your data. Prepare short SQL statements like this:</p><blockquote>INSERT INTO tab1 VALUES(?,?,?);</blockquote><p>Then use the<a href="capi3ref.html#sqlite3_bind_text">sqlite3_bind_XXXX()</a> functionsto bind your large string values to the SQL statement. The use of bindingobviates the need to escape quote characters in the string, reducing therisk of SQL injection attacks. It is also runs faster since the largestring does not need to be parsed or copied as much.</p>}limititem {Maximum Number Of Tables In A Join} {<p>SQLite does not support joins containing more than 64 tables.This limit arises from the fact that the SQLite code generatoruses bitmaps with one bit per join-table in the query optimizer.</p>}limititem {Maximum Depth Of An Expression Tree} {<p>SQLite parses expressions into a tree for processing. Duringcode generation, SQLite walks this tree recursively. The depthof expression trees is therefore limited in order to avoidusing too much stack space.</p><p>The SQLITE_MAX_EXPR_DEPTH parameter determines the maximum expressiontree depth. If the value is 0, then no limit is enforced. Thecurrent implementation has a default value of 1000.</p>}limititem {Maximum Number Of Arguments On A Function} {<p>The SQLITE_MAX_FUNCTION_ARG parameter determines the maximum numberof parameters that can be passed to an SQL function. The default valueof this limit is 100. We know of no technical reason why SQLite would not work with functions that have millions of parameters. However, we suspect that anybody who triesto invoke a function with millions of parameters is reallytrying to find security exploits in systems that use SQLite, not do useful work, and so for that reason we have set this parameter relatively low.}limititem {Maximum Number Of Terms In A Compound SELECT Statement} {<p>A compound SELECT statement is two or more SELECT statements connectedby operators UNION, UNION ALL, EXCEPT, or INTERSECT. We call eachindividual SELECT statement within a compound SELECT a "term".</p><p>The code generator in SQLite processes compound SELECT statements usinga recursive algorithm. In order to limit the size of the stack, wetherefore limit the number of terms in a compound SELECT. The maximumnumber of terms is SQLITE_MAX_COMPOUND_SELECT which defaults to 500.We think this is a generous allotment since in practice we almostnever see the number of terms in a compound select exceed single digits.</p>}limititem {Maximum Length Of A LIKE Or GLOB Pattern} {<p>The pattern matching algorithm used in the default LIKE and GLOBimplementation of SQLite can exhibit O(N²) performance (whereN is the number of characters in the pattern) for certain pathologicalcases. To avoid denial-of-service attacks from miscreants who are ableto specify their own LIKE or GLOB patterns, the length of the LIKEor GLOB pattern is limited to SQLITE_MAX_LIKE_PATTERN_LENGTH bytes.The default value of this limit is 50000. A modern workstation canevaluate even a pathological LIKE or GLOB pattern of 50000 bytesrelatively quickly. The denial of service problem only comes intoplay when the pattern length gets into millions of bytes. Nevertheless,since most useful LIKE or GLOB patterns are at most a few dozen bytesin length, paranoid application developers may want to reduce thisparameter to something in the range of a few hundred if they know thatexternal users are able to generate arbitrary patterns.</p>}limititem {Maximum Number Of Host Parameters In A Single SQL Statement} {<p>A host parameter is a place-holder in an SQL statement that is filledin using one of the<a href="capi3ref.html#sqlite3_bind_blob">sqlite3_bind_XXXX()</a> interfaces.Many SQL programmers are familiar with using a question mark ("?") as ahost parameter. SQLite also supports named host parameters prefacedby ":", "$", or "@" and numbered host parameters of the form "?123".</p><p>Each host parameter in an SQLite statement is assigned a number. Thenumbers normally begin with 1 and increase by one with each newparameter. However, when the "?123" form is used, the host parameternumber is the number that follows the question mark.</p><p>The maximum value of a host parameter number is SQLITE_MAX_VARIABLE_NUMBER.This setting defaults to 999.</p>}limititem {Maximum Number Of Attached Databases} {<p>The <a href="lang_attach.html">ATTACH</a> statement is an SQLite extensionthat allows two or more databases to be associated to the same databaseconnection and to operate as if they were a single database. The numberof simulataneously attached databases is limited to SQLITE_MAX_ATTACHEDwhich is set to 10 by default.The code generator in SQLite uses bitmapsto keep track of attached databases. That means that the number ofattached databases cannot be increased above 30 on a 32-bit machineor 62 on a 64-bit machine.}limititem {Maximum Database Page Size} {<p>An SQLite database file is organized as pages. The size of eachpage is a power of 2 between 512 and SQLITE_MAX_PAGE_SIZE.The default value for SQLITE_MAX_PAGE_SIZE is 32768. The currentimplementation will not support a larger value.</p><p>It used to be the case that SQLite would allocate some stackstructures whose size was proportional to the maximum page size.For this reason, SQLite would sometimes be compiled with a smallermaximum page size on embedded devices with limited stack memory. Butmore recent versions of SQLite put these large structures on theheap, not on the stack, so reducing the maximum page size is nolonger necessary on embedded devices.</p>}limititem {Maximum Number Of Pages In A Database File} {<p>SQLite is able to limit the size of a database file to preventthe database file from growing too large and consuming too muchdisk or flash space.The SQLITE_MAX_PAGE_COUNT parameter, which is normally set to1073741823, is the maximum number of pages allowed in a singledatabase file. An attempt to insert new data that would causethe database file to grow larger than this will returnSQLITE_FULL.</p><p>The <a href="pragma.html#pragma_max_page_count">max_page_count PRAGMA</a> can be used to raise or lower thislimit at run-time.</p><p>Note that the transaction processing in SQLite requires two bitsof heap memory for every page in the database file. For databasesof a few megabytes in size, this amounts to only a few hundredbytes of heap memory. But for gigabyte-sized databases the amountof heap memory required is getting into the kilobyte range andfor terabyte-sized databases, megabytes of heap memory must beallocated and zeroed at each transaction. SQLite willsupport very large databases in theory, but the current implementationis optimized for the common SQLite use cases of embedded devicesand persistent stores for desktop applications. In other words,SQLite is designed for use with databases sized in kilobytes or megabytes not gigabytes. If you are building an application towork with databases that are hundreds of gigabytes or more in size, then you should perhaps consider using a different database engine that is explicitly designed for such large data sets.</p>}puts {</ol>}footer $rcsid
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -