相同的mongodb查询提供了不同的解释计划

时间:2018-05-29 09:59:08

标签: mongodb performance query-planner

我有两个mongo查询,查询中唯一的变化是merchantId字段,但两个查询都给了我不同的获胜计划。

第一次查询

db.transactions.find({"created":{"$gte":1527465600000,"$lte":1527551999000},"merchantId":940,"additionalInformation.REQUESTOR":{"$ne":"MOTO"},"$or":[{"paymentMode":{"$ne":"UPI"}},{"bankCode":{"$ne":"GTEZ"}}]}).sort({ _id: -1 }).limit(200).explain()

以上查询的输出

{
    "queryPlanner" : {
            "plannerVersion" : 1,
            "indexFilterSet" : false,
            "parsedQuery" : {
                    "$and" : [
                            {
                                    "$or" : [
                                            {
                                                    "$not" : {
                                                            "bankCode" : {
                                                                    "$eq" : "GTEZ"
                                                            }
                                                    }
                                            },
                                            {
                                                    "$not" : {
                                                            "paymentMode" : {
                                                                    "$eq" : "UPI"
                                                            }
                                                    }
                                            }
                                    ]
                            },
                            {
                                    "merchantId" : {
                                            "$eq" : 940
                                    }
                            },
                            {
                                    "created" : {
                                            "$lte" : 1527551999000
                                    }
                            },
                            {
                                    "created" : {
                                            "$gte" : 1527465600000
                                    }
                            },
                            {
                                    "$not" : {
                                            "additionalInformation.REQUESTOR" : {
                                                    "$eq" : "MOTO"
                                            }
                                    }
                            }
                    ]
            },
            "winningPlan" : {
                    "stage" : "SORT",
                    "sortPattern" : {
                            "_id" : -1
                    },
                    "limitAmount" : 200,
                    "inputStage" : {
                            "stage" : "SORT_KEY_GENERATOR",
                            "inputStage" : {
                                    "stage" : "FETCH",
                                    "filter" : {
                                            "$and" : [
                                                    {
                                                            "$or" : [
                                                                    {
                                                                            "$not" : {
                                                                                    "bankCode" : {
                                                                                            "$eq" : "GTEZ"
                                                                                    }
                                                                            }
                                                                    },
                                                                    {
                                                                            "$not" : {
                                                                                    "paymentMode" : {
                                                                                            "$eq" : "UPI"
                                                                                    }
                                                                            }
                                                                    }
                                                            ]
                                                    },
                                                    {
                                                            "$not" : {
                                                                    "additionalInformation.REQUESTOR" : {
                                                                            "$eq" : "MOTO"
                                                                    }
                                                            }
                                                    }
                                            ]
                                    },
                                    "inputStage" : {
                                            "stage" : "IXSCAN",
                                            "keyPattern" : {
                                                    "created" : 1,
                                                    "merchantId" : 1
                                            },
                                            "indexName" : "created_1_merchantId_1",
                                            "isMultiKey" : false,
                                            "isUnique" : false,
                                            "isSparse" : false,
                                            "isPartial" : false,
                                            "indexVersion" : 1,
                                            "direction" : "forward",
                                            "indexBounds" : {
                                                    "created" : [
                                                            "[1527465600000.0, 1527551999000.0]"
                                                    ],
                                                    "merchantId" : [
                                                            "[940.0, 940.0]"
                                                    ]
                                            }
                                    }
                            }
                    }
            }

    },
    "serverInfo" : {

    },
    "ok" : 1

}

第二次查询

db.transactions.find({"created":{"$gte":1527465600000,"$lte":1527551999000},"merchantId":1429,"additionalInformation.REQUESTOR":{"$ne":"MOTO"},"$or":[{"paymentMode":{"$ne":"UPI"}},{"bankCode":{"$ne":"GTEZ"}}]}).sort({ _id: -1 }).limit(200).explain()

以上查询的输出

{
    "queryPlanner" : {
            "plannerVersion" : 1,
            "indexFilterSet" : false,
            "parsedQuery" : {
                    "$and" : [
                            {
                                    "$or" : [
                                            {
                                                    "$not" : {
                                                            "bankCode" : {
                                                                    "$eq" : "GTEZ"
                                                            }
                                                    }
                                            },
                                            {
                                                    "$not" : {
                                                            "paymentMode" : {
                                                                    "$eq" : "UPI"
                                                            }
                                                    }
                                            }
                                    ]
                            },
                            {
                                    "merchantId" : {
                                            "$eq" : 1429
                                    }
                            },
                            {
                                    "created" : {
                                            "$lte" : 1527551999000
                                    }
                            },
                            {
                                    "created" : {
                                            "$gte" : 1527465600000
                                    }
                            },
                            {
                                    "$not" : {
                                            "additionalInformation.REQUESTOR" : {
                                                    "$eq" : "MOTO"
                                            }
                                    }
                            }
                    ]
            },
            "winningPlan" : {
                    "stage" : "LIMIT",
                    "limitAmount" : 200,
                    "inputStage" : {
                            "stage" : "FETCH",
                            "filter" : {
                                    "$and" : [
                                            {
                                                    "$or" : [
                                                            {
                                                                    "$not" : {
                                                                            "bankCode" : {
                                                                                    "$eq" : "GTEZ"
                                                                            }
                                                                    }
                                                            },
                                                            {
                                                                    "$not" : {
                                                                            "paymentMode" : {
                                                                                    "$eq" : "UPI"
                                                                            }
                                                                    }
                                                            }
                                                    ]
                                            },
                                            {
                                                    "merchantId" : {
                                                            "$eq" : 1429
                                                    }
                                            },
                                            {
                                                    "created" : {
                                                            "$lte" : 1527551999000
                                                    }
                                            },
                                            {
                                                    "created" : {
                                                            "$gte" : 1527465600000
                                                    }
                                            },
                                            {
                                                    "$not" : {
                                                            "additionalInformation.REQUESTOR" : {
                                                                    "$eq" : "MOTO"
                                                            }
                                                    }
                                            }
                                    ]
                            },
                            "inputStage" : {
                                    "stage" : "IXSCAN",
                                    "keyPattern" : {
                                            "_id" : 1
                                    },
                                    "indexName" : "_id_",
                                    "isMultiKey" : false,
                                    "isUnique" : true,
                                    "isSparse" : false,
                                    "isPartial" : false,
                                    "indexVersion" : 1,
                                    "direction" : "backward",
                                    "indexBounds" : {
                                            "_id" : [
                                                    "[MaxKey, MinKey]"
                                            ]
                                    }
                            }
                    }
            }

    },
    "serverInfo" : {

    },
    "ok" : 1

}

正如你所看到的只有param的区别是merchantId,仍然解释给出了不同的获胜计划,IXSCAN也显示了不同的索引。在第一次查询中使用 created_1_merchantId_1 索引,在第二次查询中使用 id 索引。第一次查询需要40秒才能获得结果,而第二次查询则需要1秒。快速将受到高度赞赏。

0 个答案:

没有答案