Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQLite Vacuum

SQLite Vacuum

The VACUUM command works by copying the contents of the main database to a temporary database file, then clearing the main database, and reloading the original database file from the copy. This eliminates free pages, arranges table data contiguously, and also cleans up the database file structure.

If a table has no explicit integer primary key (INTEGER PRIMARY KEY), the VACUUM command may change the row IDs (ROWID) of entries in the table. The VACUUM command only applies to the main database; it is not possible to use the VACUUM command on attached database files.

The VACUUM command fails if there is an active transaction. The VACUUM command is a no-op for in-memory databases. Since the VACUUM command recreates the database file from scratch, VACUUM can also be used to modify many database-specific configuration parameters.

Manual VACUUM

The following is the syntax for issuing a VACUUM command on the entire database at the command prompt:



$sqlite3 database_name "VACUUM;"

You can also run VACUUM at the SQLite prompt, as shown below:



sqlite> VACUUM;

You can also run VACUUM on a specific table, as shown below:



sqlite> VACUUM table_name;

Auto-VACUUM (Auto-VACUUM)

SQLite's Auto-VACUUM is not quite the same as VACUUM; it simply moves free pages to the end of the database, thereby reducing the database size. By doing so, it can significantly fragment the database, whereas VACUUM defragments it. So Auto-VACUUM only makes the database smaller.

At the SQLite prompt, you can enable/disable SQLite's Auto-VACUUM by compiling with the following:



sqlite> PRAGMA auto_vacuum = NONE;  -- 0 means disable auto vacuum

sqlite> PRAGMA auto_vacuum = INCREMENTAL;  -- 1 means enable incremental vacuum

sqlite> PRAGMA auto_vacuum = FULL;  -- 2 means enable full auto vacuum

You can run the following command from the command prompt to check the auto-vacuum setting:



$sqlite3 database_name "PRAGMA auto_vacuum;"