SQLite 是否在读取时锁定数据库文件? [英] Does SQLite lock the database file on reads?

查看:48
本文介绍了SQLite 是否在读取时锁定数据库文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 SQLite 作为存储引擎,并且很想知道 SQLite 是否在读取时锁定数据库文件.

I'm investigating SQLite as a storage engine, and am curious to know whether SQLite locks the database file on reads.

我担心读取性能,因为我计划的项目写入很少,但读取很多.如果数据库确实锁定,是否可以采取措施(例如内存缓存)来缓解这种情况?

I am concerned about read performance as my planned project will have few writes, but many reads. If the database does lock, are there measures that can be taken (such as memory caching) to mitigate this?

推荐答案

来自其维基百科页面:

多个计算机进程或线程可以毫无问题地访问同一个数据库.可以同时满足多个读访问.

Several computer processes or threads may access the same database without problems. Several read accesses can be satisfied in parallel.

更准确地说,来自其FAQ:

多个进程可以同时打开同一个数据库.多个进程可以同时执行 SELECT.但是,任何时候只有一个进程可以对数据库进行更改.

Multiple processes can have the same database open at the same time. Multiple processes can be doing a SELECT at the same time. But only one process can be making changes to the database at any moment in time, however.

然而,对数据库的单次写入,确实会在短时间内锁定数据库,因此根本无法访问它(甚至无法读取).详细信息可在 SQLite 版本 3 中的文件锁定和并发 中找到.除非有人想立即写入数据库,否则读取数据库基本上没有问题.在这种情况下,DB 被独占锁定执行该事务所需的时间,然后释放锁定.但是,关于在 PENDING 或 EXCLUSIVE 锁定期间对数据空间的读取操作究竟做了什么的细节很少.我的猜测是他们要么返回 SQLITE_BUSY 要么阻塞直到他们可以读取.在第一种情况下,再试一次应该不会太难,特别是如果您预计很少写入.

A single write to the database however, does lock the database for a short time so nothing can access it at all (not even reading). Details may be found in File Locking And Concurrency In SQLite Version 3. Basically reading the database is no problem unless someone wants to write to the database immediately. In that case the DB is locked exclusively for the time it takes to execute that transaction and the lock is released afterwards. However, details are scarce on what exactly does with read operations on the datapase in the time of a PENDING or EXCLUSIVE lock. My guess is that they either return SQLITE_BUSY or block until they can read. In the first case, it shouldn't be too hard to just try again, especially if you are expecting few writes.

这篇关于SQLite 是否在读取时锁定数据库文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