MongoDB,即使查询字段形成分区,也会减慢查询速度吗?

标签 mongodb performance mongoose mongodb-query

假设我的用户集合中只有男性和女性。如下:

User.find({ gender: { $in: ['male','female'] }})

比这个慢:

User.find()

我觉得是这样,但我真的不知道 MongoDB 内部是如何工作的。两个请求都会返回整个集合。我正在构建一个过滤器功能,并且希望通过考虑以某种方式过滤每个调用来简化我的 api 代码。

最佳答案

这是一个好问题,因为它涉及基本的查询规划功能。 比较解释结果,我们可以看到使用 IN 通过指定的查询参数调用集合扫描 - 在不带参数的查询时,这比基本文档转储更昂贵。

db.User.find({ gender: { $in: ['male','female'] }}).explain("executionStats")

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "test.User",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "gender" : {
                "$in" : [ 
                    "female", 
                    "male"
                ]
            }
        },
        "winningPlan" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "gender" : {
                    "$in" : [ 
                        "female", 
                        "male"
                    ]
                }
            },
            "direction" : "forward"
        },
        "rejectedPlans" : []
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 24,
        "executionTimeMillis" : 0,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 24,
        "executionStages" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "gender" : {
                    "$in" : [ 
                        "female", 
                        "male"
                    ]
                }
            },
            "nReturned" : 24,
            "executionTimeMillisEstimate" : 0,
            "works" : 26,
            "advanced" : 24,
            "needTime" : 1,
            "needYield" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 1,
            "invalidates" : 0,
            "direction" : "forward",
            "docsExamined" : 24
        }
    },
    "serverInfo" : {
        "host" : "greg",
        "port" : 27017,
        "version" : "3.2.3",
        "gitVersion" : "b326ba837cf6f49d65c2f85e1b70f6f31ece7937"
    },
    "ok" : 1
}

db.User.find().explain("executionStats")

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "test.User",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "$and" : []
        },
        "winningPlan" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "$and" : []
            },
            "direction" : "forward"
        },
        "rejectedPlans" : []
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 24,
        "executionTimeMillis" : 0,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 24,
        "executionStages" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "$and" : []
            },
            "nReturned" : 24,
            "executionTimeMillisEstimate" : 0,
            "works" : 26,
            "advanced" : 24,
            "needTime" : 1,
            "needYield" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 1,
            "invalidates" : 0,
            "direction" : "forward",
            "docsExamined" : 24
        }
    },
    "serverInfo" : {
        "host" : "greg",
        "port" : 27017,
        "version" : "3.2.3",
        "gitVersion" : "b326ba837cf6f49d65c2f85e1b70f6f31ece7937"
    },
    "ok" : 1
}

关于MongoDB,即使查询字段形成分区,也会减慢查询速度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36751294/

相关文章:

node.js - 在单个响应中返回 Mongoose 错误列表

javascript - 在聚合中的嵌入文档上使用 $match

mysql - 优化 SELECT COUNT(DISTINCT(col)) var, col2 var2 FROM table WHERE col< >'X' and col2 between 'Y' and 'Z' GROUP BY var2 ORDER BY var DESC;为了速度?

ff4j 和 togglz 的性能比较

node.js - 带有可能为空参数的 find() 语句

node.js - 在 db.collection.findOne( { parm : value } 中传递变量

mongodb - 如何维护 mongoDB 中数组元素的最高计数?

Node.js - 类型错误 : Cannot read property 'readPreference' of undefined

sql-server - 我可以停止调用 sp_reset_connection 以提高性能吗?

javascript - 嵌套回调异步运行