Mongodb 搜索分片键很慢

Mongodb Searching sharding key is very slow

我使用 bitnami 的图表设置了一个包含 3 个分片的 mongodb:mongodb-sharded 我遇到了两个问题。

我的分片 collection 有超过 3800 万条记录。

第一个问题是有些mongodb pods 占用大量内存,从一个节点32G的整体内存中占用了10G多。我从 Whosebug 发现了一些类似的问题,我会尝试解决这个问题。

另一个问题是:当我搜索匹配分片键时,速度很慢,有时会超时。

这是我的 collection 的索引:

ice-shard-mongodb-sharded-shard-0:PRIMARY> db.LogInfo.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "collation" : {
            "locale" : "en_US",
            "caseLevel" : false,
            "caseFirst" : "off",
            "strength" : 2,
            "numericOrdering" : false,
            "alternate" : "non-ignorable",
            "maxVariable" : "punct",
            "normalization" : false,
            "backwards" : false,
            "version" : "57.1"
        }
    },
    {
        "v" : 2,
        "key" : {
            "Properties.Time" : 1
        },
        "name" : "Properties.Time_1",
        "collation" : {
            "locale" : "en_US",
            "caseLevel" : false,
            "caseFirst" : "off",
            "strength" : 2,
            "numericOrdering" : false,
            "alternate" : "non-ignorable",
            "maxVariable" : "punct",
            "normalization" : false,
            "backwards" : false,
            "version" : "57.1"
        }
    },
    {
        "v" : 2,
        "key" : {
            "Properties.Plot" : 1,
            "Properties.Time" : 1
        },
        "name" : "Properties.Plot_1_Properties.Time_1",
        "collation" : {
            "locale" : "en_US",
            "caseLevel" : false,
            "caseFirst" : "off",
            "strength" : 2,
            "numericOrdering" : false,
            "alternate" : "non-ignorable",
            "maxVariable" : "punct",
            "normalization" : false,
            "backwards" : false,
            "version" : "57.1"
        }
    },
    {
        "v" : 2,
        "key" : {
            "Properties.Scenario" : 1,
            "Properties.Time" : 1
        },
        "name" : "Properties.Scenario_1_Properties.Time_1",
        "collation" : {
            "locale" : "en_US",
            "caseLevel" : false,
            "caseFirst" : "off",
            "strength" : 2,
            "numericOrdering" : false,
            "alternate" : "non-ignorable",
            "maxVariable" : "punct",
            "normalization" : false,
            "backwards" : false,
            "version" : "57.1"
        }
    },
    {
        "v" : 2,
        "key" : {
            "Properties.SecsFunction" : 1,
            "Properties.Time" : 1
        },
        "name" : "Properties.SecsFunction_1_Properties.Time_1",
        "collation" : {
            "locale" : "en_US",
            "caseLevel" : false,
            "caseFirst" : "off",
            "strength" : 2,
            "numericOrdering" : false,
            "alternate" : "non-ignorable",
            "maxVariable" : "punct",
            "normalization" : false,
            "backwards" : false,
            "version" : "57.1"
        }
    },
    {
        "v" : 2,
        "key" : {
            "Properties.EapId" : "hashed",
            "Properties.Time" : 1
        },
        "name" : "Properties.EapId_hashed_Properties.Time_1"
    }
]

键“Properties.EapId_hashed_Properties.Time_1”为分片键

如果我搜索

db.LogInfo.find({"Properties.SecsFunction": "s6f11", "Properties.Time": {$gte: ISODate("2021-03-10")}})

此搜索匹配一个索引,它将搜索所有 3 个分片。 0.01秒内非常快

但是如果我搜索

db.LogInfo.find(
    {
        "Properties.EapId": "12eap012",
        "Properties.Time": {
            "$gte": ISODate("2021-03-17")
        }
    }
)

