gpt4 book ai didi

c++ - 从多个线程将数据写入 sqlite 的最佳方法

转载 作者:行者123 更新时间:2023-11-30 05:22:11 28 4
gpt4 key购买 nike

我正在创建一个监控应用程序。我想从所有服务器读取查询并将其存储在一个 sqlite 文件中。需要时显示它。我正在使用 C++ 编写此应用程序——从多个服务器获取数据并将其存储到单个 sqlite 数据库中。我正在使用单独的线程从多个服务器获取数据。如果我直接获取数据并将其写入 sqlite,那么它可能会锁定 sqlite 文件,因为 100 多个线程将同时写入。从多个线程将数据写入 sqlite 的最佳方法是什么?

最佳答案

如果您无法控制同时写入操作的数量,我认为没有更好的方法可以使用 sqlite 执行此操作。

SQLite 不是为您的用例设计的。考虑切换到客户端/服务器 RDBMS。这也是 SQLite 的建议:

https://www.sqlite.org/whentouse.html

Situations Where A Client/Server RDBMS May Work Better

Client/Server Applications

If there are many client programs sending SQL to the same database over a network, then use a client/server database engine instead of SQLite. SQLite will work over a network filesystem, but because of the latency associated with most network filesystems, performance will not be great. Also, file locking logic is buggy in many network filesystem implementations (on both Unix and Windows). If file locking does not work correctly, two or more clients might try to modify the same part of the same database at the same time, resulting in corruption. Because this problem results from bugs in the underlying filesystem implementation, there is nothing SQLite can do to prevent it.

A good rule of thumb is to avoid using SQLite in situations where the same database will be accessed directly (without an intervening application server) and simultaneously from many computers over a network.

(...)

High Concurrency

SQLite supports an unlimited number of simultaneous readers, but it will only allow one writer at any instant in time. For many situations, this is not a problem. Writer queue up. Each application does its database work quickly and moves on, and no lock lasts for more than a few dozen milliseconds. But there are some applications that require more concurrency, and those applications may need to seek a different solution.

关于c++ - 从多个线程将数据写入 sqlite 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39785185/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com