SlideShare ist ein Scribd-Unternehmen logo
1 von 45
MongoDB and Indexes
Doug Duncan
dugdun@hotmail.com
@dugdun
What we’ll cover
• What are indexes?
• Types
• Properties
• Why use indexes?
• How to create indexes.
• Commands to check indexes and plans.
What are indexes?
What are indexes?
Indexes are special data-structures that store a subset of
your data in an easily traversable format.
MongoDB stores indexes in a b-tree format which allows for
efficient access to the index content.
Proper index use is good and makes a system run
optimally. Improper index use can bring a system to a
grinding halt.
What are indexes?
Indexes are stored similar in a format similar to the
following if there was an index on Origin:
[ABE] -> 0xa193b48c
[ABE] -> 0x8e8b242a
[ABE] -> 0x0928cdc1
…
[DEN] -> 0x24aa4ecd
[DEN] -> 0x87396a3c
[DEN] -> 0x9392ab2f
…
[LAX] -> 0x89ccede0
…
Types of indexes
• _id
• Simple
• Compound
• Multikey
• Full-Text
• Geo-spatial
• Hashed
The _id index
• The _id index is automatically created and cannot be removed.
• This is the same as a primary key in traditional RDBMS.
• Default value is a 12-byte ObjectId:
• 4-byte time stamp
• 3-byte machine id
• 2-byte process id
• 3-byte counter
Simple index
• A simple index is an index on a single key
• This is similar to a book’s index where you look
up a word to find the pages it’s referenced on.
Compound index
• A compound index is created over two or more
fields in a document
• This is similar to a phone book where you can
find the phone number of a person given their
first and last names.
Multikey index
• A multikey index is an index that’s created on a
field that contains an array.
• If using in a compound index, only a single field
in a given document can be an array.
• You will get one entry in the index for every item
in the array for the given document. This means
if you have an array with 100 items, that
document will have 100 index entries.
Full-text index
• This is an index over a text based field, similar to
how Google indexes web pages.
Geo-spatial index
• A geo-spatial index will allow you to determine
distance from a given point.
• Works on both planar and spherical geometries.
Hashed indexes
• A hashed index is used in hash based sharding,
and allows for a more randomized distribution.
• Hashed indexes cannot contain compound keys
or be unique.
• Hashed indexes can contain the key in both a
hashed and non-hashed version. The non-
hashed version will allow for range based
queries.
Index properties
• Unique
• Sparse
• TTL
• Partial (new in 3.2)
Unique
• The unique property allows for only a single
value for the indexed field, or combination of
fields for a compound index
db.collection.createIndex({“email”: 1}, {“unique”:
true})
• A unique index can only have a single null or
missing field value for all documents in the
collection.
Sparse
• The sparse property allows you to index only
documents that contain a value for the given
field.
db.collection.createIndex({“kids”: 1}, {“sparse”: true})
• A sparse index will not be used if it would result in
an incomplete result set, unless specifically
hinted.
db.collection.find({“kids”: {“$gte”: 5})
TTL
• The TTL property allows for the automatic removal of
documents after a given time period.
db.collection.createIndex({“accessTime”: 1}, {“expireAfterSeconds”:
“1200”})
• The indexed field should contain an ISODate() value. If
any other type is used the document will not be removed.
• The TTL removal process runs once every 60 seconds so
you might see the document even though the time has
expired.
Partial
• The partial property allows you to index a subset
of your data.
db.collection.createIndex({“movie”: 1, “reviews”: 1},
{“rating”: {“$gte”: 4}})
• The index will not be used if it would provide an
incomplete result set (similar to the sparse
index).
Why use indexes?
Why use indexes?
• Efficiently retrieving document matches
• Equality matching
• Inequality or range matching
• Sorting
• Lack of a usable index will cause MongoDB to
scan the entire collection.
How to create indexes.
Before creating indexes
• Think about the queries you will be running and try to
create as few indexes as possible to support those
queries. Similar query patterns could use the same
(or very similar) indexes.
• Think about the data that you will query and put your
highly selective fields first in the index if possible.
• Check your current indexes before creating new
ones. MongoDB will allow you to create indexes with
the same fields in different orders.
Simple indexes
• When creating a simple index, the sort order,
ascending (1) or descending (-1), of the values
doesn’t matter as much as MongoDB can walk
the index forwards and backwards.
• Simple index creation:
db.flights.createIndex({“Origin”: 1})
Compound indexes
• When creating a compound index, the sort order, ascending (1) or
descending (-1), of the values starts to matter, especially if the index is used
to sort on multiple keys.
• When creating compound indexes you want to add keys to the index in the
following key order:
• Equality matches
• Sort fields
• Inequality matches
• A compound index will also help any queries that are made based off the
left most subset of keys.
Compound indexes
• Compound index creation:
db.flights.createIndex({“Origin”: 1, “Dest”: 1, “FlightDate”: -1})
• Queries supported:
db.flights.find({“Origin”: “DEN”})
db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”})
db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”}).sort({“FlightDate”: -1})
db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”}).sort({“FlightDate”: 1})
Compound indexes
• An index created as follows:
db.flights.createIndex({“Origin”: 1, “Dest”: -1})
Could be used with either of the following queries as well
since MongoDB can walk the index either way:
db.flights.find().sort({“Origin”: 1, “Dest”: -1})
db.flights.find().sort({“Origin”: -1, “Dest”: 1})
Full-text indexes
• Full-text index creation:
• db.messages.createIndex({“body”: “text”})
• To search using the index finding any of the words:
db.messages.find({“$text”: {“$search”: “some text”}})
• To search using the index finding a phrase
db.message.find({“$text”: {“$search”: “”some text””}}
Covering indexes
• Covering indexes are indexes that will answer a
query without going back to the data. For example:
db.flights.createIndex({“Origin”: 1, “Dest”: 1, “ArrDelay”:
1, “UniqueCarrier”: 1})
• The following query would be covered as all fields
are in the index:
db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”},
{“UniqueCarrier”: 1, “ArrDelay”: 1, “_id”:
0}).sort({“ArrDelay”: -1})
Indexing nested
fields/documents
• Let’s say you have documents with nested documents in them like the
following:
db.locations.findOne()
{
“_id”: ObjectId(…),
…,
“location”: {
“state”: “Colorado”,
“city”: “Lyons”
}
}
Indexing nested
fields/documents
• You can index on embedded fields by using dot
notation:
db.locations.createIndex({“location.state”: 1})
Indexing nested
fields/documents
• You can also index embedded documents
db.locations.createIndex({“location”: 1})
• If you do this the query must match the document exactly
(keys in the same order). That means that this will return the
document:
db.locations.find({“location”: {“state”: “Colorado”, “city”:
“Lyons”})
• But this won’t:
db.locations.find({“location”: {“city”: “Lyons”, “state”:
“Colorado”})
Index Intersection
• Index intersection is when MongoDB uses two or more
indexes to satisfy a query.
• Given the following two indexes:
db.orders.createIndex({“qty”: 1})
db.orders.createIndex({“item”: 1})
• Index intersection means a query such as the following
could use both indexes in parallel with the results being
merged together to satisfy the query:
db.orders.find({“item”: “ABC123”, “qty”: {“$gte”: 15}})
Indexing arrays
• You can index fields that contain arrays as well.
• Compound indexes however can only have a single field that is an array in a given document. If
a document has two indexed fields that are arrays, you will get an error.
db.arrtest.createIndex({“a”: 1, “b”: 1})
db.arrtest.insert({"b": [1,2,3], "a": [1,2,3]})
cannot index parallel arrays [b] [a]
WriteResult({
"nInserted": 0,
"writeError": {
"code": 10088,
"errmsg": "cannot index parallel arrays [b] [a]"
}
})
Index Intersection
• Index intersection is when MongoDB uses two or more
indexes to satisfy a query.
• Given the following two indexes:
db.orders.createIndex({“qty”: 1})
db.orders.createIndex({“item”: 1})
• Index intersection means a query such as the following
could in theory use both indexes in parallel with the results
being merged together to satisfy the query:
db.orders.find({“item”: “ABC123”, “qty”: {“$gte”: 15}})
Removing indexes
• The command to remove indexes is similar to the
one to create the index.
db.flights.dropIndex({“Origin”: 1, “Dest”: -1})
Commands to check
indexes and index usage
View all indexes in a
database
• To view all indexes in a database use the
following command:
db.system.indexes.find()
• For each index you’ll see the fields the index was
created with, the name of the index and the
namespace (db.collection) that the index was
built on.
View indexes for a given
collection
• To view all indexes for a given collection use the
following command:
db.collection.getIndexes()
• This returns the same information as the
previous command, but is limited to the given
collection.
View index sizes
• To view the size of all indexes in a collection:
db.collection.stats()
• You will see the size of all indexes and the size
of each individual index in the results. The sizes
are in bytes.
How to see if an index is
used
• If you want to see if an index is used, append the
.explain() operator to your query
db.flights.find({“Origin”: “DEN”}).explain()
• The explain operator has three levels of verbosity:
• queryPlanner - this is the default, and it returns the winning query plan
• executionStats - adds execution stats for the plan
• allPlansExecution - adds stats for the other candidate plans
Notes on indexes.
• When creating an index you need to know your
data and the queries that will run against it.
• Don’t build indexes in isolation!
• While indexes can improve performance, be
careful to not over index as every index gets
updated every time you write to the collection.
Q & A
End Notes
• User group discounts
• Manning publications: www.manning.com
• Code ‘ug367’ to save 36% off order
• APress publications: www.appress.com
• Code ‘UserGroup’ to save 10% off order
• O’Reilly publication: www.oreilly.com
• Still waiting to get information
End Notes
• Communication
• Twitter: @MUGDenver and #MUGDenver
• Email: mugdenver@gmail.com
• Slack: ???
End Notes
• MongoDB World
• When: June 28th and 29th
• Where: NYC
• Save 25% by using code ‘DDuncan’

Weitere ähnliche Inhalte

Was ist angesagt?

MongoDB World 2019: The Sights (and Smells) of a Bad Query
MongoDB World 2019: The Sights (and Smells) of a Bad QueryMongoDB World 2019: The Sights (and Smells) of a Bad Query
MongoDB World 2019: The Sights (and Smells) of a Bad QueryMongoDB
 
MongoDB World 2019: Tips and Tricks++ for Querying and Indexing MongoDB
MongoDB World 2019: Tips and Tricks++ for Querying and Indexing MongoDBMongoDB World 2019: Tips and Tricks++ for Querying and Indexing MongoDB
MongoDB World 2019: Tips and Tricks++ for Querying and Indexing MongoDBMongoDB
 
MongoDB Fundamentals
MongoDB FundamentalsMongoDB Fundamentals
MongoDB FundamentalsMongoDB
 
Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략Jin wook
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptgourav kottawar
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBNodeXperts
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)Ankit Dubey
 
Indexing and Query Optimization
Indexing and Query OptimizationIndexing and Query Optimization
Indexing and Query OptimizationMongoDB
 
Doing Joins in MongoDB: Best Practices for Using $lookup
Doing Joins in MongoDB: Best Practices for Using $lookupDoing Joins in MongoDB: Best Practices for Using $lookup
Doing Joins in MongoDB: Best Practices for Using $lookupMongoDB
 
Intro To MongoDB
Intro To MongoDBIntro To MongoDB
Intro To MongoDBAlex Sharp
 
SQL: Creating and Altering Tables
SQL: Creating and Altering TablesSQL: Creating and Altering Tables
SQL: Creating and Altering TablesRJ Podeschi
 
Introduction to MongoDB.pptx
Introduction to MongoDB.pptxIntroduction to MongoDB.pptx
Introduction to MongoDB.pptxSurya937648
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)Hemant Kumar Singh
 

