C语言 SQLite DB 中的 -shm 和 -wal 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12928294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:07:26  来源:igfitidea点击:

-shm and -wal files in SQLite DB

csqliteunixsqlite-journal-mode

提问by Manik Sidana

I am taking the backup of SQLite DB using cp commmand after running wal_checkpoint(FULL). The DB is being used in WAL mode so there are other files like -shm and -wal in my folder. When I run wal_checkpoint(FULL), the changes in WAL file get committed to the database. I waqs wondering whether -wal and -shm files get deelted after running a checkpoint. If not, then what do they contain ?

在运行 wal_checkpoint(FULL) 后,我正在使用 cp 命令备份 SQLite DB。DB 正在 WAL 模式下使用,因此我的文件夹中有 -shm 和 -wal 等其他文件。当我运行 wal_checkpoint(FULL) 时,WAL 文件中的更改会提交到数据库。我想知道 -wal 和 -shm 文件是否在运行检查点后被删除。如果没有,那么它们包含什么?

I know my backup process is not good since I am not using SQLite backup APIs. This is a bug in my code.

Can anyone please suggest what content do -shm and -wal files contain after running checkpoint.

我知道我的备份过程不好,因为我没有使用 SQLite 备份 API。这是我代码中的一个错误。

任何人都可以建议 -shm 和 -wal 文件在运行检查点后包含哪些内容。

Any link provided would be helpful.

提供的任何链接都会有所帮助。

Thanks

谢谢

回答by Manik Sidana

After searching through numerous sources, I believe the following to be true:

在查阅了大量资料后,我相信以下说法是正确的:

  1. The -shmfile contains an index to the -walfile. The -shmfile improves performance when reading the -walfile.
  2. If the -shmfile gets deleted, it get created again during next database access.
  3. If checkpointis run, the -walfile can be deleted.
  1. -shm文件包含该文件的索引-wal。该-shm文件提高了读取-wal文件时的性能。
  2. 如果-shm文件被删除,它会在下次访问数据库时再次创建。
  3. 如果checkpoint运行,则-wal可以删除该文件。

To perform safe backups:

要执行安全备份:

  1. It is recommended that you use SQLite backup functions for making backups. SQLite library can even make backups of an online database.
  2. If you don't want to use (1), then the best way is to close the database handles. This ensures a clean and consistent state of the database file, and deletes the -shmand -walfiles. A backup can then be made using cp, scpetc.
  3. If the SQLite database file is intended to be transmitted over a network, then the vacuumcommand should be run after checkpoint. This removes the fragmentation in the database file thereby reducing its size, so you transfer less data through network.
  1. 建议您使用 SQLite 备份功能进行备份。SQLite 库甚至可以备份在线数据库。
  2. 如果您不想使用(1),那么最好的方法是关闭数据库句柄。这确保了数据库文件的干净和一致状态,并删除-shm-wal文件。备份然后可以使用制造cpscp等等。
  3. 如果SQLite数据库文件旨在通过网络将被发送,则该真空命令应后运行checkpoint。这会消除数据库文件中的碎片,从而减小其大小,从而减少通过网络传输的数据。

回答by CL.

The -shmfile does not contain any permanent data.

-shm文件不包含任何永久数据。

When the last connection is closed, the database is automatically checkpointed, and the -walfile is then deleted. This implies that after a checkpoint, and if no other connections exist, the -walfile does not contain any important data.

当最后一个连接关闭时,数据库会自动设置检查点,-wal然后删除该文件。这意味着在检查点之后,如果不存在其他连接,则该-wal文件不包含任何重要数据。

If possible, you should close the connection before taking the backup.

如果可能,您应该在进行备份之前关闭连接。