在MongoDB中,唯一索引对分片键施加的实际限制是什么?(In MongoDB, what are the actual limitations imposed by unique indexes

编程入门 行业动态 更新时间:2024-10-13 08:21:16
在MongoDB中,唯一索引对分片键施加的实际限制是什么?(In MongoDB, what are the actual limitations imposed by unique indexes on shard keys?)

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.

更多推荐

本文发布于:2023-07-15 07:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1111395.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   分片   limitations   actual   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!