mongodb - MongoDB find and iterate vs count
问题描述
I have a peculiar problem with Mongo.
We have a collection of 800k documents with the following structure.
{
"_id" : ObjectId("5bd844199114bab3b2c19fab"),
"u" : 0,
"c" : 0,
"iden" : "343754856",
"name" : "alan",
"email" : "mkasd@abc.com",
"mobile" : "987654321093456",
"expires" : ISODate("2018-11-29T11:44:25.453Z"),
"created" : ISODate("2018-10-30T11:44:25.453Z")
}
We have indexed iden
and name
on which we generally query.
We tried two types of queries.
db.Collection.find({"iden": "343754856", "name": "alan", "created":
{"$gt": ....}).count()where "created" is an unindexed field.
db.Collection.find({"iden": "343754856", "name": "alan"})
and iterate over all records to filter based on
created
.
However, MongoDB seems to be taking enormous amount of time in executing the second query while it was supposed to be an optimization over 1.
Any leads on what is going wrong here? We are using Go library.
解决方案
How could the second version be an optimization over the first?
Your first query retrieves a single number from the MongoDB server: the overall count of the query result. While your second version fetches all matching documents, and you do the counting at the "client" side.
Believe me that the MongoDB can count internally the result documents just as fast as you could in your Go client. Making the MongoDB server send the results, fetching them and unmarshaling them at the client takes orders of magnitude more time (depending on lot of factors).
Please note that if you have a composite index containing "iden"
and "name"
, even if you add more filters (like "created"
in your example), the index may still be used, but the MongoDB have to iterate over the partial results to apply the rest of the query. To find out if the index is used, please execute the following command:
db.Collection.find(
{"iden": "343754856", "name": "alan", "created": {"$gt": ....}
).explain()
推荐阅读
- python - Python 3 doctest:doctest 和真实评估之间的不同行为
- javascript - 从具有相同名称的多个下拉列表中获取值
- r - 获取 R 中最新可用数据的年份
- javascript - 授权我的频道时,JSON 数据返回空字符串
- javascript - react-native: setState() 不使用 react-native-geolocation-service 更新 MapView initialRegion
- python - 对象检测模型的周围环境在 Tensorflow 中是否重要?
- c++ - 按升序将特征匹配写入文件
- python - 如何使用 py 字典来获取与表中 id 相关的名称?
- vue.js - 页面查询在 Gridsome 的组件内部不起作用
- c# - 如何在 .NET Core 应用程序中使用 EntityFrameworkCore 模拟 DbContext