Sqlite 简明教程

SQLite - VACUUM

VACUUM 命令通过将数据库内容复制到临时数据库文件并从副本重新加载原始数据库文件的方式来清理主数据库。这一操作消除了空页,使表数据平铺,并且以其他方式清理数据库文件结构。

VACUUM command cleans the main database by copying its contents to a temporary database file and reloading the original database file from the copy. This eliminates free pages, aligns table data to be contiguous, and otherwise cleans up the database file structure.

VACUUM 命令可能会更改没有明确整数主键的表中条目的 ROWID。VACUUM 命令仅适用于主数据库。不能对附加数据库文件进行 VACUUM。

VACUUM command may change the ROWID of entries in tables that do not have an explicit INTEGER PRIMARY KEY. The VACUUM command only works on the main database. It is not possible to VACUUM an attached database file.

如果存在活动事务,那么 VACUUM 命令将失败。对于内存中数据库而言,VACUUM 命令是一个无操作符。由于 VACUUM 命令会从头重建数据库文件,所以 VACUUM 还可以用于修改许多特定于数据库的配置参数。

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

Manual VACUUM

以下是从命令提示符向整个数据库发出 VACUUM 命令的简单语法:

Following is a simple syntax to issue a VACUUM command for the whole database from command prompt −

$sqlite3 database_name "VACUUM;"

你可以这样从 SQLite 提示符运行 VACUUM:

You can run VACUUM from SQLite prompt as well as follows −

sqlite> VACUUM;

还可以这样对特定表运行 VACUUM:

You can also run VACUUM on a particular table as follows −

sqlite> VACUUM table_name;

Auto-VACCUM

SQLite 自动 VACUUM 和 VACUUM 不同,它只会将空页移动到数据库的末尾,从而减小数据库大小。这样一来,它会显著使数据库碎片化,而 VACUUM 确保对碎片进行整理。因此,自动 VACUUM 只是保持数据库较小。

SQLite Auto-VACUUM does not do the same as VACUUM rather it only moves free pages to the end of the database thereby reducing the database size. By doing so it can significantly fragment the database while VACUUM ensures defragmentation. Hence, Auto-VACUUM just keeps the database small.

你可以通过在 SQLite 提示符处运行以下编译指示来启用/禁用 SQLite 自动 VACUUM:

You can enable/disable SQLite auto-vacuuming by the following pragmas running at SQLite prompt −

sqlite> PRAGMA auto_vacuum = NONE; -- 0 means disable auto vacuum
sqlite> PRAGMA auto_vacuum = FULL; -- 1 means enable full auto vacuum
sqlite> PRAGMA auto_vacuum = INCREMENTAL; -- 2 means enable incremental vacuum

你可以从命令提示符运行以下命令来检查自动 VACUUM 设置:

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

$sqlite3 database_name "PRAGMA auto_vacuum;"