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版)
索引
99
多键索引可能会比非多键索引慢一些。可能会有许多索引项指向同一个文档,因此
MongoDB
在返回结果之前可能需要做一些删除重复数据的操作。
5.1.7
 索引基数
基数
cardinality
是指集合中某个字段有多少个不同的值。有些字段,比如
"gender"
"newsletter opt-out"
,可能只有两个值,这类键的基数就非常低。其他一些字段,比如
"username"
"email"
,可能集合中的每个文档都有一个不同的值,这类键的基数就非常
高。还有一些字段介于两者之间,比如
"age"
"zip code"
通常来说,一个字段的基数越高,这个字段上的索引就越有用。这是因为这样的索引能够
迅速将搜索范围缩小到一个比较小的结果集。对于基数比较低的字段,索引通常无法排除
大量可能的匹配项。
假设我们在
"gender"
上有一个索引,需要查找名字是
Susan
的女性。通过这个索引,只能
将搜索空间缩小大约
50%
,然后再引用每个单独的文档查询
"name"
字段。相反,如果在
"name"
上创建索引,就可以立即将结果集缩小到名字为
Susan
的一小部分用户,然后引用
这些文档检查性别。
根据经验来说,应该在基数比较高的键上创建索引,或者至少应该把基数比较高的键放在
复合索引的前面(在低基数的键之前)。
5.2
 
explain
输出
如你所见,
explain
可以为查询提供大量的信息。对于慢查询来说,它是最重要的诊断工
具之一。通过查看一个查询的
explain
输出,可以了解查询都使用了哪些索引以及是如何
使用的。对于任何查询 ...
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