Fragmentação com intervalos
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 estilhaço. 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 Seleção de chave de shard.
Range-based sharding is the default sharding methodology if no other options such as those required for Fragmentação em hash or zones are configured.
Seleção de chave de shard
Ranged sharding is most efficient when the shard key displays the following traits:
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:
Fragmentar uma Coleção
Use the sh.shardCollection()
method, specifying the full namespace
of the collection and the target index or Índice Composto
to use as the chave de fragmento.
sh.shardCollection( "database.collection", { <shard key> } )
Importante
A partir do MongoDB 5.0, você pode reestruturar uma coleção alterando a chave de fragmento de uma coleção.
Você pode refinar uma chave de fragmento adicionando um ou mais campos de sufixo à chave de fragmento existente.
Fragmentar uma collection preenchida
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.
Fragmentar uma collection vazia
If you shard an empty collection:
With no zones and zone ranges specified for the empty or non-existing collection:
A operação de fragmentação cria um único chunk vazio para cobrir todo o intervalo dos valores de chave de shard.
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:
A operação de fragmentação cria chunks vazios para as faixas de zonas definidas e chunks adicionais para cobrir toda a faixa de valores das chaves de shard e executa uma distribuição inicial de chunks com base nas faixas de zonas. A criação e distribuição inicial de chunks agiliza a configuração da fragmentação por zonas.
Após a distribuição inicial, o balancer passa a gerenciar a distribuição de chunks.
Veja também:
To learn how to deploy a sharded cluster and implement ranged sharding, see Implemente um cluster fragmentado autogerenciado.