`

VACUUM command in SQLite

 
阅读更多

http://www.sqlite.org/lang_vacuum.html

 

The VACUUM command rebuilds the entire database. There are several reasons an application might do this:

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

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

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

VACUUM only works on the main database. It is not possible to VACUUM an attached database file.

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.

The VACUUM command may change the ROWIDs of entries in any tables that do not have an explicit 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.

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.

分享到:
评论

相关推荐

    C# SQLite执行效率的优化教程

    foreach (DataRow row in table.Rows) { list.Add(new Test { Name = row["Name"].ToString(), Age = int.Parse(row["Age"].ToString()) }); } return list; }, "执行SELECT命令"); } } } 五、执行效率...

    SQLite数据库文件自收缩_sqlite3_

    在提供的压缩包文件“SQLite数据库文件自收缩.pdf”中,可能包含了关于如何使用VACUUM和PRAGMA语句进行数据库文件收缩的详细步骤、注意事项以及最佳实践。通过阅读这份文档,你可以深入理解这两个方法的工作原理,并...

    SQLite入门与分析

    本篇文章将深入探讨SQLite的基础知识,包括虚拟机、存储模型以及VACUUM命令。 首先,我们来关注SQLite的虚拟机。在SQLite入门与分析(七)---浅谈SQLite的虚拟机.doc中,主要讲解了SQLite如何通过虚拟机执行SQL语句。...

    SQLite3的性能优化

    SQLite3 的性能优化 SQLite3 是一个轻量级的关系数据库管理系统,常用于嵌入式系统中。在嵌入式系统中,数据库的性能优化尤为重要。本文将主要讲述在嵌入式中使用 SQLite3 时,对相关的数据库的性能如何优化的问题...

    SQLITE数据库 UPDATE慢

    7. **VACUUM操作**:SQLite的VACUUM命令可以整理数据库文件,释放被删除记录占用的空间,提高写入速度。定期运行VACUUM可以帮助保持数据库的高效运行。 8. **缓存策略**:调整SQLite的缓存大小可以改善性能。增大 ...

    SQLITE 数据库安装包

    9. **性能优化**:SQLite允许你创建索引以加速查询,并且可以通过VACUUM命令进行碎片整理,提高数据库读写速度。 10. **分布式使用**:尽管SQLite主要用于单用户应用,但通过网络协议,它也可以实现多用户访问。...

    sqlite 3.7.13

    3. **VACUUM命令**:SQLite 3.7.13支持VACUUM命令,用于优化数据库文件结构,清理碎片,以及在需要时缩小数据库文件的大小。 4. **触发器和存储过程**:SQLite允许用户创建触发器和存储过程,扩展了数据库的功能,...

    SQLite权威指南第二版源代码

    6. **VACUUM操作**:SQLite提供VACUUM命令用于优化和压缩数据库,源代码可能包含何时及如何使用VACUUM的示例。 7. **错误处理与日志**:源代码可能会展示如何捕获和处理SQLite运行时的错误,以及如何启用和查看...

    sqlite database browser windows版

    8. 存储过程:虽然SQLite不直接支持存储过程,但可以通过VACUUM、ANALYZE等内置命令来优化数据库。 9. 权限控制:尽管SQLite是单用户数据库,但依然可以设置表级别的权限,如读写权限。 10. 报表设计:生成报表和...

    sqlite 3.7.8 官方开发文档

    SQLite是一款开源、轻型的数据库管理系统,广泛应用于嵌入式系统和移动应用中。SQLite 3.7.8是该系统的一个版本,它在2011年发布,提供了许多功能改进和性能优化。这份官方开发文档是开发者理解和操作SQLite 3.7.8的...

    sqlite数据库 大数据量处理demo

    5. **VACUUM命令**:SQLite提供VACUUM命令用于重组数据库文件,回收不再使用的空间,尤其是在频繁进行大量插入和删除操作后,这有助于保持数据库的高效运行。 6. **内存管理**:优化内存使用可以提升性能。例如,...

    sqlite官方文档全集

    SQLite是一款开源、轻量级的嵌入式关系型数据库,被广泛应用于移动设备、桌面系统以及服务器环境。它以其小巧的体积、强大的功能和高度的可靠性赢得了全球开发者的喜爱。这个"sqlite官方文档全集"包含了SQLite的完整...

    一个小时内学习 SQLite 数据库

    此外,SQLite 提供了一些实用的命令,如 `pragma table_info(table_name)` 可以显示表的结构信息,`vacuum` 用于优化和清理数据库,`pragma integrity_check` 或 `pragma quick_check` 用于检查数据库的完整性。...

    SQlite数据库笔记总结详解

    - 定期VACUUM操作,整理数据库碎片,提升读写速度。 7. SQLite安全性: - 权限控制:SQLite提供PRAGMA statements来设置权限,如PRAGMA key='password'对数据库加密。 - 数据备份:通过复制数据库文件实现简单...

    SQLite资料

    SQLite的SQL语法 SQLite库可以解析大部分标准SQL语言。但它也省去了一些特性 并且加入了一些自己的新特性。...INSERT ON CONFLICT子句 PRAGMA REINDEX REPLACE ROLLBACK TRANSACTION SELECT UPDATE VACUUM

    sqlite开发文档全手册

    - 数据库文件损坏:尝试使用VACUUM或REPAIR命令修复。 - 内存管理:合理配置内存池,避免内存泄漏。 - 锁定问题:理解锁的机制,避免死锁。 综上所述,"SQLite3开发API手册"是开发者理解和利用SQLite3进行数据库...

    Sqlite数据库支持库

    还可以通过VACUUM命令进行数据库优化,整理碎片,提高读写效率。 8. **备份与恢复**:SQLite提供了备份API,允许在运行时创建数据库的完整副本,用于数据恢复或迁移。 9. **内存数据库模式**:除了传统的磁盘...

    SQLite3性能优化

    SQLite3是一款轻量级的数据库引擎,广泛应用于嵌入式设备和移动应用中。由于它的体积小、无需服务器进程,因此在许多场合下是理想的选择。然而,随着数据量的增长和复杂查询的增加,性能优化变得至关重要。本文将...

    sqlite单文件源码

    10. **性能优化**:SQLite提供了各种优化选项,如索引、VACUUM操作、WAL模式等,以提高数据库的性能。 通过深入学习和理解这些知识点,开发者可以充分利用SQLite的强大功能,将其集成到各种应用程序中,实现数据的...

    sqlite3.7.4源码

    VACUUM是SQLite特有的维护命令,用于整理数据库文件,回收空间,以及重建B树以优化性能。 9. **扩展功能** SQLite允许通过编写C语言的扩展模块来增加新的函数和虚拟表。这为用户提供了极大的灵活性,可以根据需求...

Global site tag (gtag.js) - Google Analytics