应该用sharding key,我觉得,很流畅。有时会超过5秒,有时会超时。我已经检查了几次索引,但我不明白为什么。

请指教。谢谢。

更新:Mongodb版本:4.4.3

更新:分片构建为副本集 = 3。

更新:下面是有问题的搜索的查询计划。 (由于 Whosebug 的 30000 个字符限制,我放弃了 executionStat()。)

{ 
    "queryPlanner" : { 
        "mongosPlannerVersion" : NumberInt(1), 
        "winningPlan" : { 
            "stage" : "SHARD_MERGE", 
            "shards" : [
                { 
                    "shardName" : "ice-shard-mongodb-sharded-shard-1", 
                    "connectionString" : "ice-shard-mongodb-sharded-shard-1/ice-shard-mongodb-sharded-shard1-data-0.ice-shard-mongodb-sharded-headless.ice-system.svc.cluster.local:27017,ice-shard-mongodb-sharded-shard1-data-1.ice-shard-mongodb-sharded-headless.ice-system.svc.cluster.local:27017,ice-shard-mongodb-sharded-shard1-data-2.ice-shard-mongodb-sharded-headless.ice-system.svc.cluster.local:27017", 
                    "serverInfo" : { 
                        "host" : "ice-shard-mongodb-sharded-shard1-data-1", 
                        "port" : NumberInt(27017), 
                        "version" : "4.4.3", 
                        "gitVersion" : "913d6b62acfbb344dde1b116f4161360acd8fd13"
                    }, 
                    "plannerVersion" : NumberInt(1), 
                    "namespace" : "LogCenter.LogInfo", 
                    "indexFilterSet" : false, 
                    "parsedQuery" : { 
                        "$and" : [
                            { 
                                "Properties.EapId" : { 
                                    "$eq" : "12eap012"
                                }
                            }, 
                            { 
                                "Properties.Time" : { 
                                    "$gte" : ISODate("2021-03-17T00:00:00.000+0000")
                                }
                            }
                        ]
                    }, 
                    "collation" : { 
                        "locale" : "en_US", 
                        "caseLevel" : false, 
                        "caseFirst" : "off", 
                        "strength" : NumberInt(2), 
                        "numericOrdering" : false, 
                        "alternate" : "non-ignorable", 
                        "maxVariable" : "punct", 
                        "normalization" : false, 
                        "backwards" : false, 
                        "version" : "57.1"
                    }, 
                    "winningPlan" : { 
                        "stage" : "SHARDING_FILTER", 
                        "inputStage" : { 
                            "stage" : "FETCH", 
                            "filter" : { 
                                "Properties.EapId" : { 
                                    "$eq" : "12eap012"
                                }
                            }, 
                            "inputStage" : { 
                                "stage" : "IXSCAN", 
                                "keyPattern" : { 
                                    "Properties.Time" : 1.0
                                }, 
                                "indexName" : "Properties.Time_1", 
                                "collation" : { 
                                    "locale" : "en_US", 
                                    "caseLevel" : false, 
                                    "caseFirst" : "off", 
                                    "strength" : NumberInt(2), 
                                    "numericOrdering" : false, 
                                    "alternate" : "non-ignorable", 
                                    "maxVariable" : "punct", 
                                    "normalization" : false, 
                                    "backwards" : false, 
                                    "version" : "57.1"
                                }, 
                                "isMultiKey" : false, 
                                "multiKeyPaths" : { 
                                    "Properties.Time" : [

                                    ]
                                }, 
                                "isUnique" : false, 
                                "isSparse" : false, 
                                "isPartial" : false, 
                                "indexVersion" : NumberInt(2), 
                                "direction" : "forward", 
                                "indexBounds" : { 
                                    "Properties.Time" : [
                                        "[new Date(1615939200000), new Date(9223372036854775807)]"
                                    ]
                                }
                            }
                        }
                    }, 
                    "rejectedPlans" : [

                    ]
                }, 
                { 
                    "shardName" : "ice-shard-mongodb-sharded-shard-0", 
                    "connectionString" : "ice-shard-mongodb-sharded-shard-0/ice-shard-mongodb-sharded-shard0-data-0.ice-shard-mongodb-sharded-headless.ice-system.svc.cluster.local:27017,ice-shard-mongodb-sharded-shard0-data-1.ice-shard-mongodb-sharded-headless.ice-system.svc.cluster.local:27017,ice-shard-mongodb-sharded-shard0-data-2.ice-shard-mongodb-sharded-headless.ice-system.svc.cluster.local:27017", 
                    "serverInfo" : { 
                        "host" : "ice-shard-mongodb-sharded-shard0-data-0", 
                        "port" : NumberInt(27017), 
                        "version" : "4.4.3", 
                        "gitVersion" : "913d6b62acfbb344dde1b116f4161360acd8fd13"
                    }, 
                    "plannerVersion" : NumberInt(1), 
                    "namespace" : "LogCenter.LogInfo", 
                    "indexFilterSet" : false, 
                    "parsedQuery" : { 
                        "$and" : [
                            { 
                                "Properties.EapId" : { 
                                    "$eq" : "12eap012"
                                }
                            }, 
                            { 
                                "Properties.Time" : { 
                                    "$gte" : ISODate("2021-03-17T00:00:00.000+0000")
                                }
                            }
                        ]
                    }, 
                    "collation" : { 
                        "locale" : "en_US", 
                        "caseLevel" : false, 
                        "caseFirst" : "off", 
                        "strength" : NumberInt(2), 
                        "numericOrdering" : false, 
                        "alternate" : "non-ignorable", 
                        "maxVariable" : "punct", 
                        "normalization" : false, 
                        "backwards" : false, 
                        "version" : "57.1"
                    }, 
                    "winningPlan" : { 
                        "stage" : "SHARDING_FILTER", 
                        "inputStage" : { 
                            "stage" : "FETCH", 
                            "filter" : { 
                                "Properties.EapId" : { 
                                    "$eq" : "12eap012"
                                }
                            }, 
                            "inputStage" : { 
                                "stage" : "IXSCAN", 
                                "keyPattern" : { 
                                    "Properties.Time" : 1.0
                                }, 
                                "indexName" : "Properties.Time_1", 
                                "collation" : { 
                                    "locale" : "en_US", 
                                    "caseLevel" : false, 
                                    "caseFirst" : "off", 
                                    "strength" : NumberInt(2), 
                                    "numericOrdering" : false, 
                                    "alternate" : "non-ignorable", 
                                    "maxVariable" : "punct", 
                                    "normalization" : false, 
                                    "backwards" : false, 
                                    "version" : "57.1"
                                }, 
                                "isMultiKey" : false, 
                                "multiKeyPaths" : { 
                                    "Properties.Time" : [

                                    ]
                                }, 
                                "isUnique" : false, 
                                "isSparse" : false, 
                                "isPartial" : false, 
                                "indexVersion" : NumberInt(2), 
                                "direction" : "forward", 
                                "indexBounds" : { 
                                    "Properties.Time" : [
                                        "[new Date(1615939200000), new Date(9223372036854775807)]"
                                    ]
                                }
                            }
                        }
                    }, 
                    "rejectedPlans" : [

                    ]
                }, 
                { 
                    "shardName" : "ice-shard-mongodb-sharded-shard-2", 
                    "connectionString" : "ice-shard-mongodb-sharded-shard-2/ice-shard-mongodb-sharded-shard2-data-0.ice-shard-mongodb-sharded-headless.ice-system.svc.cluster.local:27017,ice-shard-mongodb-sharded-shard2-data-1.ice-shard-mongodb-sharded-headless.ice-system.svc.cluster.local:27017,ice-shard-mongodb-sharded-shard2-data-2.ice-shard-mongodb-sharded-headless.ice-system.svc.cluster.local:27017", 
                    "serverInfo" : { 
                        "host" : "ice-shard-mongodb-sharded-shard2-data-0", 
                        "port" : NumberInt(27017), 
                        "version" : "4.4.3", 
                        "gitVersion" : "913d6b62acfbb344dde1b116f4161360acd8fd13"
                    }, 
                    "plannerVersion" : NumberInt(1), 
                    "namespace" : "LogCenter.LogInfo", 
                    "indexFilterSet" : false, 
                    "parsedQuery" : { 
                        "$and" : [
                            { 
                                "Properties.EapId" : { 
                                    "$eq" : "12eap012"
                                }
                            }, 
                            { 
                                "Properties.Time" : { 
                                    "$gte" : ISODate("2021-03-17T00:00:00.000+0000")
                                }
                            }
                        ]
                    }, 
                    "collation" : { 
                        "locale" : "en_US", 
                        "caseLevel" : false, 
                        "caseFirst" : "off", 
                        "strength" : NumberInt(2), 
                        "numericOrdering" : false, 
                        "alternate" : "non-ignorable", 
                        "maxVariable" : "punct", 
                        "normalization" : false, 
                        "backwards" : false, 
                        "version" : "57.1"
                    }, 
                    "winningPlan" : { 
                        "stage" : "SHARDING_FILTER", 
                        "inputStage" : { 
                            "stage" : "FETCH", 
                            "filter" : { 
                                "Properties.EapId" : { 
                                    "$eq" : "12eap012"
                                }
                            }, 
                            "inputStage" : { 
                                "stage" : "IXSCAN", 
                                "keyPattern" : { 
                                    "Properties.Time" : 1.0
                                }, 
                                "indexName" : "Properties.Time_1", 
                                "collation" : { 
                                    "locale" : "en_US", 
                                    "caseLevel" : false, 
                                    "caseFirst" : "off", 
                                    "strength" : NumberInt(2), 
                                    "numericOrdering" : false, 
                                    "alternate" : "non-ignorable", 
                                    "maxVariable" : "punct", 
                                    "normalization" : false, 
                                    "backwards" : false, 
                                    "version" : "57.1"
                                }, 
                                "isMultiKey" : false, 
                                "multiKeyPaths" : { 
                                    "Properties.Time" : [

                                    ]
                                }, 
                                "isUnique" : false, 
                                "isSparse" : false, 
                                "isPartial" : false, 
                                "indexVersion" : NumberInt(2), 
                                "direction" : "forward", 
                                "indexBounds" : { 
                                    "Properties.Time" : [
                                        "[new Date(1615939200000), new Date(9223372036854775807)]"
                                    ]
                                }
                            }
                        }
                    }, 
                    "rejectedPlans" : [

                    ]
                }
            ]
        }
    }, 
    "serverInfo" : { 
        "host" : "ice-shard-mongodb-sharded-mongos-59576fb8b8-n47n5", 
        "port" : NumberInt(27017), 
        "version" : "4.4.3", 
        "gitVersion" : "913d6b62acfbb344dde1b116f4161360acd8fd13"
    }, 
    "ok" : 1.0, 
    "operationTime" : Timestamp(1616372015, 12), 
    "$clusterTime" : { 
        "clusterTime" : Timestamp(1616372015, 14), 
        "signature" : { 
            "hash" : BinData(0, "vVxiZGic6un9QIInadoHCfBCX2Y="), 
            "keyId" : NumberLong(6924833040434724866)
        }
    }
}

我找到了我的搜索没有使用分片键的根本原因。一切都是为了整理。

分片键总是使用 collation : { locale : "simple" } 进行二进制比较,而在我的情况下,我的 collection 和索引设计为使用 "collation" : { "locale" : "en_US", "strength" : 2 }.

在没有任何整理的情况下重新设计了我的 collection,现在一切正常。