定义
analyzeShardKey7.0版本新增。
计算用于评估未分片或分分片的集合的分片分片键的指标。 指标基于采样的查询。 您可以使用
configureQueryAnalyzer在集合上配置查询采样。
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
注意
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
analyzeShardKey 采用以下语法:
db.collection.analyzeShardKey( <shardKey>, { keyCharacteristics: <bool>, readWriteDistribution: <bool>, sampleRate: <double>, sampleSize: <int> } )
命令字段
字段 | 类型 | 必要性 | 说明 |
|---|---|---|---|
| 文档 | 必需 | 要分析的分片键。这可以是未分片集合或分片集合的候选分片键,也可以是分片集合的当前分片键。 没有默认值。 |
| 布尔 | Optional | Whether or not the metrics about the characteristics of the shard key are calculated. For details, see keyCharacteristics. 默认值为 |
| 布尔 | Optional | Whether or not the metrics about the read and write distribution are calculated. For details, see readWriteDistribution. 默认值为 To return read and write distribution metrics for a collection using |
| double | Optional | 计算有关分片分片键特征的指标时,集合中要进行示例的文档所占的比例。 如果设立 必须大于 没有默认值。 |
| 整型 | Optional | 计算分片密钥特征相关指标时要采样的文档数量。如果设置 如果未指定,且未指定 |
行为
analyzeShardKey 根据您在运行该方法时指定的 keyCharacteristic 和 readWriteDistribution 值,命令返回不同的指标。
关于分片键特征的指标
keyCharacteristic 由有关分片键的关联基数 ( cardinality )、频率(Frequency) 和单调性(monotonicity) 的指标组成。 仅当keyCharacteristics为 true 时才会返回这些指标。
The metrics are calculated when analyzeShardKey command is run based on documents sampled from the collection. The calculation requires the shard key to have a supporting index. If there is no supporting index, no metrics are returned.
您可以使用 sampleRate 和 sampleSize 字段配置采样。两者都是可选的,但只能指定一个。当 sampleRate 和 sampleSize 均未指定时, MongoDB将使用analyzeShardKeyCharacteristicsDefaultSampleSize 参数的值,该值的默认值为 10 百万。
要根据collection中的所有文档计算指标,请将sampleRate设置为1 。
有关读取和写入分布的指标
readWriteDistribution 包含有关查询路由模式和分片键范围热度的指标。 这些指标基于采样的查询。
要为集合配置查询采样,请使用 configureQueryAnalyzer 命令。读取和写入分布指标仅在当 readWriteDistribution 为 true 时返回。指标在 analyzeShardKey 命令运行时计算,并使用示例读写入查询。如果没有示例查询,就不会返回读取和写入分布指标。
如果没有采样的读取查询,该命令将返回
writeDistribution但忽略readDistribution。如果没有采样的写入查询,该命令将返回
readDistribution但忽略writeDistribution。
To return read and write distribution metrics for a collection using analyzeShardKey, you must configure the query analyzer to sample the queries run on the collection. Otherwise, analyzeShardKey returns the read and write distribution metrics as 0 values. To configure the query analyzer, see configureQueryAnalyzer (database command).
keyCharacteristics 值 | readWriteDistribution 值 | 返回的结果 |
|---|---|---|
|
|
|
|
|
|
|
|
|
非阻塞行为
analyzeShardKey 命令不会阻止对集合的读取或写入。
查询采样
有关读取和写入分布的指标质量取决于进行查询采样时工作负载的代表性。对于某些应用程序,返回代表性指标可能需要将查询采样保持打开状态几天。
支持索引
analyzeShardKey 命令所需的支持索引与shardCollection 命令所需的支持索引不同。
下表显示了 analyzeShardKey 命令和 shardCollection 的相同分片键支持的索引:
命令 | 片键 | 支持索引 |
|---|---|---|
|
|
|
|
|
|
这允许您分析可能尚无分片所需的支持索引的分片键。
analyzeShardKey 命令和 shardCollection 均具有以下索引要求:
要创建支持索引,请使用db.collection.createIndex()方法。
读取偏好
为了最大程度地降低性能,请使用 secondary 或 secondaryPreferred 读取偏好(read preference)运行 analyzeShardKey 命令。在分片集群(cluster)上,如果未指定, mongos 会自动将读取偏好(read preference)设置为 secondaryPreferred。
限制
You cannot run
analyzeShardKeycommand on Atlas flex clusters.您无法在独立运行的实例上运行
analyzeShardKey命令。您不能直接对
--shardsvr副本集运行analyzeShardKey命令。在分片集群上运行时,analyzeShardKey命令必须对mongos运行。您无法对 时间序列 集合运行
analyzeShardKey命令。您无法对具有 Queryable Encryption 的集合运行
analyzeShardKey命令。
访问控制
db.collection.analyzeShardKey 方法需要以下角色之一:
enableSharding针对正在分析的collection的权限操作。clusterManager针对集群的角色。
输出
analyzeShardKey command returns information regarding keyCharacteristics and readWriteDistribution.
keyCharacteristics提供有关分片键的关联基数、频率和单调性的指标。readWriteDistribution提供有关查询路由模式和分片键范围热度的指标。
keyCharacteristics
这是当keyCharacteristics设置为true时返回的keyCharacteristics文档的结构:
{ keyCharacteristics: { numDocsTotal: <integer>, numOrphanDocs: <integer>, avgDocSizeBytes: <integer>, numDocsSampled: <integer>, isUnique: <bool>, numDistinctValues: <integer>, mostCommonValues: [ { value: <shardkeyValue>, frequency: <integer> }, ... ], monotonicity: { recordIdCorrelationCoefficient: <double>, type: "monotonic"|"not monotonic"|"unknown", } } }
字段 | 类型 | 说明 | 使用 |
|---|---|---|---|
| 整型 | collection中的文档数量。 | |
| 整型 | 孤立文档的数量。 | 出于性能原因,不会将孤立文档排除在指标计算之外。 如果 |
| 整型 | collection中的文档的平均大小(以字节为单位)。 | 如果 |
| 整型 | 采样文档的数量。 | |
| 整型 | 不同分片键值的数量。 | 请选择具有较大 |
| 布尔 | 指示分片键是否唯一。 仅当分片键存在唯一索引时,才会将其设置为 | 如果分片键是唯一的,则不同值的数量等于文档的数量。 |
| 文档数组 | 最常用分片键值的值和 | 分片键值的频率是指数据块中包含该值的最小文档数。如果频率较大,则该数据块可能会成为存储、读取和写入的瓶颈。选择一个分片键,其中每个最常用值的频率相对于 可以通过设置 |
| 文档 | 分片键。 | |
| 整型 | 给定分片键的文档数量。 | 选择一个分片键,其中每个最常用值的频率相对于 |
| double | 仅在单调性已知时设置。 | 当以下任一条件为 true 时,此项设置为
如果collection已Go数据块迁移,则单调性检查可能会返回不正确的结果。数据块迁移会从发送分片中删除文档,然后将其重新插入接收分片中。不保证来自客户端的插入顺序会得到保留。 您可以使用analyzeShardKeyMonotonicity CorrelationCoffiencyThreshold 配置相关系数的阈值。 |
| 字符串 | 可以是以下之一:
| 避免使用类型为 如果按单调递增或递减的分片键对collection进行分片,则文档将插入到拥有 |
readWriteDistribution
这是当readWriteDistribution设置为true时返回的文档的结构:
{ readDistribution: { sampleSize: { total: <integer>, find: <integer>, aggregate: <integer>, count: <integer>, distinct: <integer> }, percentageOfSingleShardReads: <double>, percentageOfMultiShardReads: <double>, percentageOfScatterGatherReads: <double>, numReadsByRange: [ <integer>, ... ] }, writeDistribution: { sampleSize: { total: <integer>, update: <integer>, delete: <integer>, findAndModify: <integer> }, percentageOfSingleShardWrites: <double>, percentageOfMultiShardWrites: <double>, percentageOfScatterGatherWrites: <double>, numWritesByRange: [ <integer>, ... ], percentageOfShardKeyUpdates: <double>, percentageOfSingleWritesWithoutShardKey: <double>, percentageOfMultiWritesWithoutShardKey: <double> } }
To return read and write distribution metrics for a collection using analyzeShardKey, you must configure the query analyzer to sample the queries run on the collection. Otherwise, analyzeShardKey returns the read and write distribution metrics as 0 values. To configure the query analyzer, see configureQueryAnalyzer (database command).
readDistribution 字段
字段 | 类型 | 说明 | 使用 |
|---|---|---|---|
| 整型 | 已采样读取查询的总数。 | |
| 整型 | 已采样的 | |
| 整型 | 已采样的 | |
| 整型 | 已采样的 | |
| 整型 | 已采样的 | |
| double | 无论数据如何分布,针对单个分片的读取百分比。 | |
| double | 针对多个分片的读取百分比。 | 如果数据的分布使得读取的目标值属于单个分片,则此类别包括可能仅针对单个分片的读取。 如果查询对大量数据进行操作,则针对多个分片而不是一个分片可能会由于并行查询执行而减少延迟。 |
| double | 分散-聚集读取的百分比,无论数据如何分布。 | 避免使用该指标具有较高值的分片键。 虽然分散-聚集查询对没有目标数据的分片影响很小,但它们仍然会对性能产生一些影响。 在具有大量分片的集群上,分散-聚集查询的性能明显低于针对单个分片的查询。 |
| 整数数组 | 数字数组,表示从 | 避免使用 选择 可以使用 |
writeDistribution 字段
字段 | 类型 | 说明 | 使用 |
|---|---|---|---|
| 整型 | 已采样写入查询的总数。 | |
| 整型 | 已采样的 | |
| 整型 | 已采样的 | |
| 整型 | 已采样的 | |
| double | 无论数据如何分布,针对单个分片的写入百分比。 | |
| double | 针对多个分片的写入百分比。 | 如果数据的分布使得写入的目标值属于单个分片,则此类别包括可能仅针对单个分片的写入。 |
| double | 无论数据如何分布,分散-聚集写入的百分比。 | 避免使用该指标具有较高值的分片键,因为针对单个分片的写入通常性能更高。 |
| 整数数组 | 数字数组,表示从 | 避免使用 选择 可以使用 |
| double | 更新文档分片分片键值的写入查询的百分比。 | 避免使用 目前仅支持以可重试写入或事务形式进行更新,并且批处理大小限制为 |
| double | 为 | 避免使用该指标具有较高值的分片键。 执行此类写入的成本很高,因为可能涉及运行内部事务。 |
| double | 为 | 避免使用该指标具有较高值的分片键。 |
示例
考虑一个社交媒体应用的简化版本。 我们尝试分片的集合是post集合。
postcollection中的文档具有以下模式:
{ userId: <uuid>, firstName: <string>, lastName: <string>, body: <string>, // the field that can be modified. date: <date>, // the field that can be modified. }
背景信息
该应用有 1500 个用户。
有 30 个姓氏和 45 个名字,其中一些更常见。
其中有 3 名名人用户。
每个用户恰好关注了另外五个用户,并且很有可能关注至少一个名人用户。
示例工作负载
每个用户每天在随机时间发布大约两个帖子。 他们在每个帖子发布后立即编辑一次。
每个用户每六个小时登录一次,以阅读自己的个人资料以及过去 24 小时关注的用户发布的帖子。 他们还会在过去三个小时内随机发布的帖子下进行回复。
对于每个用户,该应用程序都会在午夜删除超过三天的帖子。
工作负载查询模式
此工作负载具有以下查询模式:
find带筛选器的命令{ userId: , firstName: , lastName: }find带筛选器的命令{ $or: [ { userId: , firstName: , lastName: , date: { $gte: } }, ... , { userId: , date: } ] }findAndModify带筛选器{ userId: , firstName: , lastName: , date: }的命令,以更新正文和日期字段。update命令(包含multi: false和筛选器{ userId: , firstName: , lastName: , date: { $gte: , $lt: } }以更新正文和日期字段。delete附带multi: true和筛选器{ userId: , firstName: , lastName: , date: { $lt: } }的命令
以下是 analyzeShardKey 命令为某些候选分片键返回的示例指标,以及从 7 天的工作负载中收集的采样查询。
注意
Before you run analyzeShardKey commands, read the Supporting Indexes section earlier on this page. If you require supporting indexes for the shard key you are analyzing, use the db.collection.createIndex() method to create the indexes.
{ _id: 1 } keyCharacteristics
此示例使用 analyzeShardKey 命令提供 social.post集合上 { _id: 1 }分片键的指标。
以下代码区块使用db.collection.configureQueryAnalyzer() 打开查询采样:
use social db.post.configureQueryAnalyzer( { mode: "full", samplesPerSecond: 5 } )
在 db.collection.configureQueryAnalyzer() 收集查询样本后,以下代码区块使用 analyzeShardKey 命令对 10,000 文档示例并计算结果:
use social db.post.analyzeShardKey( { _id: 1 }, { keyCharacteristics: true, readWriteDistribution: false, sampleSize: 10000 } )
{ lastName: 1 } keyCharacteristics
此analyzeShardKey命令提供social.post集合上{ lastName: 1 }分片键的指标:
use social db.post.analyzeShardKey( { lastName: 1 }, { keyCharacteristics: true, readWriteDistribution: false } )
此示例的输出如下所示:
{ "keyCharacteristics": { "numDocsTotal" : 9039, "avgDocSizeBytes" : 153, "numDocsSampled" : 9039, "isUnique" : false, "numDistinctValues" : 30, "mostCommonValues" : [ { "value" : { "lastName" : "Smith" }, "frequency" : 1013 }, { "value" : { "lastName" : "Johnson" }, "frequency" : 984 }, { "value" : { "lastName" : "Jones" }, "frequency" : 962 }, { "value" : { "lastName" : "Brown" }, "frequency" : 925 }, { "value" : { "lastName" : "Davies" }, "frequency" : 852 } ], "monotonicity" : { "recordIdCorrelationCoefficient" : 0.0771959161, "type" : "not monotonic" }, } }
{ userId: 1 } keyCharacteristics
此analyzeShardKey命令提供social.post集合上{ userId: 1 }分片键的指标:
use social db.post.analyzeShardKey( { userId: 1 }, { keyCharacteristics: true, readWriteDistribution: false } )
此示例的输出如下所示:
{ "keyCharacteristics": { "numDocsTotal" : 9039, "avgDocSizeBytes" : 162, "numDocsSampled" : 9039, "isUnique" : false, "numDistinctValues" : 1495, "mostCommonValues" : [ { "value" : { "userId" : UUID("aadc3943-9402-4072-aae6-ad551359c596") }, "frequency" : 15 }, { "value" : { "userId" : UUID("681abd2b-7a27-490c-b712-e544346f8d07") }, "frequency" : 14 }, { "value" : { "userId" : UUID("714cb722-aa27-420a-8d63-0d5db962390d") }, "frequency" : 14 }, { "value" : { "userId" : UUID("019a4118-b0d3-41d5-9c0a-764338b7e9d1") }, "frequency" : 14 }, { "value" : { "userId" : UUID("b9c9fbea-3c12-41aa-bc69-eb316047a790") }, "frequency" : 14 } ], "monotonicity" : { "recordIdCorrelationCoefficient" : -0.0032039729, "type" : "not monotonic" }, } }
{ userId: 1 } readWriteDistribution
此analyzeShardKey命令提供social.post集合上{ userId: 1 }分片键的指标:
use social db.post.analyzeShardKey( { userId: 1 }, { keyCharacteristics: false, readWriteDistribution: true } )
此示例的输出如下所示:
{ "readDistribution" : { "sampleSize" : { "total" : 61363, "find" : 61363, "aggregate" : 0, "count" : 0, "distinct" : 0 }, "percentageOfSingleShardReads" : 50.0008148233, "percentageOfMultiShardReads" : 49.9991851768, "percentageOfScatterGatherReads" : 0, "numReadsByRange" : [ 688, 775, 737, 776, 652, 671, 1332, 1407, 535, 428, 985, 573, 1496, ... ], }, "writeDistribution" : { "sampleSize" : { "total" : 49638, "update" : 30680, "delete" : 7500, "findAndModify" : 11458 }, "percentageOfSingleShardWrites" : 100, "percentageOfMultiShardWrites" : 0, "percentageOfScatterGatherWrites" : 0, "numWritesByRange" : [ 389, 601, 430, 454, 462, 421, 668, 833, 493, 300, 683, 460, ... ], "percentageOfShardKeyUpdates" : 0, "percentageOfSingleWritesWithoutShardKey" : 0, "percentageOfMultiWritesWithoutShardKey" : 0 } }