Your SlideShare is downloading. ×
0
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Indexing & query optimization
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Indexing & query optimization

2,173

Published on

My slides from MongoATL (2-8-2011)

My slides from MongoATL (2-8-2011)

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,173
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
58
Comments
0
Likes
4
Embeds 0
No embeds

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 <ul><li>Jared Rosoff ( [email_address] ) </li></ul>
  • 2. Overview <ul><li>Indexing 101 </li></ul><ul><li>Profiling your queries </li></ul><ul><li>Creating Indexes </li></ul><ul><li>Managing Index </li></ul>
  • 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 <ul><li>db.setProfilingLevel( level ) </li></ul><ul><ul><li>0 == off </li></ul></ul><ul><ul><li>1 == slow operations (>100ms) </li></ul></ul><ul><ul><li>2 == all operations </li></ul></ul>
  • 12. Profiler Output <ul><li>db.system.profile.find ({millis:{$gt:5}}); </li></ul><ul><li>{ </li></ul><ul><li>&quot;ts&quot; : &quot;Thu Jan 29 2009 15:21:27 GMT-0500 (EST)&quot; , </li></ul><ul><li>&quot;info&quot; : &quot;query test.foo ntoreturn:0 exception bytes:53&quot; , </li></ul><ul><li>&quot;millis&quot; : 88 </li></ul><ul><li>} </li></ul>
  • 13. Use explain <ul><li>query = db.coll.find({title:”My blog”}) </li></ul><ul><li>query.explain(); </li></ul>{ &quot;cursor&quot; : &quot; BasicCursor &quot;, &quot;indexBounds&quot; : [ ], &quot;nscanned&quot; : 57594, &quot;nscannedObjects&quot; : 57594, &quot;n&quot; : 3, &quot;millis&quot; : 108 }
  • 14. Creating Indexes
  • 15. Index a field <ul><li>db.posts.ensureIndex( { ‘name’: 1 }) </li></ul><ul><li>1 = ascending </li></ul><ul><li>-1 = descending </li></ul>
  • 16. Compound indexes <ul><li>db.posts.ensureIndex({name: 1, date: -1}) </li></ul>
  • 17. Unique Indexes <ul><li>db.posts.ensureIndex({title: 1}, {unique: true}) </li></ul>
  • 18. Embedded documents <ul><li>db.posts.save({ </li></ul><ul><li>title: “My First blog”, </li></ul><ul><li>comments: [ </li></ul><ul><li>{author: “James”, ts : new Date()} ] </li></ul><ul><li>}); </li></ul><ul><li>db.posts.ensureIndex({“ comments.author ” : 1}) </li></ul>
  • 19. Multikeys <ul><li>{ “ tags ” : [ “ mongodb ” , “ cool ” ], ...} </li></ul><ul><li>db.posts.ensureIndex({ “ tags ” : 1}) </li></ul>
  • 20. Covered Indexes <ul><li>New in 1.7.4 </li></ul><ul><li>Query can be resolved in index only </li></ul><ul><li>Need to exclude _id from items projected </li></ul>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 <ul><li>Understand your workload </li></ul><ul><ul><li>Profile your queries </li></ul></ul><ul><ul><li>Use explain on the slow ones </li></ul></ul><ul><li>Create indexes for slow operations </li></ul>

×