我们有一个包含大约 1M 文档的 mongo 数据库,我们想使用处理过的字段轮询此数据库以查找我们以前从未见过的文档。为此,我们设置了一个名为 _processed
的新字段。
要查询需要处理的文档,我们查询没有这个processed字段的文档:
db.stocktwits.find({ "_processed" : { "$exists" : false } })
但是,此查询每次需要大约 30 秒才能完成,相当慢。 _processed 字段上有一个索引 (asc):
db.stocktwits.ensureIndex({ "_processed" : -1 },{ "name" : "idx_processed" });
添加此索引不会改变查询性能。集合中还有一些其他索引(即 ID idx 和每个文档中几个字段的唯一索引)。
_processed 字段很长,也许应该将其更改为 bool 值以使处理速度更快?
我们已经尝试使用 $where 查询(即 $where : this._processed==null
)来做与 $exists : false
相同的事情和性能大致相同(慢几秒是有道理的)...
关于什么会导致性能缓慢(或者这是正常现象)的任何想法?有没有人对如何提高查询速度有任何建议?
干杯!
最佳答案
升级到 2.0 将为您做到这一点:
来自 MongoDB.org:
Before v2.0, $exists is not able to use an index. Indexes on other fields are still used.
关于MongoDB 查询具有不存在的字段和索引的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7502875/