Docs 菜单

Docs 主页启动和管理 MongoDBMongoDB Atlas

检索查询计划和执行统计信息

在此页面上

  • 语法
  • 详细程度
  • Explain 响应
  • 限制
  • 举例
  • queryPlanner
  • executionStats

重要

您可以在 mongosh中使用 explain运行 Atlas Search 查询。但是,此功能不适用于 MongoDB Compass。

在使用 explain 方法运行查询时,Atlas Search 查询返回有关 $search 查询计划和执行统计信息的信息。在您使用 explain 运行查询时,Atlas Search 返回一个 BSON 文档,其中包含有关如何运行查询的信息。

提示

另请参阅:

db.<myCollection>.explain("<verbosity>").aggregate([
{
$search: {
"<operator>": {
"<operator-options>"
}
}
}
])

详细程度模式控制 explain 行为和返回的信息量。值可以是以下值之一:

queryPlanner (默认)
有关查询计划的信息。不包括 stats 字段,其中包含查询的执行统计信息。
有关查询计划的信息,包括 stats 字段,其中包含查询的执行统计信息。
有关查询计划的信息,包括 stats 字段,其中包含查询的执行统计信息。

提示

另请参阅:

explain 响应是一个 BSON 文档,其中的键和值描述了查询的执行统计数据。结果集中的 explain 文档包含以下字段:

选项
类型
必要性
用途
path
字符串
Optional
操作符的路径,前提是它不是根目录。
type
字符串
必需
Atlas Search 操作符创建的 Lucene 查询的名称。有关更多信息,请参阅 Lucene 查询结构化摘要
analyzer
字符串
Optional
用于查询的 Atlas Search 分析器
args
文档
必需
Lucene 查询信息。有关更多信息,请参阅 Lucene 查询结构化摘要
stats
文档
Optional
explainexecutionStatsallPlansExecution 详细程度模式运行时的查询 explain 时间细分信息

您无法使用 explain 运行分面查询。

以下示例使用 sample_mflix 数据库中的 movies 集合。

提示

如果您已经加载示例数据集,请按照 Atlas Search 入门教程,创建索引定义并运行 Atlas Search 查询。

以下示例使用文本操作符以queryPlanner详细模式查询title字段。

db.movies.explain("queryPlanner").aggregate([
{
$search: {
"text": {
"path": "title",
"query": "yark",
"fuzzy": {
"maxEdits": 1,
"maxExpansions": 100,
}
}
}
}
])

查询返回以下结果。要了解有关explain响应元素的详情,请参阅解释响应。

{
"stages" : [
{
"$_internalSearchMongotRemote" : {
"mongotQuery" : {
"text" : {
"path" : "title",
"query" : "yark",
"fuzzy" : {
"maxEdits" : 1,
"maxExpansions" : 100
}
}
},
"explain" : {
"type" : "BooleanQuery",
"args" : {
"must" : [ ],
"mustNot" : [ ],
"should" : [
{
"type" : "BoostQuery",
"args" : {
"query" : {
"type" : "TermQuery",
"args" : {
"path" : "title",
"value" : "ark"
}
},
"boost" : 0.6666666269302368
}
},
{
"type" : "BoostQuery",
"args" : {
"query" : {
"type" : "TermQuery",
"args" : {
"path" : "title",
"value" : "yard"
}
},
"boost" : 0.75
}
},
{
"type" : "BoostQuery",
"args" : {
"query" : {
"type" : "TermQuery",
"args" : {
"path" : "title",
"value" : "mark"
}
},
"boost" : 0.75
}
},
{
"type" : "BoostQuery",
"args" : {
"query" : {
"type" : "TermQuery",
"args" : {
"path" : "title",
"value" : "park"
}
},
"boost" : 0.75
}
},
{
"type" : "BoostQuery",
"args" : {
"query" : {
"type" : "TermQuery",
"args" : {
"path" : "title",
"value" : "dark"
}
},
"boost" : 0.75
}
},
{
"type" : "BoostQuery",
"args" : {
"query" : {
"type" : "TermQuery",
"args" : {
"path" : "title",
"value" : "york"
}
},
"boost" : 0.75
}
}
],
"filter" : [ ],
"minimumShouldMatch" : 0
}
}
}
},
{
"$_internalSearchIdLookup" : { }
}
],
"serverInfo" : {
"host" : "atlas-example-shard-00-01.mongodb.net",
"port" : 27017,
"version" : "4.4.3",
"gitVersion" : "913d6b62acfbb344dde1b116f4161360acd8fd13"
},
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1612457287, 1),
"signature" : {
"hash" : BinData(0,"kzn7hY7NOduVIqcfx+40ENKbMKQ="),
"keyId" : NumberLong("1234567890123456789")
}
},
"operationTime" : Timestamp(1612457287, 1)
}

对于在管道中指定$limit阶段的查询, explain结果包括mongotDocsRequested指标,该指标显示mongodmongot请求的文档数。

例子

{
"mongotQuery": {},
"explain": {},
"limit": <int>,
"sortSpec": {},
"mongotDocsRequested": <int>,
}

以下示例使用autocomplete操作符以executionStats详细模式查询title字段。

db.movies.explain("executionStats").aggregate([
{
"$search": {
"autocomplete": {
"path": "title",
"query": "pre",
"fuzzy": {
"maxEdits": 1,
"prefixLength": 1,
"maxExpansions": 256
}
}
}
}
])

查询返回以下结果。要了解有关explain响应元素的详情,请参阅解释响应。

{
"stages" : [
{
"$_internalSearchMongotRemote" : {
"mongotQuery" : {
"autocomplete" : {
"path" : "title",
"query" : "pre",
"fuzzy" : {
"maxEdits" : 1,
"prefixLength" : 1,
"maxExpansions" : 256
}
}
},
"explain" : {
"type" : "MultiTermQueryConstantScoreWrapper",
"args" : {
"queries" : [
{
"type" : "DefaultQuery",
"args" : {
"queryType" : "AutomatonQuery"
},
"stats" : {
"context" : {
"millisElapsed" : NumberDouble(0)
},
"match" : {
"millisElapsed" : NumberDouble(0)
},
"score" : {
"millisElapsed" : NumberDouble(0)
}
}
}
]
},
"stats" : {
"context" : {
"millisElapsed" : NumberDouble(0.816418),
"invocationCounts" : {
"createWeight" : NumberLong(1),
"createScorer" : NumberLong(2)
}
},
"match" : {
"millisElapsed" : NumberDouble(3.849778),
"invocationCounts" : {
"nextDoc" : NumberLong(656)
}
},
"score" : {
"millisElapsed" : NumberDouble(0.035349),
"invocationCounts" : {
"score" : NumberLong(655)
}
}
}
}
},
"nReturned" : NumberLong(0),
"executionTimeMillisEstimate" : NumberLong(20)
},
{
"$_internalSearchIdLookup" : { },
"nReturned" : NumberLong(0),
"executionTimeMillisEstimate" : NumberLong(20)
}
],
"serverInfo" : {
"host" : "atlas-example-shard-00-01.mongodb.net",
"port" : 27017,
"version" : "4.4.3",
"gitVersion" : "913d6b62acfbb344dde1b116f4161360acd8fd13"
},
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1612454116, 1),
"signature" : {
"hash" : BinData(0,"OY+SMPmdK//g6rFZkvSCQr3c3hM="),
"keyId" : NumberLong("1234567890123456789")
}
},
"operationTime" : Timestamp(1612454116, 1)
}
← 运行 Atlas Search 查询