The SQLite optimizations must be carried out at two different times: once in a unique and permanent way for the database and another time for each connection that is made. Below are the configurations that should be made in each case.
These configurations are set only once and affect the database persistently, meaning they do not need to be reconfigured each time a connection is established:
Sets the database journal mode to "WAL" (Write-Ahead Logging), which improves performance in concurrent operations.
Defines the database page size to 32768 bytes. This change is persistent but can only be made when the database is empty.
Configures the auto-vacuum mode to "incremental", allowing space to be reclaimed without blocking the database.
These configurations need to be set each time a connection to the database is made, as they only affect the current session:
Controls the synchronization policy, balancing security and performance.
Defines the page cache size. A negative value indicates the size is specified in kilobytes, in this case, 20 MB.
Enables foreign key constraint enforcement to ensure data referential integrity.
Indicates that temporary tables should be stored in memory, which can speed up temporary operations.
Sets a timeout of 5000 milliseconds for locks, improving the database's ability to handle concurrency.
Sets the memory mapping space size to 2 GB, which can enhance performance by allowing more data to be directly mapped into memory.
Performs an incremental vacuum to reduce the database size. This process permanently frees up space.