Was ist angesagt? (20)

MongoDB World 2019: The Sights (and Smells) of a Bad Query
MongoDB World 2019: The Sights (and Smells) of a Bad QueryMongoDB World 2019: The Sights (and Smells) of a Bad Query
MongoDB World 2019: The Sights (and Smells) of a Bad Query
 
MongoDB World 2019: Tips and Tricks++ for Querying and Indexing MongoDB
MongoDB World 2019: Tips and Tricks++ for Querying and Indexing MongoDBMongoDB World 2019: Tips and Tricks++ for Querying and Indexing MongoDB
MongoDB World 2019: Tips and Tricks++ for Querying and Indexing MongoDB
 
MongoDB Fundamentals
MongoDB FundamentalsMongoDB Fundamentals
MongoDB Fundamentals
 
Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략
 
Indexing
IndexingIndexing
Indexing
 
MongoDB Sharding Fundamentals
MongoDB Sharding Fundamentals MongoDB Sharding Fundamentals
MongoDB Sharding Fundamentals
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) ppt
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
 
Indexing and Query Optimization
Indexing and Query OptimizationIndexing and Query Optimization
Indexing and Query Optimization
 
MongoDB
MongoDBMongoDB
MongoDB
 
Doing Joins in MongoDB: Best Practices for Using $lookup
Doing Joins in MongoDB: Best Practices for Using $lookupDoing Joins in MongoDB: Best Practices for Using $lookup
Doing Joins in MongoDB: Best Practices for Using $lookup
 
Intro To MongoDB
Intro To MongoDBIntro To MongoDB
Intro To MongoDB
 
MongoDB (Advanced)
MongoDB (Advanced)MongoDB (Advanced)
MongoDB (Advanced)
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
SQL: Creating and Altering Tables
SQL: Creating and Altering TablesSQL: Creating and Altering Tables
SQL: Creating and Altering Tables
 
Introduction to MongoDB.pptx
Introduction to MongoDB.pptxIntroduction to MongoDB.pptx
Introduction to MongoDB.pptx
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
 
Indexes in postgres
Indexes in postgresIndexes in postgres
Indexes in postgres
 

Andere mochten auch

Performance Tuning on the Fly at CMP.LY
Performance Tuning on the Fly at CMP.LYPerformance Tuning on the Fly at CMP.LY
Performance Tuning on the Fly at CMP.LYMongoDB
 
Webinar: Index Tuning and Evaluation
Webinar: Index Tuning and EvaluationWebinar: Index Tuning and Evaluation
Webinar: Index Tuning and EvaluationMongoDB
 
Indexing with MongoDB
Indexing with MongoDBIndexing with MongoDB
Indexing with MongoDBlehresman
 
Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)MongoSF
 
MongoDB Performance Tuning
MongoDB Performance TuningMongoDB Performance Tuning
MongoDB Performance TuningMongoDB
 
MongoDB Days UK: Indexing and Performance Tuning
MongoDB Days UK: Indexing and Performance TuningMongoDB Days UK: Indexing and Performance Tuning
MongoDB Days UK: Indexing and Performance TuningMongoDB
 
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based ShardingWebinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based ShardingMongoDB
 
Everything You Need to Know About Sharding
Everything You Need to Know About ShardingEverything You Need to Know About Sharding
Everything You Need to Know About ShardingMongoDB
 
Sharding Methods for MongoDB
Sharding Methods for MongoDBSharding Methods for MongoDB
Sharding Methods for MongoDBMongoDB
 
Optimizing MongoDB: Lessons Learned at Localytics
Optimizing MongoDB: Lessons Learned at LocalyticsOptimizing MongoDB: Lessons Learned at Localytics
Optimizing MongoDB: Lessons Learned at Localyticsandrew311
 

Andere mochten auch (10)

Performance Tuning on the Fly at CMP.LY
Performance Tuning on the Fly at CMP.LYPerformance Tuning on the Fly at CMP.LY
Performance Tuning on the Fly at CMP.LY
 
Webinar: Index Tuning and Evaluation
Webinar: Index Tuning and EvaluationWebinar: Index Tuning and Evaluation
Webinar: Index Tuning and Evaluation
 
Indexing with MongoDB
Indexing with MongoDBIndexing with MongoDB
Indexing with MongoDB
 
Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)
 
MongoDB Performance Tuning
MongoDB Performance TuningMongoDB Performance Tuning
MongoDB Performance Tuning
 
MongoDB Days UK: Indexing and Performance Tuning
MongoDB Days UK: Indexing and Performance TuningMongoDB Days UK: Indexing and Performance Tuning
MongoDB Days UK: Indexing and Performance Tuning
 
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based ShardingWebinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
 
Everything You Need to Know About Sharding
Everything You Need to Know About ShardingEverything You Need to Know About Sharding
Everything You Need to Know About Sharding
 
Sharding Methods for MongoDB
Sharding Methods for MongoDBSharding Methods for MongoDB
Sharding Methods for MongoDB
 
Optimizing MongoDB: Lessons Learned at Localytics
Optimizing MongoDB: Lessons Learned at LocalyticsOptimizing MongoDB: Lessons Learned at Localytics
Optimizing MongoDB: Lessons Learned at Localytics
 

Ähnlich wie MongoDB Indexes Guide

Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleMongoDB
 
unit 4,Indexes in database.docx
unit 4,Indexes in database.docxunit 4,Indexes in database.docx
unit 4,Indexes in database.docxRaviRajput416403
 
Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)MongoDB
 
Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27MongoDB
 
Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26kreuter
 
Mongo db a deep dive of mongodb indexes
Mongo db  a deep dive of mongodb indexesMongo db  a deep dive of mongodb indexes
Mongo db a deep dive of mongodb indexesRajesh Kumar
 
Indexing and Query Optimizer
Indexing and Query OptimizerIndexing and Query Optimizer
Indexing and Query OptimizerMongoDB
 
About elasticsearch
About elasticsearchAbout elasticsearch
About elasticsearchMinsoo Jun
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data ModelingDATAVERSITY
 
Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)MongoDB
 
Test driving Azure Search and DocumentDB
Test driving Azure Search and DocumentDBTest driving Azure Search and DocumentDB
Test driving Azure Search and DocumentDBAndrew Siemer
 
An Introduction to Elastic Search.
An Introduction to Elastic Search.An Introduction to Elastic Search.
An Introduction to Elastic Search.Jurriaan Persyn
 
Automated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index RobotAutomated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index RobotMongoDB
 
Indexing and Query Optimisation
Indexing and Query OptimisationIndexing and Query Optimisation
Indexing and Query OptimisationMongoDB
 
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2Antonios Giannopoulos
 
2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongo2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongoMichael Bright
 

Ähnlich wie MongoDB Indexes Guide (20)

Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You Scale
 
Nosql part 2
Nosql part 2Nosql part 2
Nosql part 2
 
unit 4,Indexes in database.docx
unit 4,Indexes in database.docxunit 4,Indexes in database.docx
unit 4,Indexes in database.docx
 
Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)
 
Query Optimization in MongoDB
Query Optimization in MongoDBQuery Optimization in MongoDB
Query Optimization in MongoDB
 
Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27
 
Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26
 
Mongo db a deep dive of mongodb indexes
Mongo db  a deep dive of mongodb indexesMongo db  a deep dive of mongodb indexes
Mongo db a deep dive of mongodb indexes
 
Indexing and Query Optimizer
Indexing and Query OptimizerIndexing and Query Optimizer
Indexing and Query Optimizer
 
About elasticsearch
About elasticsearchAbout elasticsearch
About elasticsearch
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling
 
Mongo indexes
Mongo indexesMongo indexes
Mongo indexes
 
Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)
 
Test driving Azure Search and DocumentDB
Test driving Azure Search and DocumentDBTest driving Azure Search and DocumentDB
Test driving Azure Search and DocumentDB
 
An Introduction to Elastic Search.
An Introduction to Elastic Search.An Introduction to Elastic Search.
An Introduction to Elastic Search.
 
Automated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index RobotAutomated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index Robot
 
Indexing In MongoDB
Indexing In MongoDBIndexing In MongoDB
Indexing In MongoDB
 
Indexing and Query Optimisation
Indexing and Query OptimisationIndexing and Query Optimisation
Indexing and Query Optimisation
 
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
 
2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongo2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongo
 

Kürzlich hochgeladen

English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfEnglish-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfblazblazml
 
Digital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksDigital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksdeepakthakur548787
 
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...Amil Baba Dawood bangali
 
wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...
wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...
wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...KarteekMane1
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPTBoston Institute of Analytics
 
modul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptxmodul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptxaleedritatuxx
 
The Power of Data-Driven Storytelling_ Unveiling the Layers of Insight.pptx
The Power of Data-Driven Storytelling_ Unveiling the Layers of Insight.pptxThe Power of Data-Driven Storytelling_ Unveiling the Layers of Insight.pptx
The Power of Data-Driven Storytelling_ Unveiling the Layers of Insight.pptxTasha Penwell
 
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...Boston Institute of Analytics
 
Principles and Practices of Data Visualization
Principles and Practices of Data VisualizationPrinciples and Practices of Data Visualization
Principles and Practices of Data VisualizationKianJazayeri1
 
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...Thomas Poetter
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Seán Kennedy
 
Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 217djon017
 
Advanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsAdvanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsVICTOR MAESTRE RAMIREZ
 
Real-Time AI Streaming - AI Max Princeton
Real-Time AI  Streaming - AI Max PrincetonReal-Time AI  Streaming - AI Max Princeton
Real-Time AI Streaming - AI Max PrincetonTimothy Spann
 
Unveiling the Role of Social Media Suspect Investigators in Preventing Online...
Unveiling the Role of Social Media Suspect Investigators in Preventing Online...Unveiling the Role of Social Media Suspect Investigators in Preventing Online...
Unveiling the Role of Social Media Suspect Investigators in Preventing Online...Milind Agarwal
 
Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Seán Kennedy
 
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...Boston Institute of Analytics
 
Defining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryDefining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryJeremy Anderson
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Cathrine Wilhelmsen
 
Bank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis ProjectBank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis ProjectBoston Institute of Analytics
 

Kürzlich hochgeladen (20)

English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfEnglish-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
 
Digital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksDigital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing works
 
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
 
wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...
wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...
wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
 
modul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptxmodul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptx
 
