简体   繁体   English

在iOS中使用SQLite PRAGMA同步= OFF和journal_mode = MEMORY进行更新的最坏情况行为

[英]Worst case behavior for updates using SQLite PRAGMA synchronous = OFF and journal_mode = MEMORY in iOS

By using both the synchronous=OFF and journal_mode=MEMORY options, I am able to reduce the speed of updates from 15 ms to around 2 ms which is a major performance improvement. 通过同时使用sync = OFF和journal_mode = MEMORY选项,我能够将更新速度从15毫秒降低到2毫秒左右,这是一项重大的性能改进。 These updates happen one at a time, so many other optimizations (like using transactions about a bunch of them) are not applicable. 这些更新一次发生一次,因此许多其他优化(例如使用大约一堆的事务)都不适用。

According to the SQLite documentation, the DB can go 'corrupt' in the worst case if there is a power outage of some type. 根据SQLite文档,如果发生某种类型的停电,则DB在最坏的情况下可能会“崩溃”。 However, is not the worst thing that can happen is for the data to be lost, or possibly part of a transaction to be lost (which I guess is a form of corruption). 但是,丢失数据或丢失事务的一部分(可能是损坏的一种形式)并不是最糟糕的事情。 Is it really possible for arbitrary corruption to occur with either of these options? 这些选项中的任意一个是否真的可能发生任意损坏? If so, why? 如果是这样,为什么?

I am not using any transactions, so partially written data from transactions is not a concern, and I can handle loosing data once in a blue moon. 我没有使用任何事务,因此不必担心从事务中部分写入数据,并且我可以在蓝色月亮中处理一次丢失的数据。 But if 'corruption' means that all the data in the DB can be randomly changed in an unpredictable way, that would be a strong reason to not use these options. 但是,如果“损坏”意味着可以以不可预测的方式随机更改数据库中的所有数据,那将是不使用这些选项的强烈理由。

Does any one know what the real worst-case behavior would be on iOS? 有人知道iOS上最糟糕的情况是什么吗?

Tables are organized as B-trees with the rowid as the key. 表被组织为B树,以rowid为键。 If some writes get lost while SQLite is updating the tree structure, the entire table might become unreadable. 如果在SQLite更新树结构时丢失一些写操作,则整个表可能变得不可读。 (The same can happen with indexes, but those could be simply dropped and recreated.) (索引也可能发生这种情况,但是可以简单地将其删除并重新创建。)

Data is organized in pages (typically 1 KB or 4 KB). 数据以页面组织(通常为1 KB或4 KB)。 If some page update gets lost while some tree is being reorganized, all the data in these pages (ie, some random rows from the table with nearby rowid values) might become corruped. 如果在重组某些树时丢失某些页面更新,则这些页面中的所有数据(即表中具有附近rowid值的一些随机行)可能会失效。

If SQLite needs to allocate a new page, and that page contains plausible data (eg, deleted data from the same table), and the writing of that page gets lost, then you have incorrect data in the table, without the ability to detect it. 如果SQLite需要分配一个新页面,并且该页面包含合理的数据(例如,从同一表中删除的数据),并且该页面的写入操作丢失,则表中的数据不正确,无法检测到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM