Indexing & query optimization
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Indexing & query optimization

  • 2,464 views
Uploaded on

My slides from MongoATL (2-8-2011)

My slides from MongoATL (2-8-2011)

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,464
On Slideshare
2,460
From Embeds
4
Number of Embeds
2

Actions

Shares
Downloads
56
Comments
0
Likes
4

Embeds 4

http://www.linkedin.com 3
https://twitter.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Indexing & Query Optimization
    • Jared Rosoff ( [email_address] )
  • 2. Overview
    • Indexing 101
    • Profiling your queries
    • Creating Indexes
    • Managing Index
  • 3. Indexing 101
  • 4. Ack! My queries are slow! http://michaelprescott.typepad.com/.a/6a00d83451574c69e20134858a87a2970c-800wi
  • 5. Index FTW! http://musformation.com/pics/excited.jpg
  • 6. Why did that happen? Magic More Magic
  • 7. Table scans 1 2 3 4 5 6 7 Looked at 7 objects Find where x equals 7
  • 8. Tree Lookup 7 6 5 4 3 2 1 Looked at 3 objects Find where x equals 7
  • 9. O(n) vs. O(log n) Table scan Index Number of records Number of comparisons
  • 10. Analyzing Query Performance
  • 11. Using the Profiler
    • db.setProfilingLevel( level )
      • 0 == off
      • 1 == slow operations (>100ms)
      • 2 == all operations
  • 12. Profiler Output
    • db.system.profile.find ({millis:{$gt:5}});
    • {
    • "ts" : "Thu Jan 29 2009 15:21:27 GMT-0500 (EST)" ,
    • "info" : "query test.foo ntoreturn:0 exception bytes:53" ,
    • "millis" : 88
    • }
  • 13. Use explain
    • query = db.coll.find({title:”My blog”})
    • query.explain();
    { "cursor" : " BasicCursor ", "indexBounds" : [ ], "nscanned" : 57594, "nscannedObjects" : 57594, "n" : 3, "millis" : 108 }
  • 14. Creating Indexes
  • 15. Index a field
    • db.posts.ensureIndex( { ‘name’: 1 })
    • 1 = ascending
    • -1 = descending
  • 16. Compound indexes
    • db.posts.ensureIndex({name: 1, date: -1})
  • 17. Unique Indexes
    • db.posts.ensureIndex({title: 1}, {unique: true})
  • 18. Embedded documents
    • db.posts.save({
    • title: “My First blog”,
    • comments: [
    • {author: “James”, ts : new Date()} ]
    • });
    • db.posts.ensureIndex({“ comments.author ” : 1})
  • 19. Multikeys
    • { “ tags ” : [ “ mongodb ” , “ cool ” ], ...}
    • db.posts.ensureIndex({ “ tags ” : 1})
  • 20. Covered Indexes
    • New in 1.7.4
    • Query can be resolved in index only
    • Need to exclude _id from items projected
    db.posts.ensureIndex({“title”: 1}) db.posts.find({“title”: “My blog post:}, {title: 1, _id:0}))
  • 21. Sparse Indexes
  • 22. Geospatial Indexes
  • 23. Managing Indexes
  • 24. Listing indexes db.posts.getIndexes()
  • 25. Dropping indexes db.posts.dropIndex({“tags”: 1})
  • 26. Background building db.posts.ensureIndex(..., {background: true})
  • 27. Query Planning
  • 28. Query Planning
  • 29. When isn’t an index used?
  • 30. Picking an Index scan Index on x Index on y Remember Terminate
  • 31. Review
    • Understand your workload
      • Profile your queries
      • Use explain on the slow ones
    • Create indexes for slow operations