我有一个mongo实例,它为同时读取/更新集合的其他服务提供字典知识。我注意到(使用slowlog)一些查询需要很长时间才能完成他们应该做的事情 - 因为他们正在锁定数据库和其他查询队列。集合上有多个索引和复合索引,因此它只能访问一个文档。有aprox。该系列中有100万条记录。
我很困惑为什么mongo需要锁定全局,数据库和集合?修改仅发生在一个数据库的一个集合中的一个文档上。
由于我使用的是WiredTiger
,我认为锁定只会在文档本身上?如何优化性能?
我不在乎其他并发读取是否会让人感到厌烦。版本或并发写入尝试创建相同的文档两次(upsert)因为唯一索引只会抛出错误并让我的服务处理它。我还可以考虑其他优化吗?
我得到的慢速消息(格式化可读性)
command myDB.words
command: findAndModify {
findandmodify: "words",
query: { lang: "en", name: "cat" },
new: true,
remove: false,
upsert: true,
fields: {},
update: {
$setOnInsert: { __v: 0 },
$inc: { count: 2 },
$addToSet: {
type: { $each: [ 0 ] },
occured: ObjectId('....')
},
$set: { lang: "en", name: "cat" }
},
writeConcern: { w: 1 }
}
planSummary:
IXSCAN { name: 1, lang: 1 }
update: { .... }
keysExamined:1
docsExamined:1
nMatched:1
nModified:1
keysInserted:1
numYields:0
reslen:2649211
locks:{
Global: { acquireCount: { r: 1, w: 1 } },
Database: { acquireCount: { w: 1 } },
Collection: { acquireCount: { w: 1 } }
}
protocol:op_query
754ms
存在以下索引;
lang
和name
如果重要,我使用的是mongoose
,并且连接到mongdb的服务正在node8.4上运行。 MongoDB 3.4.7