Sqlite3 slow commit solution

Sqlite3 is a great database to work with prototyping or simple web application. Recently i notice that insert to database is slow event if only inserting 1 – 10 rows. Turn out i need to change PRAGMA synchronous setting of the database which is default to 2 or FULL. In FULL mode database will issue sync command to the disk for each transaction to ensure that data is safely written. This in some case is slow.

In a modern world that a power failure is rare we dont need that all the time sync to disk things. To minimize the sync we can set to 1 or NORMAL, so sqlite will sync less often than FULL mode.

To set to NORMAL mode, execute sql command below.

PRAGMA synchronous = NORMAL;

Or

PRAGMA synchronous = 1;

Install sqlite3 gem

To install sqlite3 gem we need to install development package for ruby and sqlite3 it self because of the need to compile gem native extension.

On ubuntu/mint/debian do as follow :

apt-get install ruby-dev

apt-get install libsqlite3-dev

Dont forget to execute as root.

After that we can execute gem install sqlite3.

Thats all … Bye.

Happy chinesse new year 2570 !