DEV Community

Paul Lefebvre
Paul Lefebvre

Posted on

6 1

Speed up SQLite with Write-Ahead Logging (WAL)

When you update a SQLite Database within a transaction, the standard behavior is that the original data is copied to a separate rollback file and the new data is written directly to the DB file. This is reliable, but it results in two disk writes for every DB change. When you COMMIT, the rollback file is removed since it is no longer needed. Should you instead ROLLBACK, then the data is restored to the DB file from the rollback file.

To make SQLite even faster you can enable a feature called Write-Ahead Logging (aka WAL). WAL can be faster because it reverses the behavior described above. With WAL, instead of modifying the DB file itself and maintaining a separate rollback file, each change to the SQLite Database is written to a separate "write-ahead logging file" (which typically ends in "-wal"). This results in just a single disk write.

Additionally, because of the separate files, an app with multiple threads is able to read from the DB while it is being written to. And vice versa. This is an especially nice benefit for web apps that need a DB, but do not require a full-featured database server.

To enable WAL, you can use the SQLite Pragma command:

PRAGMA journal_mode=WAL;
Enter fullscreen mode Exit fullscreen mode

In Xojo code, you send the Pragma command to the SQLite DB using the SQLExecute method:

DB.SQLExecute("PRAGMA journal_mode=WAL;")
Enter fullscreen mode Exit fullscreen mode

Or more simply you can set the SQLiteDatabase.MultiUser property to True:

DB.MultiUser = True
Enter fullscreen mode Exit fullscreen mode

The data in the separate WAL file will at some point have to be transferred back to the original database. This is called a "checkpoint". You can do these manually or let SQLite handle them automatically, which according to the SQLite docs happens when the WAL file reaches a threshold of 1000 pages.

You can manually initiate a checkpoint using the wal_checkpoint Pragma command:

PRAGMA schema.wal_checkpoint;
Enter fullscreen mode Exit fullscreen mode

In Xojo code, you can send this Pragma command to the DB using the SQLExecute method:

DB.SQLExecute("PRAGMA schema.wal_checkpoint;")
Enter fullscreen mode Exit fullscreen mode

There are downsides to using WAL. Performance for reads can suffer when there is a large WAL file because data has to be searched in two places. The official SQLite docs on WAL also list some of the other downsides.

But for most types of usage, WAL is worth using. Try it with your apps to see if they get a benefit.

This article original appeared here: SQLite Write-Ahead Logging

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry πŸ•’

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more β†’

Top comments (0)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay