高性能 SQLite 的选项

发布于 2024-12-06 23:23:24 字数 447 浏览 2 评论 0原文

我正在开发一个嵌入式系统,它需要非常频繁地存储和检索数据。我预计每秒可能有 100 次写入和 10 次读取。数据将突发而不是连续到达。

我想使用 SQLite 来存储所有这些数据,但由于它是闪存文件系统,因此写入(插入和更新)太慢。我尝试设置 synchronous=off ,这确实提高了性能,但我担心会损坏数据库。

在我的应用中,断电的情况很少见,但确实有可能发生。我可以忍受丢失一些最近的数据,但绝对不能冒因数据库损坏而丢失所有数据的风险。

我还考虑过使用内存 SQLite 数据库作为主数据库,并定期将其同步到文件系统,如 将 sqlite 数据库从内存同步到文件

我还应该考虑其他选项吗?

I'm developing an embedded system, which needs to store and retrieve data very frequently. I am expecting perhaps 100s of writes per second and 10s of reads. The data will arrive in bursts rather than continuous.

I would like to use SQLite to store all this data, but since it is a flash file-system, the writes (INSERTS & UPDATES) are much too slow. I tried setting synchronous=off and this does certainly improve the performance, but I am nervous about corrupting the database.

In my application, power failures would be a rare, but real possibility. I can live with losing some recent data, but absolutely cannot risk losing all the data due to a database corruption.

I also thought about using an in-memory SQLite database as the primary database and periodically sync it to the file-system as discussed in Synchronizing sqlite database from memory to file

Are there any other options I should consider?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晒暮凉 2024-12-13 23:23:24

当大量数据到达时,请确保在单个事务中执行一系列插入和更新。

sqlite3_exec(handle, "BEGIN TRANSACTION", NULL, NULL, NULL);

for ( ... ) {
    // Do your inserts/updates
}

sqlite3_exec(handle, "END TRANSACTION", NULL, NULL, NULL);

默认情况下,SQLite 将每个插入/更新放在自己的事务中。如果底层文件系统很慢,这可能会使 SQLite 变得非常慢。通过声明您自己的事务,您可以显着减少对文件系统执行的实际写入量,从而显着提高 SQLite 的性能。

When a burst of data arrives, be sure to do the series of inserts and updates within a single transaction.

sqlite3_exec(handle, "BEGIN TRANSACTION", NULL, NULL, NULL);

for ( ... ) {
    // Do your inserts/updates
}

sqlite3_exec(handle, "END TRANSACTION", NULL, NULL, NULL);

By default, SQLite puts every insert/update in its own transaction. This can make SQLite quite slow if the underlying filesystem is slow. By declaring your own transactions, you can significantly reduce the amount of actual writing that is done to the filesystem, thereby substantially increasing SQLite's performance.

硬不硬你别怂 2024-12-13 23:23:24

我不知道你的数据集有多大,但如果它们不是太大,那么 WAL 模式可能会有所帮助。您可以尝试"PRAGMA synchronous=1":此设置不会在每次事务后同步,但偶尔。 (默认值:写入 2 MB 的新数据时。)SQLite 文档说您可能会丢失一些最近的事务,但数据库不会损坏。

I don't know how large are your data sets, but if they are not too big, then the WAL mode might help. You may experiment with "PRAGMA synchronous=1": This setup does not sync after each transaction, but once in a while. (Default: When 2 MB of new data is written.) SQLite docs say that you might loose a few recent transactions, but the DB won't be corrupted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文