簡體   English   中英

$或Mongodb查詢性能極差

[英]Mongodb query performance extremly bad with $or

我使用MongoDB 3.2.13,我有一個包含約50萬個文檔的集合C。 這些文檔包含一個字符串字段A,該字段只能有幾個不同的值。 如果我查詢集合以獲取特定值,例如

db.getCollection('C').count({'A':{'$eq': 'valueA'}}) 
db.getCollection('C').count({'A':{'$eq': 'valueB'}}) 

我在不到1秒的時間內得到了結果。 如果我使用$ or進行組合搜索

 db.getCollection('C').count({'$or':
       [
       {'A':{'$eq': 'valueA'}},
        {'A':{'$eq': 'valueB'}}
       ]
 }) 

查詢運行大約165秒 我有一個索引A。我無法解釋為什么$ or查詢這么慢? 這只是一個簡單的示例,可以通過僅添加前兩個查詢的結果來解決,但我們還有其他無法輕易拆分的查詢。

誰能解釋$ or查詢出了什么問題? 或給我提示如何加快速度?

db.getCollection('C')。find(query).explain()給出:

 {
     "queryPlanner" : {
         "plannerVersion" : 1,
         "namespace" : "db.C",
         "indexFilterSet" : false,
         "parsedQuery" : {
             "$or" : [ 
                 {
                     "A" : {
                         "$eq" : "valueA"
                     }
                 }, 
                 {
                     "A" : {
                         "$eq" : "valueB"
                     }
                 }
             ]
         },
         "winningPlan" : {
             "stage" : "SUBPLAN",
             "inputStage" : {
                 "stage" : "FETCH",
                 "inputStage" : {
                     "stage" : "IXSCAN",
                     "keyPattern" : {
                         "A" : 1
                     },
                     "indexName" : "A",
                     "isMultiKey" : false,
                     "isUnique" : false,
                     "isSparse" : true,
                     "isPartial" : false,
                     "indexVersion" : 1,
                     "direction" : "forward",
                     "indexBounds" : {
                         "A" : [ 
                             "[\"valueA\", \"valueA\"]", 
                             "[\"valueB\", \"valueB\"]"
                         ]
                     }
                 }
             }
         },
         "rejectedPlans" : []
     },
     "executionStats" : {
         "executionSuccess" : true,
         "nReturned" : 2513596,
         "executionTimeMillis" : 133764,
         "totalKeysExamined" : 2513597,
         "totalDocsExamined" : 2513596,
         "executionStages" : {
             "stage" : "SUBPLAN",
             "nReturned" : 2513596,
             "executionTimeMillisEstimate" : 131660,
             "works" : 2513597,
             "advanced" : 2513596,
             "needTime" : 0,
             "needYield" : 0,
             "saveState" : 20912,
             "restoreState" : 20912,
             "isEOF" : 1,
             "invalidates" : 0,
             "inputStage" : {
                 "stage" : "FETCH",
                 "nReturned" : 2513596,
                 "executionTimeMillisEstimate" : 131490,
                 "works" : 2513597,
                 "advanced" : 2513596,
                 "needTime" : 0,
                 "needYield" : 0,
                 "saveState" : 20912,
                 "restoreState" : 20912,
                 "isEOF" : 1,
                 "invalidates" : 0,
                 "docsExamined" : 2513596,
                 "alreadyHasObj" : 0,
                 "inputStage" : {
                     "stage" : "IXSCAN",
                     "nReturned" : 2513596,
                     "executionTimeMillisEstimate" : 4420,
                     "works" : 2513597,
                     "advanced" : 2513596,
                     "needTime" : 0,
                     "needYield" : 0,
                     "saveState" : 20912,
                     "restoreState" : 20912,
                     "isEOF" : 1,
                     "invalidates" : 0,
                     "keyPattern" : {
                         "A" : 1
                     },
                     "indexName" : "A_1",
                     "isMultiKey" : false,
                     "isUnique" : false,
                     "isSparse" : true,
                     "isPartial" : false,
                     "indexVersion" : 1,
                     "direction" : "forward",
                     "indexBounds" : {
                         "A" : [ 
                             "[\"valueA\", \"valueA\"]", 
                             "[\"valueB\", \"valueB\"]"
                         ]
                     },
                     "keysExamined" : 2513597,
                     "dupsTested" : 0,
                     "dupsDropped" : 0,
                     "seenInvalidated" : 0
                 }
             }
         },
         "allPlansExecution" : []
     },
     "serverInfo" : {
         "host" : "xxxx",
         "port" : 27017,
         "version" : "3.2.13",
         "gitVersion" : "23899209cad60aaafe114f6aea6cb83025ff51bc"
     },
     "ok" : 1.0
 }

使用$in代替$orMongo $ or vs $ in docs ):

db.getCollection('C').count({'A':{'$in': ['valueA', 'valueB']}}) 

UPDATE

或嘗試使用find(..)。count()這樣:

db.getCollection('C').find({'A':{'$in': ['valueA', 'valueB']}}).count()

經過搜索和搜索,我找到了原因。 我正在搜索的值之一為空值。 索引未覆蓋空值,因此從磁盤讀取了每個文檔,以檢查是否為空值。

在Mongo大學講習班中也提到了這一點。 我強烈建議您參加課程。 他們幫了我很多忙!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM