在“ DELETE FROM table”之后更改 sqlite 文件大小

我正在处理 sqlite3文件。

首先,我输入了比较大的数据库,文件大小大约是100mb。

比我赚的多

$db->exec("DELETE FROM table");

并且只输入了数据库的一小部分,但是文件大小仍然是100mb。

在删除 sqlite 文件的内容时,您应该如何更改它的文件大小?

37543 次浏览

The command you are looking for is vacuum. There is also a pragma to turn auto-vacuuming on.

From the documentation:

When an object (table, index, trigger, or view) is dropped from the database, it leaves behind empty space. This empty space will be reused the next time new information is added to the database. But in the meantime, the database file might be larger than strictly necessary. Also, frequent inserts, updates, and deletes can cause the information in the database to become fragmented - scrattered out all across the database file rather than clustered together in one place.

The 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.

Cleaning Databases SQLite has two commands designed for cleaning—reindex and vacuum.

reindex is used to rebuild indexes. It has two forms:

reindex collation_name;
reindex table_name|index_name;

vacuum has the form:

VACUUM;

You can do this

$db->exec("DELETE FROM table");
$db->exec("vacuum");

and the file size will be changed.