MongoDb 4.2:在分片集合上进行索引扫描查询非常慢

时间:2019-10-24 14:03:55

标签: mongodb

针对分片集合(MongoDb 4.2.1)运行以下查询时,我遇到非常慢的性能:

db.dc.find({
'hierarchy.company': 'PICCOLO_SRL',
"hierarchy.brand": 'PICCOLO_PICCOLO',
'dcheader.doc_date': {
    $gte: 1569016800,
    $lte: 1571695140
},
'dcheader.cardnr': '0461000402452'})

查询使用以下索引,而不是分片索引:

"key" : {
            "hierarchy.company" : 1,
            "hierarchy.brand" : 1,
            "dcheader.doc_date" : 1,
            "dcheader.cardnr" : 1
        }

该集合包含450万个文档,其大小为20.2GB。碎片索引是哈希类型,碎片是平衡的。

查询执行计划如下:

Screenshot Query execution plan graph

{
"queryPlanner" : {
    "mongosPlannerVersion" : 1,
    "winningPlan" : {
        "stage" : "SHARD_MERGE",
        "shards" : [
            {
                "shardName" : "rsshard1",
                "connectionString" : "rsshard1/kcc1prod.sipos.it:27020,kcc2prod.sipos.it:27020,kcc3prod.sipos.it:27020",
                "serverInfo" : {
                    "host" : "kcc2prod.sipos.it",
                    "port" : 27020,
                    "version" : "4.2.1",
                    "gitVersion" : "edf6d45851c0b9ee15548f0f847df141764a317e"
                },
                "plannerVersion" : 1,
                "namespace" : "kaneda.dc",
                "indexFilterSet" : false,
                "parsedQuery" : {
                    "$and" : [
                        {
                            "dcheader.cardnr" : {
                                "$eq" : "0461000402452"
                            }
                        },
                        {
                            "hierarchy.brand" : {
                                "$eq" : "PICCOLO_PICCOLO"
                            }
                        },
                        {
                            "hierarchy.company" : {
                                "$eq" : "PICCOLO_SRL"
                            }
                        },
                        {
                            "dcheader.doc_date" : {
                                "$lte" : 1571695140
                            }
                        },
                        {
                            "dcheader.doc_date" : {
                                "$gte" : 1569016800
                            }
                        }
                    ]
                },
                "winningPlan" : {
                    "stage" : "SHARDING_FILTER",
                    "inputStage" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "keyPattern" : {
                                "hierarchy.company" : 1,
                                "hierarchy.brand" : 1,
                                "dcheader.doc_date" : 1,
                                "dcheader.cardnr" : 1
                            },
                            "indexName" : "IndexCardnrDocDate",
                            "isMultiKey" : false,
                            "multiKeyPaths" : {
                                "hierarchy.company" : [ ],
                                "hierarchy.brand" : [ ],
                                "dcheader.doc_date" : [ ],
                                "dcheader.cardnr" : [ ]
                            },
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 2,
                            "direction" : "forward",
                            "indexBounds" : {
                                "hierarchy.company" : [
                                    "[\"PICCOLO_SRL\", \"PICCOLO_SRL\"]"
                                ],
                                "hierarchy.brand" : [
                                    "[\"PICCOLO_PICCOLO\", \"PICCOLO_PICCOLO\"]"
                                ],
                                "dcheader.doc_date" : [
                                    "[1569016800.0, 1571695140.0]"
                                ],
                                "dcheader.cardnr" : [
                                    "[\"0461000402452\", \"0461000402452\"]"
                                ]
                            }
                        }
                    }
                },
                "rejectedPlans" : [
                    {
                        "stage" : "SHARDING_FILTER",
                        "inputStage" : {
                            "stage" : "FETCH",
                            "filter" : {
                                "$and" : [
                                    {
                                        "dcheader.cardnr" : {
                                            "$eq" : "0461000402452"
                                        }
                                    },
                                    {
                                        "dcheader.doc_date" : {
                                            "$lte" : 1571695140
                                        }
                                    },
                                    {
                                        "dcheader.doc_date" : {
                                            "$gte" : 1569016800
                                        }
                                    }
                                ]
                            },
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                    "hierarchy.company" : 1,
                                    "hierarchy.brand" : 1,
                                    "hierarchy.zone" : 1,
                                    "hierarchy.pdv" : 1,
                                    "dcheader.idscontrino" : 1
                                },
                                "indexName" : "IndexIdScontrino",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                    "hierarchy.company" : [ ],
                                    "hierarchy.brand" : [ ],
                                    "hierarchy.zone" : [ ],
                                    "hierarchy.pdv" : [ ],
                                    "dcheader.idscontrino" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                    "hierarchy.company" : [
                                        "[\"PICCOLO_SRL\", \"PICCOLO_SRL\"]"
                                    ],
                                    "hierarchy.brand" : [
                                        "[\"PICCOLO_PICCOLO\", \"PICCOLO_PICCOLO\"]"
                                    ],
                                    "hierarchy.zone" : [
                                        "[MinKey, MaxKey]"
                                    ],
                                    "hierarchy.pdv" : [
                                        "[MinKey, MaxKey]"
                                    ],
                                    "dcheader.idscontrino" : [
                                        "[MinKey, MaxKey]"
                                    ]
                                }
                            }
                        }
                    },
                    {
                        "stage" : "SHARDING_FILTER",
                        "inputStage" : {
                            "stage" : "FETCH",
                            "filter" : {
                                "$and" : [
                                    {
                                        "dcheader.cardnr" : {
                                            "$eq" : "0461000402452"
                                        }
                                    },
                                    {
                                        "dcheader.doc_date" : {
                                            "$lte" : 1571695140
                                        }
                                    },
                                    {
                                        "dcheader.doc_date" : {
                                            "$gte" : 1569016800
                                        }
                                    }
                                ]
                            },
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                    "hierarchy.company" : 1,
                                    "hierarchy.brand" : 1,
                                    "dcheader.deptnr" : 1,
                                    "dcheader.tillid" : 1,
                                    "dcheader.numdocumentogestionale" : 1,
                                    "dcheader.fiscalprinternr" : 1
                                },
                                "indexName" : "IndexDcDocGestionale",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                    "hierarchy.company" : [ ],
                                    "hierarchy.brand" : [ ],
                                    "dcheader.deptnr" : [ ],
                                    "dcheader.tillid" : [ ],
                                    "dcheader.numdocumentogestionale" : [ ],
                                    "dcheader.fiscalprinternr" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                    "hierarchy.company" : [
                                        "[\"PICCOLO_SRL\", \"PICCOLO_SRL\"]"
                                    ],
                                    "hierarchy.brand" : [
                                        "[\"PICCOLO_PICCOLO\", \"PICCOLO_PICCOLO\"]"
                                    ],
                                    "dcheader.deptnr" : [
                                        "[MinKey, MaxKey]"
                                    ],
                                    "dcheader.tillid" : [
                                        "[MinKey, MaxKey]"
                                    ],
                                    "dcheader.numdocumentogestionale" : [
                                        "[MinKey, MaxKey]"
                                    ],
                                    "dcheader.fiscalprinternr" : [
                                        "[MinKey, MaxKey]"
                                    ]
                                }
                            }
                        }
                    }
                ]
            },
            {
                "shardName" : "rsshard2",
                "connectionString" : "rsshard2/kcc1prod.sipos.it:27021,kcc2prod.sipos.it:27021,kcc3prod.sipos.it:27021",
                "serverInfo" : {
                    "host" : "kcc3prod.sipos.it",
                    "port" : 27021,
                    "version" : "4.2.1",
                    "gitVersion" : "edf6d45851c0b9ee15548f0f847df141764a317e"
                },
                "plannerVersion" : 1,
                "namespace" : "kaneda.dc",
                "indexFilterSet" : false,
                "parsedQuery" : {
                    "$and" : [
                        {
                            "dcheader.cardnr" : {
                                "$eq" : "0461000402452"
                            }
                        },
                        {
                            "hierarchy.brand" : {
                                "$eq" : "PICCOLO_PICCOLO"
                            }
                        },
                        {
                            "hierarchy.company" : {
                                "$eq" : "PICCOLO_SRL"
                            }
                        },
                        {
                            "dcheader.doc_date" : {
                                "$lte" : 1571695140
                            }
                        },
                        {
                            "dcheader.doc_date" : {
                                "$gte" : 1569016800
                            }
                        }
                    ]
                },
                "winningPlan" : {
                    "stage" : "SHARDING_FILTER",
                    "inputStage" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "keyPattern" : {
                                "hierarchy.company" : 1,
                                "hierarchy.brand" : 1,
                                "dcheader.doc_date" : 1,
                                "dcheader.cardnr" : 1
                            },
                            "indexName" : "IndexCardnrDocDate",
                            "isMultiKey" : false,
                            "multiKeyPaths" : {
                                "hierarchy.company" : [ ],
                                "hierarchy.brand" : [ ],
                                "dcheader.doc_date" : [ ],
                                "dcheader.cardnr" : [ ]
                            },
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 2,
                            "direction" : "forward",
                            "indexBounds" : {
                                "hierarchy.company" : [
                                    "[\"PICCOLO_SRL\", \"PICCOLO_SRL\"]"
                                ],
                                "hierarchy.brand" : [
                                    "[\"PICCOLO_PICCOLO\", \"PICCOLO_PICCOLO\"]"
                                ],
                                "dcheader.doc_date" : [
                                    "[1569016800.0, 1571695140.0]"
                                ],
                                "dcheader.cardnr" : [
                                    "[\"0461000402452\", \"0461000402452\"]"
                                ]
                            }
                        }
                    }
                },
                "rejectedPlans" : [
                    {
                        "stage" : "SHARDING_FILTER",
                        "inputStage" : {
                            "stage" : "FETCH",
                            "filter" : {
                                "$and" : [
                                    {
                                        "dcheader.cardnr" : {
                                            "$eq" : "0461000402452"
                                        }
                                    },
                                    {
                                        "dcheader.doc_date" : {
                                            "$lte" : 1571695140
                                        }
                                    },
                                    {
                                        "dcheader.doc_date" : {
                                            "$gte" : 1569016800
                                        }
                                    }
                                ]
                            },
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                    "hierarchy.company" : 1,
                                    "hierarchy.brand" : 1,
                                    "hierarchy.zone" : 1,
                                    "hierarchy.pdv" : 1,
                                    "dcheader.idscontrino" : 1
                                },
                                "indexName" : "IndexIdScontrino",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                    "hierarchy.company" : [ ],
                                    "hierarchy.brand" : [ ],
                                    "hierarchy.zone" : [ ],
                                    "hierarchy.pdv" : [ ],
                                    "dcheader.idscontrino" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                    "hierarchy.company" : [
                                        "[\"PICCOLO_SRL\", \"PICCOLO_SRL\"]"
                                    ],
                                    "hierarchy.brand" : [
                                        "[\"PICCOLO_PICCOLO\", \"PICCOLO_PICCOLO\"]"
                                    ],
                                    "hierarchy.zone" : [
                                        "[MinKey, MaxKey]"
                                    ],
                                    "hierarchy.pdv" : [
                                        "[MinKey, MaxKey]"
                                    ],
                                    "dcheader.idscontrino" : [
                                        "[MinKey, MaxKey]"
                                    ]
                                }
                            }
                        }
                    },
                    {
                        "stage" : "SHARDING_FILTER",
                        "inputStage" : {
                            "stage" : "FETCH",
                            "filter" : {
                                "$and" : [
                                    {
                                        "dcheader.cardnr" : {
                                            "$eq" : "0461000402452"
                                        }
                                    },
                                    {
                                        "dcheader.doc_date" : {
                                            "$lte" : 1571695140
                                        }
                                    },
                                    {
                                        "dcheader.doc_date" : {
                                            "$gte" : 1569016800
                                        }
                                    }
                                ]
                            },
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                    "hierarchy.company" : 1,
                                    "hierarchy.brand" : 1,
                                    "dcheader.deptnr" : 1,
                                    "dcheader.tillid" : 1,
                                    "dcheader.numdocumentogestionale" : 1,
                                    "dcheader.fiscalprinternr" : 1
                                },
                                "indexName" : "IndexDcDocGestionale",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                    "hierarchy.company" : [ ],
                                    "hierarchy.brand" : [ ],
                                    "dcheader.deptnr" : [ ],
                                    "dcheader.tillid" : [ ],
                                    "dcheader.numdocumentogestionale" : [ ],
                                    "dcheader.fiscalprinternr" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                    "hierarchy.company" : [
                                        "[\"PICCOLO_SRL\", \"PICCOLO_SRL\"]"
                                    ],
                                    "hierarchy.brand" : [
                                        "[\"PICCOLO_PICCOLO\", \"PICCOLO_PICCOLO\"]"
                                    ],
                                    "dcheader.deptnr" : [
                                        "[MinKey, MaxKey]"
                                    ],
                                    "dcheader.tillid" : [
                                        "[MinKey, MaxKey]"
                                    ],
                                    "dcheader.numdocumentogestionale" : [
                                        "[MinKey, MaxKey]"
                                    ],
                                    "dcheader.fiscalprinternr" : [
                                        "[MinKey, MaxKey]"
                                    ]
                                }
                            }
                        }
                    }
                ]
            }
        ]
    }
},
"executionStats" : {
    "nReturned" : 0,
    "executionTimeMillis" : 170263,
    "totalKeysExamined" : 555702,
    "totalDocsExamined" : 0,
    "executionStages" : {
        "stage" : "SHARD_MERGE",
        "nReturned" : 0,
        "executionTimeMillis" : 170263,
        "totalKeysExamined" : 555702,
        "totalDocsExamined" : 0,
        "totalChildMillis" : 255673,
        "shards" : [
            {
                "shardName" : "rsshard1",
                "executionSuccess" : true,
                "executionStages" : {
                    "stage" : "SHARDING_FILTER",
                    "nReturned" : 0,
                    "executionTimeMillisEstimate" : 1447,
                    "works" : 202303,
                    "advanced" : 0,
                    "needTime" : 202301,
                    "needYield" : 0,
                    "saveState" : 6882,
                    "restoreState" : 6882,
                    "isEOF" : 1,
                    "chunkSkips" : 0,
                    "inputStage" : {
                        "stage" : "FETCH",
                        "nReturned" : 0,
                        "executionTimeMillisEstimate" : 1428,
                        "works" : 202302,
                        "advanced" : 0,
                        "needTime" : 202301,
                        "needYield" : 0,
                        "saveState" : 6882,
                        "restoreState" : 6882,
                        "isEOF" : 1,
                        "docsExamined" : 0,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "nReturned" : 0,
                            "executionTimeMillisEstimate" : 1428,
                            "works" : 202302,
                            "advanced" : 0,
                            "needTime" : 202301,
                            "needYield" : 0,
                            "saveState" : 6882,
                            "restoreState" : 6882,
                            "isEOF" : 1,
                            "keyPattern" : {
                                "hierarchy.company" : 1,
                                "hierarchy.brand" : 1,
                                "dcheader.doc_date" : 1,
                                "dcheader.cardnr" : 1
                            },
                            "indexName" : "IndexCardnrDocDate",
                            "isMultiKey" : false,
                            "multiKeyPaths" : {
                                "hierarchy.company" : [ ],
                                "hierarchy.brand" : [ ],
                                "dcheader.doc_date" : [ ],
                                "dcheader.cardnr" : [ ]
                            },
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 2,
                            "direction" : "forward",
                            "indexBounds" : {
                                "hierarchy.company" : [
                                    "[\"PICCOLO_SRL\", \"PICCOLO_SRL\"]"
                                ],
                                "hierarchy.brand" : [
                                    "[\"PICCOLO_PICCOLO\", \"PICCOLO_PICCOLO\"]"
                                ],
                                "dcheader.doc_date" : [
                                    "[1569016800.0, 1571695140.0]"
                                ],
                                "dcheader.cardnr" : [
                                    "[\"0461000402452\", \"0461000402452\"]"
                                ]
                            },
                            "keysExamined" : 202302,
                            "seeks" : 202302,
                            "dupsTested" : 0,
                            "dupsDropped" : 0,
                            "indexDef" : {
                                "indexName" : "IndexCardnrDocDate",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                    "hierarchy.company" : [ ],
                                    "hierarchy.brand" : [ ],
                                    "dcheader.doc_date" : [ ],
                                    "dcheader.cardnr" : [ ]
                                },
                                "keyPattern" : {
                                    "hierarchy.company" : 1,
                                    "hierarchy.brand" : 1,
                                    "dcheader.doc_date" : 1,
                                    "dcheader.cardnr" : 1
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "direction" : "forward"
                            }
                        }
                    }
                }
            },
            {
                "shardName" : "rsshard2",
                "executionSuccess" : true,
                "executionStages" : {
                    "stage" : "SHARDING_FILTER",
                    "nReturned" : 0,
                    "executionTimeMillisEstimate" : 1677,
                    "works" : 353401,
                    "advanced" : 0,
                    "needTime" : 353399,
                    "needYield" : 0,
                    "saveState" : 12612,
                    "restoreState" : 12612,
                    "isEOF" : 1,
                    "chunkSkips" : 0,
                    "inputStage" : {
                        "stage" : "FETCH",
                        "nReturned" : 0,
                        "executionTimeMillisEstimate" : 1660,
                        "works" : 353400,
                        "advanced" : 0,
                        "needTime" : 353399,
                        "needYield" : 0,
                        "saveState" : 12612,
                        "restoreState" : 12612,
                        "isEOF" : 1,
                        "docsExamined" : 0,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "nReturned" : 0,
                            "executionTimeMillisEstimate" : 1656,
                            "works" : 353400,
                            "advanced" : 0,
                            "needTime" : 353399,
                            "needYield" : 0,
                            "saveState" : 12612,
                            "restoreState" : 12612,
                            "isEOF" : 1,
                            "keyPattern" : {
                                "hierarchy.company" : 1,
                                "hierarchy.brand" : 1,
                                "dcheader.doc_date" : 1,
                                "dcheader.cardnr" : 1
                            },
                            "indexName" : "IndexCardnrDocDate",
                            "isMultiKey" : false,
                            "multiKeyPaths" : {
                                "hierarchy.company" : [ ],
                                "hierarchy.brand" : [ ],
                                "dcheader.doc_date" : [ ],
                                "dcheader.cardnr" : [ ]
                            },
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 2,
                            "direction" : "forward",
                            "indexBounds" : {
                                "hierarchy.company" : [
                                    "[\"PICCOLO_SRL\", \"PICCOLO_SRL\"]"
                                ],
                                "hierarchy.brand" : [
                                    "[\"PICCOLO_PICCOLO\", \"PICCOLO_PICCOLO\"]"
                                ],
                                "dcheader.doc_date" : [
                                    "[1569016800.0, 1571695140.0]"
                                ],
                                "dcheader.cardnr" : [
                                    "[\"0461000402452\", \"0461000402452\"]"
                                ]
                            },
                            "keysExamined" : 353400,
                            "seeks" : 353400,
                            "dupsTested" : 0,
                            "dupsDropped" : 0,
                            "indexDef" : {
                                "indexName" : "IndexCardnrDocDate",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                    "hierarchy.company" : [ ],
                                    "hierarchy.brand" : [ ],
                                    "dcheader.doc_date" : [ ],
                                    "dcheader.cardnr" : [ ]
                                },
                                "keyPattern" : {
                                    "hierarchy.company" : 1,
                                    "hierarchy.brand" : 1,
                                    "dcheader.doc_date" : 1,
                                    "dcheader.cardnr" : 1
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "direction" : "forward"
                            }
                        }
                    }
                }
            }
        ]
    }
},
"ok" : 1,
"operationTime" : Timestamp(1571924213, 6),
"$clusterTime" : {
    "clusterTime" : Timestamp(1571924213, 6),
    "signature" : {
        "hash" : BinData(0,"YOMe4acOH+iIfoHlsq/dCErNiZc="),
        "keyId" : 6708447541475672066
    }
}

}

