Scott Robison <scott at casaderobison.com> wrote:

> On Tue, Dec 15, 2015 at 11:19 PM, Dominique Pell? <dominique.pelle at 
> gmail.com
>> wrote:
>
>> ??? <2004wqg2008 at 163.com> wrote:
>>
>> >
>> >         After testing the Sqlite3_open and ATTACH DATABASE,
>> >         I found that the attach database is slower than sqlite3_open.
>> >         there is attachment after the mail which includ the speed
>> >         information ( millisecond ).
>>
>>
>> Your attachment was discarded (attachment not allowed in this
>> mailing list).
>>
>> Anyway, I remember observing that:
>>
>> - sqlite3_open_v2(...) is lazy.  In other words, it does not parse the
>>   schema of the DB until the first query is performed after opening
>>   the database.
>> - ATTACH is not lazy. The schema is parsed as soon as you
>>   attach a database.
>>
>> That could explain the difference in speed.
>> Would there be a way to make ATTACH lazy by the way?
>>
>
> 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(...)?

Having said all that, reading https://www.sqlite.org/c3ref/open.html
I see no mention of the fact that sqlite3_open*() is lazy.
Is it documented somewhere?

Regards
Dominique

Reply via email to