Mongo Docs阅读:
除非唯一字段位于分片键中,否则分片系统无法强制实施群集范围的唯一索引。
从这里: http : //docs.mongodb.org/manual/core/sharding-shard-key/
如果分片键应该是唯一索引,或者它可以是唯一索引的前缀,那么对我来说这是相当模糊的。
我找到了很多关于这个特定主题的参考资料,但遗憾的是,我找不到一个好的“DO和DO NOT”示例。
总而言之,我的问题将出现在以下示例中:给定一个Mongo集合,在字段{a,b,c}上具有唯一索引,以下哪个分片键是正确的:
A. {a} B. {a,b,c} C. {a,b,c,d} D. {a,b,d}?
非常感谢。
Mongo Docs read:
sharded systems cannot enforce cluster-wide unique indexes unless the unique field is in the shard key.
from here: http://docs.mongodb.org/manual/core/sharding-shard-key/
Still, it is rather vague to me if the shard key should be exactly the unique index or it can be a prefix of the unique index.
I found a lot of reference on this particular topic, but, unfortunately, I couldn't find a good "DO and DON'T" example.
To summarize, my question would reside in the following example: given a Mongo collection with a unique index on fields {a,b,c}, which of the following shard keys are right:
A. {a} B. {a,b,c} C. {a,b,c,d} D. {a,b,d} ?
Thanks a lot.
最满意答案
这种限制的原因是分片必须能够检查重复项而不必与其他分片通信。
这意味着必须清楚它所残留的碎片的每个可能值。 碎片只能确定当碰撞文档也存储在自身时,值是唯一的。
这意味着当分片键只是索引的一部分时是可以的,但是当索引只是分片键的一部分时则不行。
对于您的示例,分片键A和B可以工作,但C和D不会 。 当场景C或D中的分片会收到字段a,b和c与其所具有的任何内容不匹配的文档时,仍然可能存在a,b和c上具有相同值的文档但是d的值不同在另一个碎片上。
The reason for this limitation is that it must be possible for the shards to check for duplicates without having to communicate with the other shards.
That means it must be clear for every possible value of the index on which shard it residues. A shard can only be certain that a value is unique when a colliding document would also be stored on itself.
That means that it is OK when the shard-key is only a part of the index, but not when the index is only a part of the shard-key.
For your examples, the shard-keys A and B would work, but C and D would not. When a shard in scenario C or D would receive a document where the fields a, b and c don't match anything it has, there could still be a document with the same values on a, b and c but a different value of d on another shard.
更多推荐
发布评论