Database Manual / Reference / Database Commands / Sharding

configureQueryAnalyzer (database command数据库命令)

Definition定义

configureQueryAnalyzer

New in version 7.0.在版本7.0中新增。

Configures query sampling for a collection on a replica set or sharded cluster. Sampled queries provide information to analyzeShardKey to calculate metrics about read and write distribution of a shard key.为副本集或分片群集上的集合配置查询采样。采样查询提供信息来analyzeShardKey,以计算分片键的读写分布指标。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.adminCommand(
{
configureQueryAnalyzer: <string>,
mode: <string>,
samplesPerSecond: <double>
}
)

Command Fields命令字段

configureQueryAnalyzer has the following fields:具有以下字段:

Field字段Type类型Necessity必要性Description描述
configureQueryAnalyzerstring字符串Required必需namespace of the collection to configure for query sampling.要为查询采样配置的集合的命名空间
modestring字符串Required必需Mode the query analyzer runs in. Must be set to either "full" or "off".查询分析器运行的模式。必须设置为"full""off"
samplesPerSeconddouble双精度浮点数Optional可选

Number of samples per second.每秒采样数。

  • When mode is set to "full", samplesPerSecond must be set between 0 and 50.mode设置为"full"时,samplesPerSecond必须设置在050之间。
  • When mode is set to "off", the server ignores samplesPerSecond.当模式设置为mode时,服务器忽略samplesPerSecond

For details, see samplesPerSecond Upper Limit.

Access Control访问控制

configureQueryAnalyzer requires one of the following roles:需要以下角色之一:

  • dbAdmin role against the database that contains the collection being analyzed针对包含正在分析的集合的数据库的dbAdmin角色
  • clusterManager role against the cluster针对集群的clusterManager角色

Behavior行为

Consider the following behavior when running configureQueryAnalyzer:运行configureQueryAnalyzer时,请考虑以下行为:

Dropped Collections and Renamed Collections删除集合和重命名集合

Query sampling is disabled automatically when the collection is dropped or renamed. If you want to sample queries after a collection is recreated or renamed, you must reconfigure query sampling.删除或重命名集合时,查询采样会自动禁用。如果要在重新创建或重命名集合后对查询进行采样,则必须重新配置查询采样。

samplesPerSeconds Upper Limit上限

The upper limit for samplesPerSecond is 50. A higher rate causes the sampled queries to fill up 10GB of disk space in less than four days.每秒samplesPerSecond50。更高的速率会导致采样查询在不到四天的时间内填满10GB的磁盘空间。

This table shows the estimated disk usage for each sample rate and duration combination:此表显示了每种采样率和持续时间组合的估计磁盘使用情况:

Average Sampled Query Size平均采样查询大小 (kB)samplesPerSecondSampling Duration采样持续时间 (Days)Number of Sampled Queries采样查询数Total Size of Sampled Queries采样查询的总大小 (GB)
0.50.1760,4800.03024
0.51076,048,0003.024
0.550730,240,00015.12
10005014,320,0004320
16,0005014,320,00069,120

queryAnalysisSampleExpirationSecs

Sampled queries are stored in an internal collection that has a TTL index with expireAfterSeconds. To configure expireAfterSeconds, use the queryAnalysisSampleExpirationSecs server parameter. Sampled queries are automatically deleted after queryAnalysisSampleExpirationSecs.采样查询存储在一个内部集合中,该集合的TTL索引为expireAfterSeconds。要配置expireAfterSeconds,请使用queryAnalysisSampleExpirationSecs服务器参数。在queryAnalysisSampleExpirationSecs之后,采样查询会自动删除。

Query Sampling Progress查询采样进度

To monitor the query sampling process, use the $currentOp stage. For an example, see Sampled Queries.要监视查询采样过程,请使用$currentOp阶段。有关示例,请参阅采样查询

View Sampled Queries查看示例查询

To see sampled queries for all collections or a specific collection, use the $listSampledQueries aggregation stage.要查看所有集合或特定集合的采样查询,请使用$listSampledQueries聚合阶段。

To see the count of every command type captured by the query analyzer, use:要查看查询分析器捕获的每种命令类型的计数,请使用:

db.getSiblingDB("admin").aggregate( [
{ "$listSampledQueries": { ns: "<db.collectionName>" } },
{
"$group": {
"_id": {
ns: "$ns",
cmdName: "$cmdName"
},
count: { "$sum": 1 }
}
},
{
"$project": {
"_id.ns": 1,
"_id.cmdName": 1,
"count": 1 // Explicitly include fields to output
}
},
{ "$sort": { "_id.ns": 1 } }
] );
[{ count: 10, cmdName: 'aggregate' },
{ count: 51, cmdName: 'delete' },
{ count: 25, cmdName: 'distinct' },
{ count: 100, cmdName: 'find' },
{ count: 24, cmdName: 'findAndModify' },
{ count: 7, cmdName: 'update' }]

When you have enough samples, you can disable the query analyzer.当您有足够的样本时,可以禁用查询分析器。

Limitations局限性

  • You cannot run configureQueryAnalyzer on Atlas flex clusters.您无法在Atlas flex集群上运行configureQueryAnalyzer
  • You cannot run configureQueryAnalyzer on standalone deployments.您无法在独立部署上运行configureQueryAnalyzer
  • You cannot run configureQueryAnalyzer directly against a --shardsvr replica set. 您不能直接对--shardsvr副本集运行configureQueryAnalyzerWhen running on a sharded cluster, configureQueryAnalyzer must run against a mongos.在分片集群上运行时,configureQueryAnalyzer必须针对mongos运行。
  • You cannot run configureQueryAnalyzer against time series collections.您无法对时间序列集合运行configureQueryAnalyzer
  • You cannot run configureQueryAnalyzer against collections with Queryable Encryption.您无法对具有可查询加密的集合运行configureQueryAnalyzer

Output输出

configureQueryAnalyzer returns a document containing fields that describe the old configuration, if one exists, and fields describing the new configuration.返回一个文档,其中包含描述旧配置的字段(如果存在)和描述新配置的字段。

  • oldConfiguration, if it exists, contains fields describing the old configuration.如果存在,则包含描述旧配置的字段。
  • newConfiguration contains fields describing the new configuration.包含描述新配置的字段。

configureQueryAnalyzer returns a document similar to the following:返回一个类似于以下内容的文档:

{
ok: 1,
oldConfiguration: {
mode: ...,
samplesPerSecond: ...
}
newConfiguration: {
...
}
}

Examples示例

Enable Query Sampling启用查询采样

To enable query sampling on the test.students collection at a rate of five samples per second, use the following command:要以每秒五个样本的速度对test.students集合启用查询采样,请使用以下命令:

db.adminCommand(
{
configureQueryAnalyzer: "test.students",
mode: "full",
samplesPerSecond: 5
}
)

Disable Query Sampling禁用查询采样

To disable query sampling on the test.students collection, use the following command:要禁用test.students集合上的查询采样,请使用以下命令:

db.adminCommand(
{
configureQueryAnalyzer: "test.students",
mode: "off"
}
)

Learn More了解更多