範囲ベースのシャーディング
Range-based sharding involves dividing data into contiguous ranges determined by the shard key values. In this model, documents with "close" shard key values are likely to be in the same chunk or shard. This allows for efficient queries where reads target documents within a contiguous range. However, both read and write performance may decrease with poor shard key selection. See Shard Key Selection.
Range-based sharding is the default sharding methodology if no other options such as those required for ハッシュされたシャーディング or zones are configured.
Shard Key Selection
Ranged sharding is most efficient when the shard key displays the following traits:
Large シャードキー濃度
低 シャードキー頻度
Non-単調に変化するシャードキー
The following image illustrates a sharded cluster using the field X
as the
shard key. If the values for X
have a large range, low frequency, and
change at a non-monotonic rate, the distribution of inserts may look similar
to the following:
コレクションのシャーディング
Use the sh.shardCollection()
method, specifying the full namespace
of the collection and the target index or compound index
to use as the shard key.
sh.shardCollection( "database.collection", { <shard key> } )
重要
MongoDB 5.0 以降、ドキュメントのシャードキーを変更することでコレクションの再シャーディングが可能です。
既存のシャードキーにサフィックス フィールドを追加することで、シャードキーを調整できます。
入力済みのコレクションのシャーディング
If you shard a populated collection, only one chunk is created initially. The balancer then migrates ranges from that chunk if necessary according to the configured range size.
空のコレクションのシャーディング
If you shard an empty collection:
With no zones and zone ranges specified for the empty or non-existing collection:
シャーディング操作では、シャードキー値の全範囲をカバーする空のチャンクが 1 つ作成されます。
After the initial chunk creation, the balancer migrates the initial chunk across the shards as appropriate as well as manages the chunk distribution going forward.
With zones and zone ranges specified for the empty or a non-existing collections:
シャーディング操作では、定義されたゾーン範囲の空のチャンクと、シャードキー値の全範囲をカバーする追加のチャンクが作成され、ゾーン範囲に基づいて初期チャンク分散が実行されます。このようにチャンクを最初に作成して分散することで、ゾーン シャーディングの設定を迅速に行うことができます。
初期分散後、バランサーは今後のチャンク分散を管理します。
以下も参照してください。
To learn how to deploy a sharded cluster and implement ranged sharding, see 自己管理型シャーディングされたクラスターの配置.