On 5/26/17, Wout Mertens <wout.mert...@gmail.com> wrote: > > Are the above assumptions correct? Any other calls (besides opening the db) > that can take a long time?
Most of the work associated with opening the database connection (which is to say, parsing the schema) is deferred until the first time you call sqlite3_prepare_v2(). Note also that if another process modifies the schema (for example by running CREATE TABLE or CREATE INDEX) then SQLite will automatically reparse the whole schema the next time you run sqlite3_step() for previously prepared statements. Note that parsing the schema involves calling sqlite3_exec() to run an SQL statement that reads the schema - something that is possible because SQLite is reentrant. See https://www.sqlite.org/src/artifact/b1140c3d0?ln=298 for the recursive call to sqlite3_exec() and https://www.sqlite.org/src/artifact/b1140c3d0?ln=289-291 for the SELECT statement it uses for this. Then for each CREATE statement in the schema, SQLite recurses yet one more time to parse that statement as well. See https://www.sqlite.org/src/artifact/b1140c3d0?ln=84 for the second level recursion. Prior to the second recursion, SQLite sets flags that tell the parser not to actually create the tables and indexes, but just build up its internal symbol table. Hence, the statement that gets prepared does not actually get stepped. The side-effect of building the symbol tables, which is what we want, happens during the preparation. All of this is to say there is a lot going on under the hood, and all that mechanism is deferred until the last possible moment, which means it can happen just about anytime. It is not as simple as saying "all the hard work is done during sqlite3_step()". Normally a schema parse takes microseconds - SQLite's parser will normally churn though 100K or more SQL statements per second - but it can be longer depending on how big the schema is. -- D. Richard Hipp d...@sqlite.org _______________________________________________ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users