SlideShare una empresa de Scribd logo
1 de 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’

Más contenido relacionado

La actualidad más candente

Project Tungsten: Bringing Spark Closer to Bare Metal
Project Tungsten: Bringing Spark Closer to Bare MetalProject Tungsten: Bringing Spark Closer to Bare Metal
Project Tungsten: Bringing Spark Closer to Bare MetalDatabricks
 
MongoDB .local Toronto 2019: Tips and Tricks for Effective Indexing
MongoDB .local Toronto 2019: Tips and Tricks for Effective IndexingMongoDB .local Toronto 2019: Tips and Tricks for Effective Indexing
MongoDB .local Toronto 2019: Tips and Tricks for Effective IndexingMongoDB
 
Introduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerIntroduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerMydbops
 
MongoDB WiredTiger Internals
MongoDB WiredTiger InternalsMongoDB WiredTiger Internals
MongoDB WiredTiger InternalsNorberto Leite
 
Mongodb basics and architecture
Mongodb basics and architectureMongodb basics and architecture
Mongodb basics and architectureBishal Khanal
 
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 Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMike Friedman
 
MongoDB Aggregation Performance
MongoDB Aggregation PerformanceMongoDB Aggregation Performance
MongoDB Aggregation PerformanceMongoDB
 
NoSQL and MapReduce
NoSQL and MapReduceNoSQL and MapReduce
NoSQL and MapReduceJ Singh
 
Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략Jin wook
 
Introduction to apache lucene
Introduction to apache luceneIntroduction to apache lucene
Introduction to apache luceneShrikrishna Parab
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBNodeXperts
 

La actualidad más candente (20)

MongodB Internals
MongodB InternalsMongodB Internals
MongodB Internals
 
MongoDB (Advanced)
MongoDB (Advanced)MongoDB (Advanced)
MongoDB (Advanced)
 
Project Tungsten: Bringing Spark Closer to Bare Metal
Project Tungsten: Bringing Spark Closer to Bare MetalProject Tungsten: Bringing Spark Closer to Bare Metal
Project Tungsten: Bringing Spark Closer to Bare Metal
 
Indexing
IndexingIndexing
Indexing
 
MongoDB .local Toronto 2019: Tips and Tricks for Effective Indexing
MongoDB .local Toronto 2019: Tips and Tricks for Effective IndexingMongoDB .local Toronto 2019: Tips and Tricks for Effective Indexing
MongoDB .local Toronto 2019: Tips and Tricks for Effective Indexing
 
Introduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerIntroduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizer
 
MongoDB WiredTiger Internals
MongoDB WiredTiger InternalsMongoDB WiredTiger Internals
MongoDB WiredTiger Internals
 
Mongodb basics and architecture
Mongodb basics and architectureMongodb basics and architecture
Mongodb basics and architecture
 
Lucene indexing
Lucene indexingLucene indexing
Lucene indexing
 
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
 
Mongo DB
Mongo DB Mongo DB
Mongo DB
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World Examples
 
ElasticSearch
ElasticSearchElasticSearch
ElasticSearch
 
Mongo DB Presentation
Mongo DB PresentationMongo DB Presentation
Mongo DB Presentation
 
MongoDB Aggregation Performance
MongoDB Aggregation PerformanceMongoDB Aggregation Performance
MongoDB Aggregation Performance
 
NoSQL and MapReduce
NoSQL and MapReduceNoSQL and MapReduce
NoSQL and MapReduce
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
 
Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략
 
Introduction to apache lucene
Introduction to apache luceneIntroduction to apache lucene
Introduction to apache lucene
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 

Destacado

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
 

Destacado (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
 

Similar a 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
 

Similar a 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
 

Último

Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfadriantubila
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Delhi Call girls
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...SUHANI PANDEY
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...amitlee9823
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...shambhavirathore45
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...amitlee9823
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 

Último (20)

Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 

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’

Notas del editor

  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.