Wednesday, June 1, 2016

In sqlite re-organizing the database

SQLite is like a big file and has tables inside that database file. Some times there is a need to delete a large number of records from the table in that case the records are deleting and leaving empty pockets behind in that large file. After the deletion the file size still remains same.

But there is a way to reduce the database file size and that can be achieved using a command VACUUM;

Below is the example on sqlite to reduce the size of database file to what is used and omitting the blank pockets out.

$ ls -l
-rw-rw-r--  1 rag rag 1627136 Jan  1 15:00 templog.db
$ sqlite3 templog.db
sqlite> . timer on
sqlite>
sqlite> vacuum;
CPU Time: user 0.360000 sys 0.250000
sqlite>
$ ls -l templog.db
-rw-rw-r-- 1 rag rag 399360 Jan  1 15:15 templog.db

you can see that the size of the database file is reduced to what is under use at the moment.

No comments: