Fastquerying Indexingforperformance4 150324144349 Converske01
Fastquerying Indexingforperformance4 150324144349 Converske01
Muthu Chinnasamy
Senior Solutions Architect
Agenda
• Introduction
• What is fast Querying?
• Index Types & Properties
• Index Intersection
• Index Monitoring
• New in MongoDB 3.0
Introduction
MongoDB's unique architecture
• MongoDB uniquely brings the best features of
both RDBMS and NoSQL
RDBMS No SQL
Observations:
"cursor" : "BtreeCursor state_1"
"n" : 2 Better.
Better. Only
Only 1596
1596
"nscannedObjects" : 1596 documents
documents scanned
scanned for
for
"nscanned" : 1596 the
the same
same result!
result!
"scanAndOrder" : true
"millis" : 3
Compound Index on two fields
Create Index:
db.zips.ensureIndex({state:1, city:1})
Observations:
"cursor" : "BtreeCursor state_1_city_1"
"n" : 2 Much
Much better.
better. Only
Only 40
40
"nscannedObjects" : 40 documents
documents scanned
scanned
"nscanned" : 40 for
for the
the same
same result!
result!
"scanAndOrder" : true
"millis" : 0
Compound Index on three fields
Create Index:
db.zips.ensureIndex({state:1, city:1, pop:1})
Observations:
"cursor" : "BtreeCursor state_1_city_1_pop_1 reverse"
"n" : 2 22 documents
documents scanned
scanned for for
"nscannedObjects" : 2 the
the same
same result.
result. This
This isis fast
fast
"nscanned" : 2 querying
querying folks!
folks!
"scanAndOrder" : false
"millis" : 0
Types of Indexes
Types of Indexes
Be sure to remove unneeded
indexes
Drop Indexes:
db.zips.dropIndex({state:1, city:1})
db.zips.dropIndex({state:1})
// Create index on arrays of values on the "tags" field – multi key index.
>db.articles.ensureIndex( { tags : 1 } )
Sub-document indexes
• Index on sub-documents
– Using dot notation {
‘_id’ : ObjectId(..),
‘article_id’ : ObjectId(..),
‘section’ : ‘schema’,
>db.interactions.ensureIndex( ‘date’ : ISODate(..),
‘daily’: { ‘views’ : 45,
{ “daily.comments” : 1} ‘comments’ : 150 }
‘hours’ : {
} 0 : { ‘views’ : 10 },
1 : { ‘views’ : 2 },
>db.interactions.find( …
23 : { ‘views’ : 14,
{“daily.comments” : { "$gte" : 150} } ‘comments’ : 10 }
) }
}
Compound indexes
• Indexes defined on multiple fields
• Sparse Indexes
>db.articles.find( {
location: { $near :
{ $geometry :
{ type : "Point”, coordinates : [37.449, -122.158] } },
$maxDistance : 5000
}
})
Text Indexes
Operators
$text, $search, $language,
$meta >db.articles.ensureIndex(
{title: ”text”, content: ”text”}
• Only one text index )
> db.articles.find(
{ $text: { $search: "MongoDB" }},
{ score: { $meta: "textScore" }, _id:0, title:1 } )
MongoDB should be able to use this index as the all fields of the
compound index are used in the query
Index Intersection
• Consider the scenario with collection having a Compound Index
{status:1, order_date: -1} & your query is
b. find({status: 'A'})
MongoDB will not be able to use this index as sort order on the
order_date in the query is different than that of the compound
index
Index Intersection
Index intersection should be able to resolve all four query
combinations with two separate indexes
{ Other Types:
"cursor" : ”BasicCursor",
… •BasicCursor
"n" : 12, • Full collection scan
"nscannedObjects" : 25820, •BtreeCursor
"nscanned" : 25820, •GeoSearchCursor
… •Complex Plan
"indexOnly" : false, •TextCursor
…
"millis" : 27,
…
}
Explain plan output (Index)
{ Other Types:
"cursor" : "BtreeCursor author_1_date_-
1", •BasicCursor
… • Full collection scan
"n" : 12, •BtreeCursor
"nscannedObjects" : 12, •GeoSearchCursor
"nscanned" : 12, •Complex Plan
… •TextCursor
"indexOnly" : false,
…
"millis" : 0,
…
}
Explain() method in 3.0
• By default .explain() gives query planner verbosity
mode. To see stats use .explain("executionStats")
• Descriptive names used for some key fields
{ …
"nReturned" : 2,
"executionTimeMillis" : 0,
"totalKeysExamined" : 2,
"totalDocsExamined" : 2,
"indexName" : "state_1_city_1_pop_1",
"direction" : "backward",
…
}
Explain() method in 3.0
• Fine grained query introspection into query plan and
query execution – Stages
• Support for commands: Count, Group, Delete,
Update
• db.collection.explain().find() – Allows for additional
chaining of query modifiers
– Returns a cursor to the explain result
– var a = db.zips.explain().find({state: 'NY'})
– a.next() to return the results
Database Profiler
Production Support
In production and under control
Development Support
Let’s get you running
Consulting
We solve problems
Training
Get your teams up to speed.