多个分片键有助于提高 mongodb 的性能吗?
由于分片数据库使用分片键来分割块并路由查询,所以我认为更多的分片键可能有助于进行更多有针对性的查询
我尝试像这样指定多个键
db.runCommand( { shardcollection : "test.users" , key : {_id:1, email : 1 ,address:1}
,但我不知道它是否有效以及这样做的缺点是什么
Since sharding database use shard key to split chunk AND route queries, so I think maybe more shard key can helps to make more queries targeted
I tried to specify multiple keys like this
db.runCommand( { shardcollection : "test.users" , key : {_id:1, email : 1 ,address:1}
but I have no idea if it works and what the downsides of doing this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里需要明确的是,您只能拥有一个片键。所以你不能有多个分片键。
但是,您建议使用 复合索引 作为分片键。这是可以做到的,但有一些限制。
例如,
_id
、email
和address
的组合必须是唯一的。选择分片键的文档。还有其他一些考虑因素,我无法在此列出。请参阅该文件。
To be clear here, you can only have one shard key. So you cannot have multiple shard keys.
However, you are suggesting a compound index as the shard key. This can be done, but there are some limitations.
For example the combination of
_id
,email
andaddress
must be unique.Documents for choosing a shard key. There are several more considerations that I cannot list here. Please see that document.
基于以下条件选择分片键:
coarseLocality 是您想要的数据的任何位置,
搜索是对数据的常见搜索。
您必须在分片所依据的键上有一个索引,因此如果您选择随机值
如果您不通过该键进行查询,那么您基本上是在浪费索引。每个附加索引
使写入速度变慢,因此保持索引数量尽可能少很重要。
所以,增加片键组合并没有多大帮助。
摘自 Kristina Chodrow 的书《Scaling MongoDB》。
Selection of shard key based on :
coarseLocality is whatever locality you want for your data,
search is a common search on your data.
You must have an index on the key you shard by, so if you choose a randomly-valued
key that you don’t query by, you’re basically wasting an index. Every additional index
makes writes slower, so it’s important to keep the number of indexes as low as possible.
So,increasing shard key combination doesn't help much.
Extract taken from Kristina Chodrow's book "Scaling MongoDB".