正如您所看到的,似乎SHARD_MERGE阶段大约需要168000毫秒:有人可以解释为什么吗?

请注意,使用以前的MongoDb版本(3.6),我从未有过如此糟糕的表现。

我还没有找到有关SHARD_MERGE阶段的详细信息:哪个进程执行此任务(mongod或mongos)?在此阶段会发生什么?

我的体系结构由三台Centos 7机器组成,每台机器托管三个mongod进程(shard1副本集,shard2副本集和配置副本集)和一个mongos进程。

2 个答案:

答案 0 :(得分:0)

该解释计划中显示的阶段按执行顺序为:

在mongod碎片上:
IXSCAN-将查询与索引值进行比较
FETCH-检索完整文档
SHARDING_FILTER-删除所有不是分片所有的文档(即孤儿)

在mongos:
SHARD_MERGE-等待,然后合并分片中的结果集

每个分片的执行时间与SHARD_MERGE阶段之间的巨大差异表明,一个分片的启动时间比另一个分片要晚得多,这延迟了响应,或者此处可能存在时钟偏斜。

答案 1 :(得分:0)

您可以尝试将索引顺序更改为此。

"key" : {
   "hierarchy.company" : 1,
   "hierarchy.brand" : 1,
   "dcheader.cardnr" : 1
   "dcheader.doc_date" : 1,
}

通常的经验法则是索引字段的顺序应为以下顺序。

  • 范围字段之前的相等字段
  • 在范围字段之前对字段进行排序
  • 排序字段之前的相等字段

在您的情况下,“范围”字段(doc_date)在“平等”字段(cardnr)之前。