如何检查我的聚合查询是好还是坏?

时间:2017-02-16 08:20:18

标签: node.js mongodb express mongoose ubuntu-14.04

如何检查我的汇总查询是好还是坏?我已经使用了“解释”,但这并不具体。

这是我的汇总“解释”的输出。

{
    "waitedMS" : NumberLong(0),
    "stages" : [
            {
                    "$cursor" : {
                            "query" : {
                                    "keys.license_id" : ObjectId("580eeb7fb79bec95648775a2"),
                                    "deleted.status" : {
                                            "$ne" : 1
                                    }
                            },
                            "queryPlanner" : {
                                    "plannerVersion" : 1,
                                    "namespace" : "serpentsmsapp.conversations",
                                    "indexFilterSet" : false,
                                    "parsedQuery" : {
                                            "$and" : [
                                                    {
                                                            "keys.license_id" : {
                                                                    "$eq" : ObjectId("580eeb7fb79bec95648775a2")
                                                            }
                                                    },
                                                    {
                                                            "$not" : {
                                                                    "deleted.status" : {
                                                                            "$eq" : 1
                                                                    }
                                                            }
                                                    }
                                            ]
                                    },
                                    "winningPlan" : {
                                            "stage" : "COLLSCAN",
                                            "filter" : {
                                                    "$and" : [
                                                            {
                                                                    "keys.license_id" : {
                                                                            "$eq" : ObjectId("580eeb7fb79bec95648775a2")
                                                                    }
                                                            },
                                                            {
                                                                    "$not" : {
                                                                            "deleted.status" : {
                                                                                    "$eq" : 1
                                                                            }
                                                                    }
                                                            }
                                                    ]
                                            },
                                            "direction" : "forward"
                                    },
                                    "rejectedPlans" : [ ]
                            }
                    }
            },
            {
                    "$sort" : {
                            "sortKey" : {
                                    "_id" : 1,
                                    "keys.license_id" : 1,
                                    "status.deleted" : 1
                            }
                    }
            },
            {
                    "$lookup" : {
                            "from" : "conversation_messages",
                            "as" : "cmf",
                            "localField" : "_id",
                            "foreignField" : "keys.conv_id",
                            "unwinding" : {
                                    "preserveNullAndEmptyArrays" : false
                            }
                    }
            },
            {
                    "$match" : {
                            "cmf.deleted.status" : 0
                    }
            },
            {
                    "$sort" : {
                            "sortKey" : {
                                    "cmf.deleted.status" : -1
                            }
                    }
            },
            {
                    "$group" : {
                            "_id" : {
                                    "id" : "$_id",
                                    "number" : "$number",
                                    "mode" : "$mode",
                                    "keys" : "$keys",
                                    "ports" : "$ports",
                                    "user_assign" : "$user_assign",
                                    "spam" : "$spam"
                            },
                            "cm_field" : {
                                    "$last" : {
                                            "id" : "$cmf._id",
                                            "message" : "$cmf.message",
                                            "ports" : "$cmf.ports",
                                            "mode" : "$cmf.mode",
                                            "keys" : "$cmf.keys",
                                            "status" : "$cmf.status",
                                            "date" : "$cmf.updated"
                                    }
                            },
                            "counts" : {
                                    "$sum" : "$cmf.status"
                            },
                            "sms_mode" : {
                                    "$addToSet" : "$cmf.mode"
                            }
                    }
            },
            {
                    "$sort" : {
                            "sortKey" : {
                                    "cm_field.date" : -1
                            },
                            "limit" : NumberLong(5000)
                    }
            },
            {
                    "$group" : {
                            "_id" : "$_id",
                            "cm_field" : {
                                    "$last" : "$cm_field"
                            },
                            "counts" : {
                                    "$first" : "$counts"
                            },
                            "sms_mode" : {
                                    "$first" : "$sms_mode"
                            }
                    }
            }
    ],
    "ok" : 1

}

在执行查询之前,如何查看扫描的文档数量?

1 个答案:

答案 0 :(得分:1)

为了解决您的问题,我建议您采取以下计划

  1. 了解Explain如何运作。
  2. “好的或坏的”方法不是一种工程方法,它总是取决于很多因素(要求,硬件等......)。如果查询的性能足够,您应该在回答之前定义这些要求是什么。
  3. 针对您的具体说明日志。 "COLSCAN" - 表示它已使用集合中的所有文档来汇总结果,这通常是一个警告标志,您应该考虑使用正确的indexes

相关问题