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版)
68
5
> db.users.find({"username": "user999999"}).explain("executionStats")
索引可以显著缩短查询时间。然而,使用索引是有代价的:修改索引字段的写操作(插
入、更新和删除)会花费更长的时间。这是因为在更改数据时,除了更新文档,
MongoDB
还必须更新索引。通常来说,这个代价是值得的。关键是找出要索引的字段。
MongoDB
索引的工作原理与典型的关系数据库索引几乎相同。因此,如果
你已经掌握了这部分内容,那么可以略过本节以查看语法细节。
要选择为哪些字段创建索引,可以查看常用的查询以及那些需要快速执行的查询,并尝试
从中找到一组通用的键。例如,前面的示例中查询的是
"username"
。如果这是一个特别常
见的查询或者此查询是一个性能瓶颈,那么在
"username"
上创建索引是不错的选择。然
而,如果这是一个很少用到的查询或者是由管理员执行的不太关心时间消耗的查询,那么
就不应该在此上面创建索引。
5.1.2
 复合索引简介
索引的目的是使查询尽可能高效。对于许多查询模式来说,在两个或更多的键上创建索引
是必要的。例如,索引会将其所有值按顺序保存,因此按照索引键对文档进行排序的速度
要快得多。然而,索引只有在作为排序的前缀时才有助于排序。例如,
"username"
上的索
引对下面这种排序就没什么帮助:
> db.users.find().sort({"age" : 1, "username" : 1})
这里是先根据
"age"
,然后再根据 ...
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