The Power of Data-Driven Storytelling_ Unveiling the Layers of Insight.pptx
The Power of Data-Driven Storytelling_ Unveiling the Layers of Insight.pptxThe Power of Data-Driven Storytelling_ Unveiling the Layers of Insight.pptx
The Power of Data-Driven Storytelling_ Unveiling the Layers of Insight.pptx
 
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
 
Principles and Practices of Data Visualization
Principles and Practices of Data VisualizationPrinciples and Practices of Data Visualization
Principles and Practices of Data Visualization
 
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...
 
Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2
 
Advanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsAdvanced Machine Learning for Business Professionals
Advanced Machine Learning for Business Professionals
 
Real-Time AI Streaming - AI Max Princeton
Real-Time AI  Streaming - AI Max PrincetonReal-Time AI  Streaming - AI Max Princeton
Real-Time AI Streaming - AI Max Princeton
 
Unveiling the Role of Social Media Suspect Investigators in Preventing Online...
Unveiling the Role of Social Media Suspect Investigators in Preventing Online...Unveiling the Role of Social Media Suspect Investigators in Preventing Online...
Unveiling the Role of Social Media Suspect Investigators in Preventing Online...
 
Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...
 
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
 
Defining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryDefining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data Story
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)
 
Bank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis ProjectBank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis Project
 

