删除大量行后,数据库大小保持不变

2024-04-26 09:26:20 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
3条回答

“的大小”数据库.db“也许不等于桌子的大小。你知道吗

这并不奇怪。数据库是文件(或其他存储单元)的集合。这些文件将数据存储在称为数据页的东西上。数据页包含行。表是逻辑结构,其行存储在数据页上。你知道吗

从表中删除记录时,会更改表示该表的数据页(甚至可能会删除一些)。但是,您不会更改数据库,只更改中的数据。你知道吗

Gordon已经解释了为什么磁盘上的数据库大小没有改变。对于SQLite,可以使用VACUUM命令在大量删除之后将数据库重建到最小的空间中。你知道吗

官方文件在解释这些事情时通常都很枯燥。更多信息可以在here找到。一些片段:

SQLite first copies data within a database file to a temporary database. This operation defragments the database objects, ignores the free spaces, and repacks individual pages. Then, SQLite copies the content of the temporary database file back to the original database file. The original database file is overwritten.

The VACUUM command does not change the content of the database except the rowid values. If you use INTEGER PRIMARY KEY column, the VACUUM does not change values of that column. However, if you use unaliased rowid, the VACUUM command will reset the rowid values. Besides changing the rowid values, the VACUUM command also builds the index from the scratch.

It is a good practice to perform the VACUUM command periodically, especially when you delete the large table or index.

It is important to note that the VACCUM command requires storage for hold the original file and also the copy.

相关问题 更多 >