和 MySQL 以及其他计算引擎类似,MongoDB 给我们提供了 explain 命令来查看某个查询的执行计划,其使用也比较简单,具体如下:
db.collection.explain().<method(...)>
explain 命令默认是打印出查询的 queryPlanner,也就是什么参数都不传递。从 3.5.5 版本开始,explain 命名还支持 executionStats 和 allPlansExecution 两种运行模式,参见 SERVER-19758。
比如下面我们查看 db.orders.find({"o_custkey" : 1439371}) 命令运行的运行详细的信息可以如下执行:
db.orders.find({"o_custkey" : 1439371}).explain('executionStats')
[
{
'queryPlanner': {
'plannerVersion': 1,
'namespace': "mongo_test.orders",
'indexFilterSet': false,
'parsedQuery': {
'o_custkey': {
'$eq': 1439371
}
},
'winningPlan': {
'stage': "FETCH",
'inputStage': {
'stage': "IXSCAN",
'keyPattern': {
'o_custkey': 1
},
'indexName': "o_custkey_1",
'isMultiKey': false,
'multiKeyPaths': {
'o_custkey': [
]
},
'isUnique': false,
'isSparse': false,
'isPartial': false,
'indexVersion': 2,
'direction': "forward",
'indexBounds': {
'o_custkey': [
"[1439371, 1439371]"
]
}
}
},
'rejectedPlans': [
]
},
'executionStats': {
'executionSuccess': true,
'nReturned': 31,
'executionTimeMillis': 1,
'totalKeysExamined': 31,
'totalDocsExamined': 31,
'executionStages': {
'stage': "FETCH",
'nReturned': 31,
'executionTimeMillisEstimate': 0,
'works': 32,
'advanced': 31,
'needTime': 0,
'needYield': 0,
'saveState': 0,
'restoreState': 0,
'isEOF': 1,
'docsExamined': 31,
'alreadyHasObj': 0,
'inputStage': {
'stage': "IXSCAN",
'nReturned': 31,
'executionTimeMillisEstimate': 0,
'works': 32,
'advanced': 31,
'needTime': 0,
'needYield': 0,
'saveState': 0,
'restoreState': 0,
'isEOF': 1,
'keyPattern': {
'o_custkey': 1
},
'indexName': "o_custkey_1",
'isMultiKey': false,
'multiKeyPaths': {
'o_custkey': [
]
},
'isUnique': false,
'isSparse': false,
'isPartial': false,
'indexVersion': 2,
'direction': "forward",
'indexBounds': {
'o_custkey': [
"[1439371, 1439371]"
]
},
'keysExamined': 31,
'seeks': 1,
'dupsTested': 0,
'dupsDropped': 0
}
}
},
'ok': 1.0
}
]
从上面输出可以看出,executionStats 模式下可以看到详细的执行计划,比如数据返回多少行(nReturned),查询运行时间(executionTimeMillis)等信息。如果想看到 aggregate 命令相关的执行计划,也是可以使用这个命令的,具体如下:
db.orders.explain('executionStats').aggregate([
{ $match: { o_orderdate : { $gt: "1996-06-20" } } },
{ $group: { _id: "$o_custkey", total: { $sum: "$o_totalprice" } } },
{ $sort: { total: -1 } },
{ $limit: 10 }
],
{ cursor:{} })
[
{
'stages': [
{
'$cursor': {
'query': {
'o_orderdate': {
'$gt': "1996-06-20"
}
},
'fields': {
'o_custkey': 1,
'o_totalprice': 1,
'_id': 0
},
'queryPlanner': {
'plannerVersion': 1,
'namespace': "mongo_test.orders",
'indexFilterSet': false,
'parsedQuery': {
'o_orderdate': {
'$gt': "1996-06-20"
}
},
'queryHash': "52B420E9",
'planCacheKey': "487D9A58",
'winningPlan': {
'stage': "PROJECTION_COVERED",
'transformBy': {
'o_custkey': 1,
'o_totalprice': 1,
'_id': 0
},
'inputStage': {
'stage': "IXSCAN",
'keyPattern': {
'o_orderdate': 1,
'o_custkey': 1,
'o_totalprice': 1,
'o_orderkey': 1
},
'indexName': "o_orderdate_1_o_custkey_1_o_totalprice_1_o_orderkey_1",
'isMultiKey': false,
'multiKeyPaths': {
'o_orderdate': [
],
'o_custkey': [
],
'o_totalprice': [
],
'o_orderkey': [
]
},
'isUnique': false,
'isSparse': false,
'isPartial': false,
'indexVersion': 2,
'direction': "forward",
'indexBounds': {
'o_orderdate': [
"(\"1996-06-20\", {})"
],
'o_custkey': [
"[MinKey, MaxKey]"
],
'o_totalprice': [
"[MinKey, MaxKey]"
],
'o_orderkey': [
"[MinKey, MaxKey]"
]
}
}
},
'rejectedPlans': [
]
},
'executionStats': {
'executionSuccess': true,
'nReturned': 4818876,
'executionTimeMillis': 13264,
'totalKeysExamined': 4818876,
'totalDocsExamined': 0,
'executionStages': {
'stage': "PROJECTION_COVERED",
'nReturned': 4818876,
'executionTimeMillisEstimate': 237,
'works': 4818877,
'advanced': 4818876,
'needTime': 0,
'needYield': 0,
'saveState': 38011,
'restoreState': 38011,
'isEOF': 1,
'transformBy': {
'o_custkey': 1,
'o_totalprice': 1,
'_id': 0
},
'inputStage': {
'stage': "IXSCAN",
'nReturned': 4818876,
'executionTimeMillisEstimate': 177,
'works': 4818877,
'advanced': 4818876,
'needTime': 0,
'needYield': 0,
'saveState': 38011,
'restoreState': 38011,
'isEOF': 1,
'keyPattern': {
'o_orderdate': 1,
'o_custkey': 1,
'o_totalprice': 1,
'o_orderkey': 1
},
'indexName': "o_orderdate_1_o_custkey_1_o_totalprice_1_o_orderkey_1",
'isMultiKey': false,
'multiKeyPaths': {
'o_orderdate': [
],
'o_custkey': [
],
'o_totalprice': [
],
'o_orderkey': [
]
},
'isUnique': false,
'isSparse': false,
'isPartial': false,
'indexVersion': 2,
'direction': "forward",
'indexBounds': {
'o_orderdate': [
"(\"1996-06-20\", {})"
],
'o_custkey': [
"[MinKey, MaxKey]"
],
'o_totalprice': [
"[MinKey, MaxKey]"
],
'o_orderkey': [
"[MinKey, MaxKey]"
]
},
'keysExamined': 4818876,
'seeks': 1,
'dupsTested': 0,
'dupsDropped': 0
}
}
}
}
},
{
'$group': {
'_id': "$o_custkey",
'total': {
'$sum': "$o_totalprice"
}
}
},
{
'$sort': {
'sortKey': {
'total': -1
},
'limit': NumberLong(10)
}
}
],
'ok': 1.0
}
]
注意,如果对于的 collection 数据比较多,explain 的 executionStats 模式下运行时间可能会比较长。
本博客文章除特别声明,全部都是原创!原创文章版权归过往记忆大数据(过往记忆)所有,未经许可不得转载。
本文链接: 【MongoDB 查看某个命令的运行统计信息】(https://www.iteblog.com/archives/9967.html)


