最近遇到的一個MongoDB索引順序的問題

最近遇到個mongo慢查問題,查詢這樣子:bash

db.tb1.find({status:'normal', lastReviewTime:{$gte:1583038740,$lte:1585285140}}).sort({createdTime:-1}).limit(30)


執行計劃以下:
ide

> db.tb1.find({status:'normal', lastReviewTime:{$gte:1583038740,$lte:1585285140}}).sort({createdTime:-1}).limit(30).explain()
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "db1.tb1",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [
                                {
                                        "status" : {
                                                "$eq" : "normal"
                                        }
                                },
                                {
                                        "lastReviewTime" : {
                                                "$lte" : 1585285140
                                        }
                                },
                                {
                                        "lastReviewTime" : {
                                                "$gte" : 1583038740
                                        }
                                }
                        ]
                },
                "winningPlan" : {
                        "stage" : "SORT",
                        "sortPattern" : {
                                "createdTime" : -1
                        },
                        "limitAmount" : 30,
                        "inputStage" : {
                                "stage" : "SORT_KEY_GENERATOR",
                                "inputStage" : {
                                        "stage" : "FETCH",
                                        "filter" : {
                                                "$and" : [
                                                        {
                                                                "lastReviewTime" : {
                                                                        "$lte" : 1585285140
                                                                }
                                                        },
                                                        {
                                                                "lastReviewTime" : {
                                                                        "$gte" : 1583038740
                                                                }
                                                        }
                                                ]
                                        },
                                        "inputStage" : {
                                                "stage" : "IXSCAN",
                                                "keyPattern" : {
                                                        "status" : 1,
                                                        "type" : 1,
                                                        "executionTime" : 1
                                                },
                                                "indexName" : "idx_stats_typ_execTime",   # 走的這個索引
                                                "isMultiKey" : false,
                                                "multiKeyPaths" : {
                                                        "status" : [ ],
                                                        "type" : [ ],
                                                        "executionTime" : [ ]
                                                },
                                                "isUnique" : false,
                                                "isSparse" : false,
                                                "isPartial" : false,
                                                "indexVersion" : 2,
                                                "direction" : "forward",
                                                "indexBounds" : {
                                                        "status" : [
                                                                "[\"normal\", \"normal\"]"
                                                        ],
                                                        "type" : [
                                                                "[MinKey, MaxKey]"
                                                        ],
                                                        "executionTime" : [
                                                                "[MinKey, MaxKey]"
                                                        ]
                                                }
                                        }
                                }
                        }
                },
                "rejectedPlans" : [
                        {
                                "stage" : "SORT",
                                "sortPattern" : {
                                        "createdTime" : -1
                                },
                                "limitAmount" : 30,
                                "inputStage" : {
                                        "stage" : "SORT_KEY_GENERATOR",
                                        "inputStage" : {
                                                "stage" : "FETCH",
                                                "inputStage" : {
                                                        "stage" : "IXSCAN",
                                                        "keyPattern" : {
                                                                "lastReviewTime" : 1,
                                                                "status" : 1,
                                                                "createdTime" : -1
                                                        },
                                                        "indexName" : "lastReviewTime_1_status_1_createdTime_-1",
                                                        "isMultiKey" : false,
                                                        "multiKeyPaths" : {
                                                                "lastReviewTime" : [ ],
                                                                "status" : [ ],
                                                                "createdTime" : [ ]
                                                        },
                                                        "isUnique" : false,
                                                        "isSparse" : false,
                                                        "isPartial" : false,
                                                        "indexVersion" : 2,
                                                        "direction" : "forward",
                                                        "indexBounds" : {
                                                                "lastReviewTime" : [
                                                                        "[1583038740.0, 1585285140.0]"
                                                                ],
                                                                "status" : [
                                                                        "[\"normal\", \"normal\"]"
                                                                ],
                                                                "createdTime" : [
                                                                        "[MaxKey, MinKey]"
                                                                ]
                                                        }
                                                }
                                        }
                                }
                        },
                        {
                                "stage" : "SORT",
                                "sortPattern" : {
                                        "createdTime" : -1
                                },
                                "limitAmount" : 30,
                                "inputStage" : {
                                        "stage" : "SORT_KEY_GENERATOR",
                                        "inputStage" : {
                                                "stage" : "FETCH",
                                                "inputStage" : {
                                                        "stage" : "IXSCAN",
                                                        "keyPattern" : {
                                                                "lastReviewTime" : -1,
                                                                "status" : 1
                                                        },
                                                        "indexName" : "lastReviewTime_-1_status_1",
                                                        "isMultiKey" : false,
                                                        "multiKeyPaths" : {
                                                                "lastReviewTime" : [ ],
                                                                "status" : [ ]
                                                        },
                                                        "isUnique" : false,
                                                        "isSparse" : false,
                                                        "isPartial" : false,
                                                        "indexVersion" : 2,
                                                        "direction" : "forward",
                                                        "indexBounds" : {
                                                                "lastReviewTime" : [
                                                                        "[1585285140.0, 1583038740.0]"
                                                                ],
                                                                "status" : [
                                                                        "[\"normal\", \"normal\"]"
                                                                ]
                                                        }
                                                }
                                        }
                                }
                        }
                ]
        },
        "ok" : 1
}


這種狀況下,咱們的索引順序須要注意下,這樣寫:spa

{精確匹配字段,排序字段,範圍查詢字段} 這樣的索引排序會更爲高效orm

db.tb1.createIndex({status:1,createdTime:-1,lastReviewTime:1},{background:true})


加完索引後,能夠發現查詢速度有質的飛越了。排序

相關文章
相關標籤/搜索