如果是使用了文字索引,就不能使用複合索引?
因為我用了下面的查詢語句但是只做了文字索引:
db.tests.find({$text:{$search:'zhangshan'},'age':5,'city':'beijing'}
透過explain查看,他做了text的索引。然後再從這個索引中去過濾出其他條件的!
那我該怎麼做才能全部走索引呢?
我想到把所有欄位建立一個text索引。因為資料量大,所以沒有嘗試,想問這個可行嗎?
謝謝
補充explain()
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 50424,
"executionTimeMillis" : 510228,
"totalKeysExamined" : 83546,
"totalDocsExamined" : 167092,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"age" : {
"$eq" : "5"
}
},
{
"city" : {
"$eq" : "beijing"
}
}
]
},
"nReturned" : 50424,
"executionTimeMillisEstimate" : 509543,
"works" : 167095,
"advanced" : 50424,
"needTime" : 116670,
"needYield" : 0,
"saveState" : 24418,
"restoreState" : 24418,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 83546,
"alreadyHasObj" : 83546,
"inputStage" : {
"stage" : "TEXT",
"nReturned" : 83546,
"executionTimeMillisEstimate" : 509362,
"works" : 167095,
"advanced" : 83546,
"needTime" : 83548,
"needYield" : 0,
"saveState" : 24418,
"restoreState" : 24418,
"isEOF" : 1,
"invalidates" : 0,
"indexPrefix" : {
},
"indexName" : "banner_text",
"parsedTextQuery" : {
"terms" : [
"ii"
],
"negatedTerms" : [ ],
"phrases" : [ ],
"negatedPhrases" : [ ]
},
"textIndexVersion" : 3,
"inputStage" : {
"stage" : "TEXT_MATCH",
"nReturned" : 83546,
"executionTimeMillisEstimate" : 509320,
"works" : 167095,
"advanced" : 83546,
"needTime" : 83548,
"needYield" : 0,
"saveState" : 24418,
"restoreState" : 24418,
"isEOF" : 1,
"invalidates" : 0,
"docsRejected" : 0,
"inputStage" : {
"stage" : "TEXT_OR",
"nReturned" : 83546,
"executionTimeMillisEstimate" : 509310,
"works" : 167095,
"advanced" : 83546,
"needTime" : 83548,
"needYield" : 0,
"saveState" : 24418,
"restoreState" : 24418,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 83546,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 83546,
"executionTimeMillisEstimate" : 1642,
"works" : 83547,
"advanced" : 83546,
"needTime" : 0,
"needYield" : 0,
"saveState" : 24418,
"restoreState" : 24418,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"_fts" : "text",
"_ftsx" : 1
},
"indexName" : "name_text",
"isMultiKey" : true,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "backward",
"indexBounds" : {
},
"keysExamined" : 83546,
"seeks" : 1,
"dupsTested" : 83546,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
試著回答:
1、首先想看看你的複合索引是具體怎麼建立的?
2、執行的結果是按照所選的執行計劃執行的;如果效能不滿意,可以使用hint來指定您所建立的複合索引。
Love MongoDB! Have fun!
2017MongoDB中文社群北京用戶群組大會在即,2017年6月3日 13:00-18:00
眾多精彩分享!值得參與!