indexing in mongodb
DESCRIPTION
TRANSCRIPT
![Page 1: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/1.jpg)
MongoDB
Indexing & Query Optimization
![Page 2: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/2.jpg)
Overview
• Indexing • Profiling your queries• Creating indexes• Managing indexes
![Page 3: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/3.jpg)
What is an Index?
A data structure that can used to make certain query more efficient ?
![Page 4: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/4.jpg)
Table scans
1 2 3 4 5 6 7
Looked at 7 objects
Find where x equals 7
![Page 5: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/5.jpg)
Tree Lookup
7
6
5
4
3
2
1
Looked at 3 objects
Find where x equals 7
![Page 6: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/6.jpg)
Use explainquery = db.user.find({title:”Blog”})query.explain();
{ "cursor" : "BasicCursor", "indexBounds" : [ ], "nscanned" : 57594, "nscannedObjects" : 57594, "n" : 3, "millis" : 108}
![Page 7: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/7.jpg)
Creating Indexes
![Page 8: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/8.jpg)
Index a field
db.user.ensureIndex( { ‘name’: 1 })
1 = ascending-1 = descending
Test last query with explain method.
![Page 9: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/9.jpg)
Compound indexes
db.posts.ensureIndex({name: 1, date: -1})
Indexes maintain order index{a:1}{a: 0, b : 1}{a: 1, b : 2}{a: 1, b : 3}{a: 2, b : 1}{a: 3, b : 2}{a: 3, b : 7}{a: 3, b : 9}
![Page 10: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/10.jpg)
index{a:1,b:-1}{a: 0, b : 1}{a: 1, b : 3}{a: 1, b : 2}{a: 2, b : 1}{a: 3, b : 9}{a: 3, b : 7}{a: 3, b : 2}
![Page 11: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/11.jpg)
Unique Indexes
db.user.ensureIndex({title: 1}, {unique: true})
![Page 12: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/12.jpg)
Embedded documents
db.user.save({ title: “My First blog”, comments: [ {author: “Ram”, time : new Date()} ]});
db.posts.ensureIndex({“comments.author”: 1})
![Page 13: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/13.jpg)
Multikeys
{ “tags”: [“mongodb”,
“cool”,”balck], ...}
db.posts.ensureIndex({“tags”: 1})
![Page 14: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/14.jpg)
Covered Indexes
• New in 1.7.4• Query can be resolved in index only• Need to exclude _id from items projected
db.user.ensureIndex({“title”: 1})
db.user.find({“title”: “My blog post:}, {title: 1, _id:0}))
![Page 15: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/15.jpg)
Sparse Indexes
• Included if and only if indexed values is present• Limited to single field
db.user.ensureIndex({ title : 1} , {sparse :true} )db.user.save({ name : Rohan})db.user.save({ name : Ram , title : princes })Return only Ramdb.user.find ( { title : { $ne : null} } ) .sort({ title : 1} )
![Page 16: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/16.jpg)
Geospatial Indexes
![Page 17: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/17.jpg)
Managing Indexes
![Page 18: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/18.jpg)
Listing indexesdb.user.getIndexes();Dropping indexesdb.user.dropIndex({tag :1})Background Building Indexesdb.user.ensureIndex( { … },{background : true} })
![Page 19: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/19.jpg)
Indexing Strategies
![Page 20: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/20.jpg)
Strategies
• The application queries.• The relative frequency of query in application.• Current indexes for your application.• Which indexes most common query use.
![Page 21: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/21.jpg)
• Use indexes to sort query result• Ensure that your indexes fit entirely in RAM• Consider Performance when Creating Indexes for Write-
heavy Applications
![Page 22: Indexing In MongoDB](https://reader034.vdocuments.mx/reader034/viewer/2022051514/54b6ea954a795908448b45c0/html5/thumbnails/22.jpg)
Review
• Understand your workload– Profile your queries – Use explain on the slow ones
• Create indexes for slow operations