Sqlite: Create databases with journal_mode=WAL
When EF creates SQLite databases (via EnsureCreated or Migrations), we should consider setting the journaling mode to write-ahead logging.
Pros:
- Faster
- Better concurrency (less locking)
Cons:
- Not for databases accessed via a network file share
@ericsink Any reason not to? (in EF Core, not the ADO.NET provider)
IMO, WAL mode in general is better for ~98% of cases.
#ifdef QUIBBLE
Note that it is not universally true that WAL mode is faster. In fact, in some cases involving reads, it's slower: https://www.sqlite.org/wal.html#performance_considerations
#endif
But in general, using WAL mode is good, and will probably be the right choice for most users. I use WAL mode all the time on all of my projects.
That said, a wrapper that automatically sets WAL mode can be a problem. The sqlite-net library recently made a change to use WAL mode all the time, and it has caused a number of problems.
See praeclarum/sqlite-net#765 for more info.
If you make this change to EF Core, it will probably benefit most users, but it will very likely cause trouble for somebody.
I would agree that making this kind of change at a higher abstraction layer (like EF Core) is much more appropriate to consider than at lower layers (like the ADO.NET provider).
I’d like to mitigate the risk by only setting it on new databases (not for every connection). If you find out that you don’t want it, you can just send the PRAGMA in a migration or something to disable it.
Please, if you do, give the developers a working opportunity to opt out. I have an app which use sqlite-net and their defaulting to WAL in the connection constructor in the latest version completely broke my export data functionality, which relayed on the fact that all the data was in the sqlite file, which is no more true when WAL is enabled. Tried to invoke PRAGMA wal_checkpoint(FULL) before exporting, but this only mitigate the issue, in that most often than not the most recent data are not copied from the -wal file to the sqlite one. Also, closing the connection does not force the -wal file to be emptied (maybe a bug in sqlite-net implementation, I don't know). Disabling WAL with pragma journal_mode does not work either consistently (that is: almost never). If someone knows a proper way to export data when WAL is enabled I would be glad to know, but still, I think that is better to have the choice to opt out at the constructor level.
So how to disable WAL today?
@ahdung After the database is created by EF, you can turn off WAL using by ececuting this SQL on it:
PRAGMA journal_mode = 'delete';
@bricelam Thank you very much.
@bricelam We have an issue with setting the journaling mode #36513. It is taking 26 seconds to run. We have to be able to use the sqlite db file in a network share. Is there anyway to drastically speed up the setting of the journaling mode.
@dgxhubbard minimizing as off-topic here. Your conversation is in progress in #36513 and where the delay is coming from seems like it could be orthogonal.