On Wed, Dec 16, 2015 at 1:37 AM, Dominique Pell? <dominique.pelle at gmail.com> wrote:
> Scott Robison <scott at casaderobison.com> wrote: > > > Why would that be of benefit to you? Are you intending to attach a > database > > and never use it? It seems to me the same amount of time will be taken > > either way. > > > > When it comes to opening a database, there may be a need to do some > > connection specific configuration prior to actually opening the database > > file and parsing the schema. I believe this is the reason why open defers > > that processing until later, giving you a chance to finish configuring > your > > connection before locking it down. Once that configuration is complete, > > there is no advantage to deferring the open of the database. > > > > I say no advantage ... maybe I just can't think of one. Why do you think > > there would be an advantage to deferring the open & schema processing of > an > > attached database? > > Laziness can be useful in some cases. I have an application > that opens hundred or so of database connections. Being able to open > all connections at start-up is simple. Since it's lazy, it's also fast and > does > not use memory to store schemas until the databases are actually > used later. In my application, queries happen in only few connections > after start-up out of all opened connections. For many connections, > queries happen much later or sometimes do not even happen. Laziness > is thus useful to make start-up fast and simple, without application having > to implement laziness itself. > > I see that the original message from ??? says "Because there are so > many database [...]", so it seems to be the same scenario as in my > application in which laziness is quite useful. I'm not 100% sure but I'm > quite confident that laziness is the explanation for performance > discrepancy between sqlite3_open*() and ATTACH. > > If laziness was useless, why would it then be already implemented > for sqlite3_open_v2(...)? > As I indicated above, in the case of SQLite, it isn't about lazy. It is about deferring opening the database to give the programmer a chance to do any further configuration of the connection that must be done prior to creating or opening the actual database file and reading / parsing the schema (pragma auto_vaccum, pragma encoding, perhaps sqlite3_db_config, maybe others). Those are operations that may require a connection that has not yet processed a schema. In any other case of 'lazy' loading (which I agree can be a valuable technique and I have used it myself), it can be implemented in your own code. By tracking what databases you've attached and only attaching them on first use, rather than attaching them all in the beginning. -- Scott Robison