MongoDB 分片collection 唯一索引(_id键)的误区
以下是MongoDB官方文档中关于唯一索引的解释
Unique Indexes
MongoDB supports unique indexes, which guarantee that no documents are inserted whose values for the indexed keys match those of an existing document. To create an index that guarantees that no two documents have the same values for both firstname and lastname you would do:
db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
以上解释针对未分片的表和同一分片内的数据有效,在不同分片内唯一索引是无效的。
比如:_id做为分片键值,建有key的唯一索引,collection1 分为两片shard1,shard2
经测试会存在以下情况:
在shard1中存在记录: {"_id" : 100, "key" : NumberLong("305521725191") .....}
在shard2中存在记录: {"_id" : 1000000, "key" : NumberLong("305521725191") ........}
根据collection1的唯一索引key查找,将返回两条数据。
在设计分片键值时需要考虑以上问题!
页:
[1]