Indexing In MongoDB
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Indexing In MongoDB

on

  • 214 views

 

Statistics

Views

Total Views
214
Views on SlideShare
214
Embed Views
0

Actions

Likes
0
Downloads
4
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Indexing In MongoDB Presentation Transcript

  • 1. MongoDB Indexing & Query Optimization
  • 2. Overview • Indexing • Profiling your queries • Creating indexes • Managing indexes
  • 3. What is an Index? A data structure that can used to make certain query more efficient ?
  • 4. Table scans 1 2 3 4 5 6 7 Looked at 7 objects Find where x equals 7
  • 5. Tree Lookup 7 6 5 4 3 2 1 Looked at 3 objects Find where x equals 7
  • 6. Use explain query = db.user.find({title:‛Blog‛}) query.explain(); { "cursor" : "BasicCursor", "indexBounds" : [ ], "nscanned" : 57594, "nscannedObjects" : 57594, "n" : 3, "millis" : 108 }
  • 7. Creating Indexes
  • 8. Index a field db.user.ensureIndex( { ‘name’: 1 }) 1 = ascending -1 = descending Test last query with explain method.
  • 9. 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}
  • 10. 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}
  • 11. Unique Indexes db.user.ensureIndex({title: 1}, {unique: true})
  • 12. Embedded documents db.user.save({ title: ‚My First blog‛, comments: [ {author: ‚Ram‛, time : new Date()} ] }); db.posts.ensureIndex({‚comments.author‛: 1})
  • 13. Multikeys { ‚tags‛: [‚mongodb‛, ‚cool‛,‛balck] , ...} db.posts.ensureIndex({‚tags‛: 1})
  • 14. 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}))
  • 15. 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 Ram db.user.find ( { title : { $ne : null} } ) .sort({ title : 1} )
  • 16. Geospatial Indexes
  • 17. Managing Indexes
  • 18. Listing indexes db.user.getIndexes(); Dropping indexes db.user.dropIndex({tag :1}) Background Building Indexes db.user.ensureIndex( { … },{background : true} })
  • 19. Indexing Strategies
  • 20. Strategies • The application queries. • The relative frequency of query in application. • Current indexes for your application. • Which indexes most common query use.
  • 21. • Use indexes to sort query result • Ensure that your indexes fit entirely in RAM • Consider Performance when Creating Indexes for Write- heavy Applications
  • 22. Review • Understand your workload – Profile your queries – Use explain on the slow ones • Create indexes for slow operations