ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Menu Docs

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.

Diagram of the shard key value space segmented into smaller ranges or chunks.

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.

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:

Diagram of good shard key distribution
clique para ampliar

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

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:

    • 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.