Mongodb 数据库查询性能优化

Mongodb 数据库查询性能优化,mongodb,indexing,query-optimization,Mongodb,Indexing,Query Optimization,我的商店收藏中有以下文档结构 { "_id": "some_custom_id", "inventory": [ { "productId": "some_prod_id", // ...restAttributes }, // 500+ such items ] } 我正在尝试进行查询coll.find({u id:“some\u id”,“inventory.productId:“some\u prod\u id”},{…}) 查

我的
商店
收藏中有以下文档结构

{
  "_id": "some_custom_id",
  "inventory": [
    {
      "productId": "some_prod_id",
      // ...restAttributes
    },
    // 500+ such items
  ]
}
我正在尝试进行查询
coll.find({u id:“some\u id”,“inventory.productId:“some\u prod\u id”},{…})

查询有时需要很长时间才能返回(10秒左右)。所以我创建了一个索引
{u id:1,“inventory.productId”:1}
,但仍然没有性能提升,所以我尝试了mongo,发现使用了
\u id
索引,而不是我创建的索引。然后我创建了另一个索引
{“inventory.productId”:1,_id:1}
仍然不走运

这是
coll.find({u id:“some\u id”,“inventory.productId:“some\u prod\u id”})的输出。解释(“executionStats”)

所以我有两个问题

  • 如何提高查询性能
  • 我看到索引
    {“inventory.productId:1,{u id:1}
    {u id:1,{inventory.productId:1}
    的大小不同。他们之间有什么区别
  • 有时候Mongo会选择错误的索引,基本上Mongo会在可用索引之间进行小型“竞争”,并选择先获取101个文档的索引

    显然,这并不一定意味着选择了最佳指数。与您的情况一样,为了避免这种情况,这将迫使Mongo使用您选择的索引,这将使查询运行得更快

  • Mongo将其索引构建为一个索引,由于数据分布的性质,树的构建方式不同,并且具有其他大小。 关于他们如何在视频中建立索引,有一个有趣且更深入的解释。但是如果不深入源代码,这对您来说仍然是一个“黑匣子”


  • 您使用的MongoDB版本是什么?另外,请发布使用
    解释(“executionStats”)生成的查询计划。
    @prasad\uz。我已经用查询计划更新了问题,我使用的是
    4.0.16
    mongoldb版本。您的查询计划说它使用的是
    \u id
    字段(“indexName”:“id”)上的默认索引,。此外,不使用其他索引。“executionStats”:。。。具有“executionTimeMillis”:0。那么,问题是什么?告诉集合中有多少文档?问题是,正如问题中提到的,有时查询需要花费很多时间来响应,“executionTimeMillis”很低,因为在执行查询时没有发生并发操作,即使mongo集群有10个并发操作,执行时间也会增加。我有大约500条记录,每个记录在库存阵列中有500个项目。我使用的是m30 mongo atlas集群,我在服务器端有足够的资源@那么,您必须进一步调查这些并发操作及其资源使用情况。它与此集合或查询无关(我认为,根据查询计划输出),我在查询中使用了
    hint
    ,现在正在使用新索引,但大多数情况下使用
    \u id
    索引的查询的性能较低。此外,由于queryPlanner的rejectedPlans为空(附在问题后面),因此没有提示的查询似乎没有在索引之间运行
    竞赛。
    
    {
        "queryPlanner" : {
            "plannerVersion" : 1,
            "namespace" : "somedb.Stores",
            "indexFilterSet" : false,
            "parsedQuery" : {
                "$and" : [ 
                    {
                        "_id" : {
                            "$eq" : "114"
                        }
                    }, 
                    {
                        "inventory.productId" : {
                            "$eq" : "41529689"
                        }
                    }
                ]
            },
            "winningPlan" : {
                "stage" : "FETCH",
                "filter" : {
                    "inventory.productId" : {
                        "$eq" : "41529689"
                    }
                },
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "keyPattern" : {
                        "_id" : 1
                    },
                    "indexName" : "_id_",
                    "isMultiKey" : false,
                    "multiKeyPaths" : {
                        "_id" : []
                    },
                    "isUnique" : true,
                    "isSparse" : false,
                    "isPartial" : false,
                    "indexVersion" : 2,
                    "direction" : "forward",
                    "indexBounds" : {
                        "_id" : [ 
                            "[\"114\", \"114\"]"
                        ]
                    }
                }
            },
            "rejectedPlans" : []
        },
        "executionStats" : {
            "executionSuccess" : true,
            "nReturned" : 1,
            "executionTimeMillis" : 0,
            "totalKeysExamined" : 1,
            "totalDocsExamined" : 1,
            "executionStages" : {
                "stage" : "FETCH",
                "filter" : {
                    "inventory.productId" : {
                        "$eq" : "41529689"
                    }
                },
                "nReturned" : 1,
                "executionTimeMillisEstimate" : 0,
                "works" : 2,
                "advanced" : 1,
                "needTime" : 0,
                "needYield" : 0,
                "saveState" : 0,
                "restoreState" : 0,
                "isEOF" : 1,
                "invalidates" : 0,
                "docsExamined" : 1,
                "alreadyHasObj" : 0,
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "nReturned" : 1,
                    "executionTimeMillisEstimate" : 0,
                    "works" : 2,
                    "advanced" : 1,
                    "needTime" : 0,
                    "needYield" : 0,
                    "saveState" : 0,
                    "restoreState" : 0,
                    "isEOF" : 1,
                    "invalidates" : 0,
                    "keyPattern" : {
                        "_id" : 1
                    },
                    "indexName" : "_id_",
                    "isMultiKey" : false,
                    "multiKeyPaths" : {
                        "_id" : []
                    },
                    "isUnique" : true,
                    "isSparse" : false,
                    "isPartial" : false,
                    "indexVersion" : 2,
                    "direction" : "forward",
                    "indexBounds" : {
                        "_id" : [ 
                            "[\"114\", \"114\"]"
                        ]
                    },
                    "keysExamined" : 1,
                    "seeks" : 1,
                    "dupsTested" : 0,
                    "dupsDropped" : 0,
                    "seenInvalidated" : 0
                }
            }
        },
        "serverInfo" : {
            "host" : "somecluster-shard-00-02-1jury.gcp.mongodb.net",
            "port" : 27017,
            "version" : "4.0.16",
            "gitVersion" : "2a5433168a53044cb6b4fa8083e4cfd7ba142221"
        },
        "ok" : 1.0,
        "operationTime" : Timestamp(1585112231, 1),
        "$clusterTime" : {
            "clusterTime" : Timestamp(1585112231, 1),
            "signature" : {
                "hash" : { "$binary" : "joFIiOgu32NHAVrAO40lHKl7/i8=", "$type" : "00" },
                "keyId" : NumberLong(6778940624956555265)
            }
        }
    }