Skip to Content
MongoDB权威指南(第3版)
book

MongoDB权威指南(第3版)

by Shannon Bradshaw, Eoin Brazil, Kristina Chodorow
November 2021
Intermediate to advanced
410 pages
12h 1m
Chinese
Posts & Telecom Press
Content preview from MongoDB权威指南(第3版)
选择片键
273
可以将此配置想象成每个块都是一个升序文档的栈。每个分片上有多个栈,每个栈都是递
增的,直到块被拆分。一旦某个块被拆分,只有一个新的块会成为热点块:其他块实质上
将“死亡”,不再增长。如果栈均匀分发在各个分片上,则写操作也将均匀分发。
16.4
 片键规则和指导方针
在选择片键之前,有几个实践中的限制需要注意。
确定要分片的键并创建片键会让人想起索引,因为这两个概念是相似的。事实上,通常你
的片键可能就是你最常使用的索引(或者索引的一些变体)。
16.4.1
 片键的限制
片键不能是数组。如果任何键有数组值,那么
sh.shardCollection()
就会失败,并且将数
组插入该字段是不允许的。
文档在插入之后,其片键值可能会被修改,除非片键字段是不可变的
_id
字段。在
MongoDB 4.2
之前的旧版本中,是不可以修改文档的片键值的。
大多数特殊类型的索引不能用作片键。特别是,不能在地理空间索引上进行分片。如前所
述,允许使用哈希索引作为片键。
16.4.2
 片键的基数
无论片键是跳跃的还是稳定增长的,选择值会发生变化的键是很重要的。与索引一样,在
高基数字段上进行分片的性能会更好。如果有一个
"logLevel"
键只有
"DEBUG"
"WARN"
"ERROR"
几个值,则
MongoDB
将无法将数据拆分成
3
个以上的块(因为片键只有
3
个不同的
值)。如果想把一个变化不大的值用作片键,那么可以使用该键和另一个拥有多样值的键组成
一个复合片键,比如
"logLevel"
"timestamp"
。重要的是,键的组合要具有很高的基数。 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

大数据项目管理:从规划到实现

大数据项目管理:从规划到实现

Ted Malaska, Jonathan Seidman
Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉
Spark快速大数据分析(第2版)

Spark快速大数据分析(第2版)

Jules S. Damji, Brooke Wenig, Tathagata Das, Denny Lee
Kafka权威指南(第2版)

Kafka权威指南(第2版)

Gwen Shapira, Todd Palino, Rajini Sivaram, Krit Petty

Publisher Resources

ISBN: 9787115576538