我使用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ù)這么慢呢?大神們在做分頁的時候都怎么解決這個問題的?我這個問題又該怎么解決呢?
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學院和江蘇省首批服務外包人才培訓基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術與教育服務機構,發(fā)展為教育服務業(yè)的綜合性企業(yè)集團,成為集合面授教學培訓、網(wǎng)
達內教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構,是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
浪潮集團項目經理。精通Java與.NET 技術, 熟練的跨平臺面向對象開發(fā)經驗,技術功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經理職務負責iOS教學及管理工作。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經驗。曾經歷任德國Software AG 技術顧問,美國Dachieve 系統(tǒng)架構師,美國AngelEngineers Inc. 系統(tǒng)架構師。