鍍金池/ 問答/數(shù)據(jù)庫/ mongo查詢count很慢怎么辦?

mongo查詢count很慢怎么辦?

我使用mongodb3.4,目前有個查詢需要做分頁,分頁需要返回總的數(shù)量和頁數(shù)。
但問題就出在這里,分頁查詢還算快,但計數(shù)就特別慢

計數(shù)語句如下:

db.message.find(
    {
        "field_1":"ajian",
        "field_2":{ "$exists": true },
        "field_3":{ "$exists": true },
        "field_4":{ "$exists": true },
        "field_5":{"$regex":value1},
        "field_6":{"$regex":value2}
    }
).count()

返回結果要2秒5的時間!
我explain的info結果如下:

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "engine.message",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "$and" : [ 
                {
                    "field_1" : {
                        "$eq" : "ajian"
                    }
                }, 
                {
                    "field_2" : {
                        "$regex" : ""
                    }
                }, 
                {
                    "field_3" : {
                        "$regex" : "未處理"
                    }
                }, 
                {
                    "field_4" : {
                        "$exists" : true
                    }
                }, 
                {
                    "field_5" : {
                        "$exists" : true
                    }
                }, 
                {
                    "field_6" : {
                        "$exists" : true
                    }
                }
            ]
        },
        "winningPlan" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "$and" : [ 
                    {
                        "field_1" : {
                            "$eq" : "ajian"
                        }
                    }, 
                    {
                        "field_2" : {
                            "$regex" : ""
                        }
                    }, 
                    {
                        "field_3" : {
                            "$regex" : "未處理"
                        }
                    }, 
                    {
                        "field_4" : {
                            "$exists" : true
                        }
                    }, 
                    {
                        "field_5" : {
                            "$exists" : true
                        }
                    }, 
                    {
                        "field_6" : {
                            "$exists" : true
                        }
                    }
                ]
            },
            "direction" : "forward"
        },
        "rejectedPlans" : []
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1537253,
        "executionTimeMillis" : 2536,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 1610191,
        "executionStages" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "$and" : [ 
                    {
                        "field_1" : {
                            "$eq" : "ajian"
                        }
                    }, 
                    {
                        "field_2" : {
                            "$regex" : ""
                        }
                    }, 
                    {
                        "field_3" : {
                            "$regex" : "未處理"
                        }
                    }, 
                    {
                        "field_4" : {
                            "$exists" : true
                        }
                    }, 
                    {
                        "field_5" : {
                            "$exists" : true
                        }
                    }, 
                    {
                        "field_6" : {
                            "$exists" : true
                        }
                    }
                ]
            },
            "nReturned" : 1537253,
            "executionTimeMillisEstimate" : 2417,
            "works" : 1610193,
            "advanced" : 1537253,
            "needTime" : 72939,
            "needYield" : 0,
            "saveState" : 12588,
            "restoreState" : 12588,
            "isEOF" : 1,
            "invalidates" : 0,
            "direction" : "forward",
            "docsExamined" : 1610191
        },
        "allPlansExecution" : []
    },
    "serverInfo" : {
        "host" : "secret",
        "port" : 27017,
        "version" : "3.4.10",
        "gitVersion" : "2234234232325323343"
    },
    "ok" : 1.0
}

為什么mongo的count計數(shù)這么慢呢?大神們在做分頁的時候都怎么解決這個問題的?我這個問題又該怎么解決呢?

回答
編輯回答
陌上花

簡單地說,不能命中任何索引的查詢需要進行全表掃描,這個過程當然是很慢的。你需要添加合適的索引。

2017年4月6日 18:58