MongoDB Indexes Guide

  • 1. MongoDB and Indexes Doug Duncan dugdun@hotmail.com @dugdun
  • 2. What we’ll cover • What are indexes? • Types • Properties • Why use indexes? • How to create indexes. • Commands to check indexes and plans.
  • 4. What are indexes? Indexes are special data-structures that store a subset of your data in an easily traversable format. MongoDB stores indexes in a b-tree format which allows for efficient access to the index content. Proper index use is good and makes a system run optimally. Improper index use can bring a system to a grinding halt.
  • 5. What are indexes? Indexes are stored similar in a format similar to the following if there was an index on Origin: [ABE] -> 0xa193b48c [ABE] -> 0x8e8b242a [ABE] -> 0x0928cdc1 … [DEN] -> 0x24aa4ecd [DEN] -> 0x87396a3c [DEN] -> 0x9392ab2f … [LAX] -> 0x89ccede0 …
  • 6. Types of indexes • _id • Simple • Compound • Multikey • Full-Text • Geo-spatial • Hashed
  • 7. The _id index • The _id index is automatically created and cannot be removed. • This is the same as a primary key in traditional RDBMS. • Default value is a 12-byte ObjectId: • 4-byte time stamp • 3-byte machine id • 2-byte process id • 3-byte counter
  • 8. Simple index • A simple index is an index on a single key • This is similar to a book’s index where you look up a word to find the pages it’s referenced on.
  • 9. Compound index • A compound index is created over two or more fields in a document • This is similar to a phone book where you can find the phone number of a person given their first and last names.
  • 10. Multikey index • A multikey index is an index that’s created on a field that contains an array. • If using in a compound index, only a single field in a given document can be an array. • You will get one entry in the index for every item in the array for the given document. This means if you have an array with 100 items, that document will have 100 index entries.
  • 11. Full-text index • This is an index over a text based field, similar to how Google indexes web pages.
  • 12. Geo-spatial index • A geo-spatial index will allow you to determine distance from a given point. • Works on both planar and spherical geometries.
  • 13. Hashed indexes • A hashed index is used in hash based sharding, and allows for a more randomized distribution. • Hashed indexes cannot contain compound keys or be unique. • Hashed indexes can contain the key in both a hashed and non-hashed version. The non- hashed version will allow for range based queries.
  • 14. Index properties • Unique • Sparse • TTL • Partial (new in 3.2)
  • 15. Unique • The unique property allows for only a single value for the indexed field, or combination of fields for a compound index db.collection.createIndex({“email”: 1}, {“unique”: true}) • A unique index can only have a single null or missing field value for all documents in the collection.
  • 16. Sparse • The sparse property allows you to index only documents that contain a value for the given field. db.collection.createIndex({“kids”: 1}, {“sparse”: true}) • A sparse index will not be used if it would result in an incomplete result set, unless specifically hinted. db.collection.find({“kids”: {“$gte”: 5})
  • 17. TTL • The TTL property allows for the automatic removal of documents after a given time period. db.collection.createIndex({“accessTime”: 1}, {“expireAfterSeconds”: “1200”}) • The indexed field should contain an ISODate() value. If any other type is used the document will not be removed. • The TTL removal process runs once every 60 seconds so you might see the document even though the time has expired.
  • 18. Partial • The partial property allows you to index a subset of your data. db.collection.createIndex({“movie”: 1, “reviews”: 1}, {“rating”: {“$gte”: 4}}) • The index will not be used if it would provide an incomplete result set (similar to the sparse index).
  • 20. Why use indexes? • Efficiently retrieving document matches • Equality matching • Inequality or range matching • Sorting • Lack of a usable index will cause MongoDB to scan the entire collection.
  • 21. How to create indexes.
  • 22. Before creating indexes • Think about the queries you will be running and try to create as few indexes as possible to support those queries. Similar query patterns could use the same (or very similar) indexes. • Think about the data that you will query and put your highly selective fields first in the index if possible. • Check your current indexes before creating new ones. MongoDB will allow you to create indexes with the same fields in different orders.
  • 23. Simple indexes • When creating a simple index, the sort order, ascending (1) or descending (-1), of the values doesn’t matter as much as MongoDB can walk the index forwards and backwards. • Simple index creation: db.flights.createIndex({“Origin”: 1})
  • 24. Compound indexes • When creating a compound index, the sort order, ascending (1) or descending (-1), of the values starts to matter, especially if the index is used to sort on multiple keys. • When creating compound indexes you want to add keys to the index in the following key order: • Equality matches • Sort fields • Inequality matches • A compound index will also help any queries that are made based off the left most subset of keys.
  • 25. Compound indexes • Compound index creation: db.flights.createIndex({“Origin”: 1, “Dest”: 1, “FlightDate”: -1}) • Queries supported: db.flights.find({“Origin”: “DEN”}) db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”}) db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”}).sort({“FlightDate”: -1}) db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”}).sort({“FlightDate”: 1})
  • 26. Compound indexes • An index created as follows: db.flights.createIndex({“Origin”: 1, “Dest”: -1}) Could be used with either of the following queries as well since MongoDB can walk the index either way: db.flights.find().sort({“Origin”: 1, “Dest”: -1}) db.flights.find().sort({“Origin”: -1, “Dest”: 1})
  • 27. Full-text indexes • Full-text index creation: • db.messages.createIndex({“body”: “text”}) • To search using the index finding any of the words: db.messages.find({“$text”: {“$search”: “some text”}}) • To search using the index finding a phrase db.message.find({“$text”: {“$search”: “”some text””}}
  • 28. Covering indexes • Covering indexes are indexes that will answer a query without going back to the data. For example: db.flights.createIndex({“Origin”: 1, “Dest”: 1, “ArrDelay”: 1, “UniqueCarrier”: 1}) • The following query would be covered as all fields are in the index: db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”}, {“UniqueCarrier”: 1, “ArrDelay”: 1, “_id”: 0}).sort({“ArrDelay”: -1})
  • 29. Indexing nested fields/documents • Let’s say you have documents with nested documents in them like the following: db.locations.findOne() { “_id”: ObjectId(…), …, “location”: { “state”: “Colorado”, “city”: “Lyons” } }
  • 30. Indexing nested fields/documents • You can index on embedded fields by using dot notation: db.locations.createIndex({“location.state”: 1})
  • 31. Indexing nested fields/documents • You can also index embedded documents db.locations.createIndex({“location”: 1}) • If you do this the query must match the document exactly (keys in the same order). That means that this will return the document: db.locations.find({“location”: {“state”: “Colorado”, “city”: “Lyons”}) • But this won’t: db.locations.find({“location”: {“city”: “Lyons”, “state”: “Colorado”})
  • 32. Index Intersection • Index intersection is when MongoDB uses two or more indexes to satisfy a query. • Given the following two indexes: db.orders.createIndex({“qty”: 1}) db.orders.createIndex({“item”: 1}) • Index intersection means a query such as the following could use both indexes in parallel with the results being merged together to satisfy the query: db.orders.find({“item”: “ABC123”, “qty”: {“$gte”: 15}})
  • 33. Indexing arrays • You can index fields that contain arrays as well. • Compound indexes however can only have a single field that is an array in a given document. If a document has two indexed fields that are arrays, you will get an error. db.arrtest.createIndex({“a”: 1, “b”: 1}) db.arrtest.insert({"b": [1,2,3], "a": [1,2,3]}) cannot index parallel arrays [b] [a] WriteResult({ "nInserted": 0, "writeError": { "code": 10088, "errmsg": "cannot index parallel arrays [b] [a]" } })
  • 34. Index Intersection • Index intersection is when MongoDB uses two or more indexes to satisfy a query. • Given the following two indexes: db.orders.createIndex({“qty”: 1}) db.orders.createIndex({“item”: 1}) • Index intersection means a query such as the following could in theory use both indexes in parallel with the results being merged together to satisfy the query: db.orders.find({“item”: “ABC123”, “qty”: {“$gte”: 15}})
  • 35. Removing indexes • The command to remove indexes is similar to the one to create the index. db.flights.dropIndex({“Origin”: 1, “Dest”: -1})
  • 36. Commands to check indexes and index usage
  • 37. View all indexes in a database • To view all indexes in a database use the following command: db.system.indexes.find() • For each index you’ll see the fields the index was created with, the name of the index and the namespace (db.collection) that the index was built on.
  • 38. View indexes for a given collection • To view all indexes for a given collection use the following command: db.collection.getIndexes() • This returns the same information as the previous command, but is limited to the given collection.
  • 39. View index sizes • To view the size of all indexes in a collection: db.collection.stats() • You will see the size of all indexes and the size of each individual index in the results. The sizes are in bytes.
  • 40. How to see if an index is used • If you want to see if an index is used, append the .explain() operator to your query db.flights.find({“Origin”: “DEN”}).explain() • The explain operator has three levels of verbosity: • queryPlanner - this is the default, and it returns the winning query plan • executionStats - adds execution stats for the plan • allPlansExecution - adds stats for the other candidate plans
  • 41. Notes on indexes. • When creating an index you need to know your data and the queries that will run against it. • Don’t build indexes in isolation! • While indexes can improve performance, be careful to not over index as every index gets updated every time you write to the collection.
  • 42. Q & A
  • 43. End Notes • User group discounts • Manning publications: www.manning.com • Code ‘ug367’ to save 36% off order • APress publications: www.appress.com • Code ‘UserGroup’ to save 10% off order • O’Reilly publication: www.oreilly.com • Still waiting to get information
  • 44. End Notes • Communication • Twitter: @MUGDenver and #MUGDenver • Email: mugdenver@gmail.com • Slack: ???
  • 45. End Notes • MongoDB World • When: June 28th and 29th • Where: NYC • Save 25% by using code ‘DDuncan’

Hinweis der Redaktion

  1. The indexes do not have to store the field names as all fields are the same for each entry. After the values you will have a pointer back to the data portion of the file.
  2. _id index is a primary key. Default value is a 12 byte ObjectId that has as it’s first 4 bytes a time stamp that the document was entered into the collection. 3 byte machine id, 2 byte process id and 3 byte counter starting with a random value. You can however override this as long as the values you enter are unique. Automatically created and cannot be removed. Simple index is similar to a book where you look up a word and find page numbers. Compound index is similar to a phone book where you can find the phone number of a person if you know their first and last names. Multikey indexes are indexes over columns that have an array. There will be an entry for each item in the array and there can only be a single array column indexed in a given index. Full-text indexes are similar to what Google does when search for words in a web site. Geo-spatial indexes allow you to determine map proximity similar to Google maps find restaurants around this location. Can use both 2d for planar geometry and 2dsphere for spherical geometries. Hashed indexes are used in hash-based sharding which allows for a more random distribution. Can only do equality searches against this type of index, unless you add the field as in both hashed and non-hashed forms ({“field”: “hashed”, “field”: 1}). Cannot be a compound or unique index.
  3. Unique indexes are indexes that can only store a single value for the given key that’s being indexes (or set of values if a compound index). This can only contain a single document that has a null for the indexed field or a document that doesn’t have the field at all. Cannot have both of these. db.coll.ensureIndex({“a”: 1}, {“unique”: true}). Sparse indexes only index the documents that the field actually exists in. Will not index missing fields, but will index fields whose value is null. db.coll.ensureIndex({“a”: 1}, {“sparse”: true}). Use db.coll.find().hint({“a”: 1}) to see what index contains. In 2.6 and earlier, this could result in queries returning incorrect data. TTL indexes are indexes that will automatically remove documents in a collection after a given time. Indexed field needs to be a Date object. db.coll.ensureIndex({“field1”: 1}, {“expireAfterSeconds”: 300}). If you put any other value in the indexed field it will never expire. Partial indexes allow you to add a filter to the index so only those documents are indexes. This allows you to have smaller storage footprint than a regular index over the same field. These should be preferred over sparse indexes. db.coll.createIndex({“a”: 1}, {“partialFilterExpression”: {“a”: {“$gt”: 5}}}). Mongo will not use this index if it will return an incomplete result set. The query must contain the filter expression or a modified version that will return a smaller subset of the documents covered by the index.
  4. I’ve never had a case where index intersection worked, at least not when running an explain() on the query.