翻译或纠错本页面

Operational Restrictions in Sharded Clusters

Sharding Operational Restrictions

Operations Unavailable in Sharded Environments

The group does not work with sharding. Use mapReduce or aggregate instead.

3.0 版后已移除: db.eval() is deprecated.

虽然分片是强大而又引人注目的特性,集群所需要的条件提高了部署的复杂程度,因此,只在应用所需要并且满足部署条件的时候使用集群.

在某些情况下,使用分片是 唯一 的解决办法,在以下情况下使用 集群 :

The $isolated update modifier does not work in sharded environments.

$snapshot queries do not work in sharded environments.

The geoSearch command is not supported in sharded environments.

Single Document Modification Operations in Sharded Collections

All updateOne(), removeOne(), and deleteOne() operations for a sharded collection must include the shard key or the _id field in the query specification. updateOne(), removeOne(), and deleteOne() operations without the shard key or the _id field return an error.

Unique Indexes in Sharded Collections

MongoDB does not support unique indexes across shards, except when the unique index contains the full shard key as a prefix of the index. In these situations MongoDB will enforce uniqueness across the full key, not a single field.

如果这些特性在你的系统中都没有出现,使用分片只会增加系统的复杂程度,而不会带来什么好处.

Unique Constraints on Arbitrary Fields for an alternate approach.

Sharding Existing Collection Data Size

部署集群会花费时间和资源,如果你的系统已经或即将达到性能极限,很难在不影响应用性能的情况下部署集群.

重要

因此,如果你觉得你的数据库在未来需要分片, 不要等到 系统负载超限之后才开始操作.

Use the following formulas to calculate the theoretical maximum collection size.

maxSplits = 16777216 (bytes) / <average size of shard key values in bytes>
maxCollectionSize (MB) = maxSplits * (chunkSize / 2)

注解

The maximum BSON document size is 16MB or 16777216 bytes.

All conversions should use base-2 scale, e.g. 1024 kilobytes = 1 megabyte.

If maxCollectionSize is less than or nearly equal to the target collection, increase the chunk size to ensure sucessful initial sharding. If there is doubt as to whether the result of the calculation is too ‘close’ to the target collection size, it is likely better to increase the chunk size.

在某些情况下,你也许需要对一个小的集合开启分片,但在大多数情况下,对小的集合开启分片,带来的复杂程度和开销会使得这种行为得不偿失,除非你想要获得更高的写性能.一般情况下,如果你的数据量较小,一个合理配置的单个MongoDB或者一个复制集在很长时间内都已经足够.

This table illustrates the approximate maximum collection sizes using the formulas described above:

Average Size of Shard Key Values 512 bytes 256 bytes 128 bytes 64 bytes
Maximum Number of Splits 32,768 65,536 131,072 262,144
Max Collection Size (64 MB Chunk Size) 1 TB 2 TB 4 TB 8 TB
Max Collection Size (128 MB Chunk Size) 2 TB 4 TB 8 TB 16 TB
Max Collection Size (256 MB Chunk Size) 4 TB 8 TB 16 TB 32 TB