我了解到 SQLite 在默认设置下针对吞吐量进行了优化。调整 SQLite 以实现最小(最好是可预测的)写入延迟的最佳方法是什么?我希望在所有情况下都将简单更新+提交的延迟保持在 10 毫秒以下。
我认为我需要在另一个线程上使用 WAL 和检查点,但我不确定检查点是否会阻止主线程上的编写器。
我的应用程序一次只有一个进程访问数据库,因此无需担心并发问题。数据库不损坏很重要,但持久性并不那么重要(最近的事务可能会丢失)。
最佳答案
获得低写入延迟的唯一方法是异步执行它们,即不要等待它们从缓存中刷新。
WAL documentation对此说道:
Note that with PRAGMA synchronous set to NORMAL, the checkpoint is the only operation to issue an I/O barrier or sync operation (fsync() on unix or FlushFileBuffers() on Windows). If an application therefore runs checkpoint in a separate thread or process, the main thread or process that is doing database queries and updates will never block on a sync operation. This helps to prevent "latch-up" in applications running on a busy disk drive. The downside to this configuration is that transactions are no longer durable and might rollback following a power failure or hard reset.
要防止主线程中发生检查点,请使用 PRAGMA wal_autocheckpoint .
但这仍然会带来延迟,因为检查点必须在检查点操作期间锁定整个数据库。如果您无法忍受这一点,请将检查点移至您可以负担得起的空闲时间,或者允许 WAL 文件增长到无限大小。
关于c - 我应该如何调整 SQLite 以获得最小延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42463127/