且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么清除 SQLite 数据库不会减小其大小?

更新时间:2023-02-13 23:34:21

来自这里:

当一个对象(表、索引、触发器或视图)从数据库中删除时,它会留下空白空间.下次将新信息添加到数据库时,将重新使用此空白空间.但与此同时,数据库文件可能比严格需要的要大.此外,频繁的插入、更新和删除会导致数据库中的信息变得碎片化——分散在整个数据库文件中,而不是聚集在一个地方.

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.

VACUUM 命令通过将主数据库的内容复制到临时数据库文件并从副本重新加载原始数据库文件来清理主数据库.这会消除空闲页面,将表数据对齐为连续的,否则会清理数据库文件结构.

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.