Performance MongoDB,嵌入式文档索引
我有一个mongoDB集合,包含超过100万个文档,其结构如下:Performance MongoDB,嵌入式文档索引,performance,mongodb,Performance,Mongodb,我有一个mongoDB集合,包含超过100万个文档,其结构如下: { _id:"..", title:"…", year:…, authors : [{_id:"...", mail: "…"}, {_id: …, Name : "…"}, …] } 我运行了查询 db.papers.find({"authors._id": /Stonebraker$/}, {_id:1}) 有或没有作者索引。\u id (db.papers.createInde
{ _id:"..",
title:"…",
year:…,
authors :
[{_id:"...", mail: "…"},
{_id: …, Name : "…"}, …]
}
我运行了查询
db.papers.find({"authors._id": /Stonebraker$/}, {_id:1})
有或没有作者索引。\u id
(db.papers.createIndex( { "authors._id": 1 } )).
查询应该返回大约100个文档
令人惊讶的是,查询在没有索引的情况下执行得更快
在前面的一个线程()中,对类似问题给出的答案是,匹配的结果集几乎由整个集合组成。但我的情况并非如此(100万份文件中有100份)
不带索引:
query: { authors._id: /Stonebraker$/ }
planSummary: COLLSCAN
cursorid:43101148517
ntoreturn:0
ntoskip:0
nscanned:0
nscannedObjects:1109672
keyUpdates:0
writeConflicts:0
numYields:14218
nreturned:101
reslen:3905
locks:{}
time: 12686ms
带有索引:
query: { authors._id: /Stonebraker$/ }
planSummary: IXSCAN { authors._id: 1.0 }
cursorid:47336451823
ntoreturn:0
ntoskip:0
nscanned:2576899
nscannedObjects:1394747
keyUpdates:0
writeConflicts:0
numYields:213058
nreturned:101
reslen:3959
locks:{}
time: 1743651ms
我错过什么了吗 您似乎遗漏了文档,其中解释了适用的索引用法。基本上,“未编排”表达式(未绑定到字符串开头)不能使用idex有效地选择范围。因此,从字符串“末尾”查看的表达式仍然需要检查每个条目,无论是否有索引。唯一的区别是加载要扫描的索引,而不是集合的整个文档。顺便说一句,不是总是为集合的id(\u id)创建索引吗?