【问题标题】:mongoldb query performance optimisationmongodb查询性能优化
【发布时间】:2020-07-05 00:03:24
【问题描述】:

我的store 集合中有以下文档结构,

{
  "_id": "some_custom_id",
  "inventory": [
    {
      "productId": "some_prod_id",
      // ...restAttributes
    },
    // 500+ such items
  ]
}

我正在尝试查询coll.find({_id:"some_id","inventory.productId":"some_prod_id"},{...})

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

这是coll.find({_id:"some_id","inventory.productId":"some_prod_id"}).explain("executionStats")的输出

{
    "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)
        }
    }
}

所以我有两个问题,

  1. 如何提高查询性能?
  2. 我看到索引{"inventory.productId":1, _id:1} 和{_id:1,"inventory.productId":1} 的大小不同。它们有什么区别?

【问题讨论】:

  • 您使用的 MongoDB 版本是什么?另外,请发布使用explain("executionStats").生成的查询计划。
  • @prasad_ 我已经用查询计划更新了问题,我正在使用4.0.16 mongoldb 版本。
  • 您的查询计划说它正在使用_id 字段上的默认索引(“indexName”:“id”)。此外,不使用其他索引。 “executionStats”:......有“executionTimeMillis”:0。那么,有什么问题?告诉集合中有多少文档?
  • 问题是,正如问题中提到的,有时查询需要很长时间才能响应,“executionTimeMillis”很低,因为我执行查询时没有发生并发操作,即使执行时间也会增加mongo 集群有 10 个并发操作。我有大约 500 条记录,每条记录在库存数组中有 500 个项目。我正在使用 m30 mongo atlas 集群,我在服务器端有足够的资源。 @prasad_
  • 然后,您必须进一步调查这些并发操作及其资源使用情况。它与此集合或查询无关(我认为,根据查询计划输出)。

标签: mongodb indexing query-optimization


【解决方案1】:
  1. 有时 Mongo 会选择错误的索引,基本上 Mongo 会在可用索引之间进行小型“竞赛”,然后选择首先获取 101 个文档的索引。

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

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

【讨论】:

  • 我在查询中使用了hint,现在正在使用新索引,但大多数时候性能仍然低于使用_id 索引的查询。也没有提示的查询似乎没有在索引之间运行race,因为queryPlanner 的rejectPlans 是空的(附加到问题中)。
猜你喜欢
  • 1970-01-01
  • 2021-12-06
  • 2016-12-01
  • 2013-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-26
  • 2015-01-21
相关资源
最近更新 更多