SQLite的VACUUM命令

PRAGMA auto_vacuum

The VACUUM command rebuilds the entire database. There are several reasons an application might do this:
1. Unless SQLite is running in “auto_vacuum=FULL” mode, when a large amount of data is deleted from the database file it leaves behind empty space, or “free” database pages. This means the
database file might be larger than strictly necessary. Running VACUUM to rebuild the database reclaims this space and reduces the size of the database file.
(譯文:除非SQLite運行在“auto_vacuum=FULL”模式,否則當從數據庫文件中刪除大量數據之後,就會留下很多空白空間,或者“空閒”的數據庫頁。這意味着數據庫文件的大小會比(它所存儲的數據)實際需要的(空間)更大。運行VACUUM命令將會重新構建數據庫文件,回收空白空間,減小數據庫文件的大小。)
2. Frequent inserts, updates, and deletes can cause the database file to become fragmented – where data for a single table or index is scattered around the database file. Running VACUUM ensures
that each table and index is largely stored contiguously within the database file. In some cases, VACUUM may also reduce the number of partially filled pages in the database, reducing the size of the database file further.
(譯文:頻繁的插入,更新和刪除操作,會導致數據庫文件變得支離破碎(產生大量的內存碎片)——因爲單獨一個表中的數據或者索引可能會分散的存儲在數據庫文件中。運行VACUUM命令可以確保每個表(的數據)和索引可以最大限度的連續存儲在數據庫文件中。在某些情況下,VACUUM命令也會減少數據庫中的一部分填充頁面,從而進一步減小數據庫文件的大小。)
3. Normally, the database page_size and whether or not the database supports auto_vacuum must be configured before the
database file is actually created. However, when not in write-ahead log mode, the page_size and/or auto_vacuum properties of an existing database may be changed by using the page_size and/or pragma auto_vacuum pragmas and then immediately VACUUMing the database.
When in write-ahead log mode, only the auto_vacuum support property can be changed using VACUUM.
(譯文:通常情況下,數據庫的page_size和數據庫是否支持auto_vacuum(這些配置)都要在數據庫文件實際創建之前進行配置。然而,如果SQLite不是運行在“寫前日誌(write-ahead log)”模式下,一個已經存在(已經創建)的數據庫(文件)的page_size和/或auto_vacuum屬性,可以使用page_size和/或auto_vacuum編譯指示進行修改,然後立即清掃數據庫。如果SQLite是運行在“寫前日誌”模式下,則只有(是否支持)auto_vacuum屬性可以通過VACUUM命令進行修改。)
VACUUM only works on the main database. It is not possible to VACUUM an attached database file.
(譯文:VACUUM命令只能工作在主數據庫上,不能用來“清掃”一個附加的數據庫文件。)
The VACUUM command works by copying the contents of the database into a temporary database file and then overwriting the original with the contents of the temporary file. When overwriting
the original, a rollback journal or write-ahead log WAL file is used just as it would be for any other database transaction. This means that when VACUUMing a database, as much as twice the size of the original database file is required in free disk space.
(譯文:VACUUM命令通過如下方式進行工作:首先把數據庫內容複製到一個臨時數據庫文件中,然後再把臨時數據庫文件中的內容寫回到原始數據庫文件中(以整理數據庫文件)。當將內容重新寫回到原始數據庫文件時,一個回滾日誌或者寫前日誌WAL文件將會被使用,正如它爲其它數據庫事務服務一樣。這意味着當“清掃”一個數據庫時,將需要使用高達兩倍於原始數據庫文件大小的空閒磁盤空間。)
The VACUUM command may change the ROWIDs of entries in any tables that do not have an explicit INTEGER PRIMARY KEY.
(譯文:VACUUM命令可能會改變數據庫表的行ID(ROWIDs),假如這個表沒有一個顯示的整形主鍵(INTEGER PRIMARY KEY)的話。)
A VACUUM will fail if there is an open transaction, or if there are one or more active SQL statements when it is run.
(譯文:如果存在一個打開(open)的事務,或者存在一個或多個正在活動(活躍)的(active)SQL語句,那麼VACUUM命令將會執行失敗。)
As of SQLite version 3.1, an alternative to using the VACUUM command to reclaim space after data has been deleted is auto-vacuum mode, enabled using the auto_vacuum pragma. When auto_vacuum
is enabled for a database free pages may be reclaimed after deleting data, causing the file to shrink, without rebuilding the entire database using VACUUM. However, using auto_vacuum can lead to extra database file fragmentation. And auto_vacuum does not compact
partially filled pages of the database as VACUUM does.
(譯文:對於SQLite3.1,可以使用“auto-vacuum模式”代替“VACUUM命令”在刪除數據後回收空間,可以通過使用auto_vacuum編譯指示來使能auto-vacuum模式。使能auto_vacuum模式之後,一個數據庫在刪除數據後留下的空閒頁就可能會被回收,從而縮小數據庫文件,而不用使用VACUUM命令重新構建整個數據庫。然而,使用auto_vacuum模式將會導致額外的數據庫文件碎片。並且使用auto_vacuum模式不會壓縮部分數據庫的填充頁而VACUUM命令則會(壓縮)。)
上面的內容來自SQLite官網:http://www.sqlite.org/lang_vacuum.html,下面做些補充:
(1)
VACUUM命令是SQLite的一個擴展功能,模仿PostgreSQL中的相同命令而來。在SQLite早期版本中,若調用VACUUM帶一個表名或索引名,則將整理該表或索引。後來VACUUM被重新實現,索引名或表名被忽略。
當數據庫中的一個對象(表,索引或觸發器)被撤銷,會留下空白的空間。它使數據庫比需要的大小更大,但能加快插入速度。實時的插入和刪除會使得數據庫文件結構混亂,減慢對數據庫內容訪問的速度。VACUUM命令複製主數據庫文件到臨時數據庫並從臨時數據庫重新載入到主數據庫,以整理數據庫文件。這將除去空白頁,使表數據彼此相鄰排列,並整理數據庫文件結構。不能對附加數據庫文件進行以上操作。
若當前有活動事務,該命令無法起作用。對於in-memory數據庫,該命令無效。在SQLite3.1中,可以通過使用auto-vacuum模式作爲VACUUM命令的一個替代,使用 auto_vacuum pragma開啓該模式。
(2)
使用VACUUM命令可以在刪除數據後使數據庫文件減小,在SQLIte3以後有一個替代的辦法是使用PRAGMA auto_vacuum。
PRAGMA auto_vacuum;
PRAGMA auto_vacuum = 0 | 1;
查詢或設置數據庫的auto-vacuum標記。
正常情況下,當提交一個從數據庫中刪除數據的事務時,數據庫文件不改變大小。未使用的文件頁被標記並在以後的添加操作中再次使用。這種情況下使用VACUUM命令釋放刪除後留下的空白空間。
當開啓auto-vacuum,並提交一個從數據庫中刪除數據的事務時,數據庫文件自動收縮,(VACUUM命令在auto-vacuum開啓的數據庫中不起作用)。數據庫會在內部存儲一些信息以便支持這一功能,這使得數據庫文件比不開啓該選項時稍微大一些。
另外,“auto-vacuuming must be turned on before any tables are created. It is not possible to enable or disable auto-vacuum after a table has been created”。大概意思是,當有表創建後就不能對auto-vacuum進行更改。
使用auto_vacuum僅僅是將空閒的頁除去,並不會像VACUUM那樣對數據庫進行碎片整理,或是壓縮數據庫頁。使用auto_vacuum會造成而額外的文件碎片。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章