首页 > 解决方案 > MongoDB查询性能优化

问题描述

我的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)
        }
    }
}

所以我有2个问题,

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

标签: mongodbindexingquery-optimization

解决方案


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

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

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


推荐阅读