Definition定义
configureQueryAnalyzerNew in version 7.0.在版本7.0中新增。Configures query sampling for a collection on a replica set or sharded cluster. Sampled queries provide information to为副本集或分片群集上的集合配置查询采样。采样查询提供信息来analyzeShardKeyto 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:具有以下字段:
configureQueryAnalyzer | |||
mode | "full" or "off"."full"或"off"。 | ||
samplesPerSecond |
For details, see samplesPerSecond Upper Limit. |
Access Control访问控制
configureQueryAnalyzer requires one of the following roles:需要以下角色之一:
针对包含正在分析的集合的数据库的dbAdminrole against the database that contains the collection being analyzeddbAdmin角色针对集群的clusterManagerrole against the clusterclusterManager角色
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.samplesPerSecond为50。更高的速率会导致采样查询在不到四天的时间内填满10GB的磁盘空间。
This table shows the estimated disk usage for each sample rate and duration combination:此表显示了每种采样率和持续时间组合的估计磁盘使用情况:
samplesPerSecond | ||||
|---|---|---|---|---|
| 0.5 | 0.1 | 7 | 60,480 | 0.03024 |
| 0.5 | 10 | 7 | 6,048,000 | 3.024 |
| 0.5 | 50 | 7 | 30,240,000 | 15.12 |
| 1000 | 50 | 1 | 4,320,000 | 4320 |
| 16,000 | 50 | 1 | 4,320,000 | 69,120 |
queryAnalysisSampleExpirationSecs
Sampled queries are stored in an internal collection that has a TTL index with 采样查询存储在一个内部集合中,该集合的TTL索引为expireAfterSeconds. To configure expireAfterSeconds, use the queryAnalysisSampleExpirationSecs server parameter. Sampled queries are automatically deleted after queryAnalysisSampleExpirationSecs.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您无法在Atlas flex集群上运行configureQueryAnalyzeron Atlas flex clusters.configureQueryAnalyzer。You cannot run您无法在独立部署上运行configureQueryAnalyzeron standalone deployments.configureQueryAnalyzer。You cannot run您不能直接对configureQueryAnalyzerdirectly against a--shardsvrreplica set.--shardsvr副本集运行configureQueryAnalyzer。When running on a sharded cluster,在分片集群上运行时,configureQueryAnalyzermust run against amongos.configureQueryAnalyzer必须针对mongos运行。You cannot run您无法对时间序列集合运行configureQueryAnalyzeragainst time series collections.configureQueryAnalyzer。You cannot run您无法对具有可查询加密的集合运行configureQueryAnalyzeragainst 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.如果存在,则包含描述旧配置的字段。newConfigurationcontains 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"
}
)