mongodb 锁

mongodb不同于其他关系型数据库,mongodb锁的粒度很大,2.2之前是整个server级别的锁,也叫全局锁,但是支持并发读,也就是说读可以同时读,但是只要有写操作,那么就加上锁,其他人既不能读也不能写,从2.2开始,这个锁的粒度缩小到了库级别,如果你有3个库,一个库有写锁,那么其他2个库还是继续可以读写操作,希望以后能像关系数据库一样到表级别。

2.2锁引发的条件:

OperationLock Type
Issue a queryRead lock
Get more data from acursor Read lock
Insert dataWrite lock
Remove dataWrite lock
Update dataWrite lock
Map-reduceRead lock and write lock, unless operations are specified as non-atomic. Portions of map-reduce jobs can run concurrently.
Create an indexBuilding an index in the foreground, which is the default, locks the database for extended periods of time.
db.eval()Write lock or no lock. If this is used without thenolockoption, all JavaScript is blocked.
aggregate()Read lock

The following administrative operations require an exclusive (i.e. write) lock to a the database for extended periods:

db.collection.ensureIndex(), when issuedwithoutsettingbackgroundtotrue,
reIndex,
compact,
db.repairDatabase(),
db.createCollection(), when creating a very large (i.e. many gigabytes) capped collection,
db.collection.validate(), and
db.copyDatabase(). This operation may lock all databases.

Thedb.collection.group()operation takes a read lock and does not allow any other threads to execute JavaScript while it is running.

The following administrative commands lock the database but only hold the lock for a very short time:

db.collection.dropIndex(),
db.collection.getLastError(),
db.isMaster(),
rs.status()(i.e.replSetGetStatus,)
db.serverStatus(),
db.auth(), and
db.addUser().
Does a read or write operation ever yield the lock?
New in version 2.0.

A read and write operations will yield their locks if themongodreceives apage faultorfetches data that is unlikely to be in memory. Yielding allows other operations that only need to access documents that are already in memory to complete whilemongodloads documents into memory.

Additionally, write operations that affect multiple documents (i.e.update()<db.collection.update()with themultiparameter,) will yield periodically to allow read operations during these log write operations. Similarly, long running read locks will yield periodically to ensure that write operations have the opportunity to complete.

Changed in version 2.2:The use of yielding expanded greatly in MongoDB 2.2. Including the “yield for page fault.” MongoDB tracks the contents of memory and predicts whether data is available before performing a read. If MongoDB predicts that the data is not in memory a read operation yields its lock while MongoDB loads the data to memory. Once data is available in memory, the read will reacquire the lock to completes the operation.


至于page fault,以及mmap请参阅http://docs.mongodb.org/manual/faq/storage/#faq-storage-page-faults

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值