0
Indexing & Query Optimization <ul><li>Jared Rosoff ( [email_address] ) </li></ul>
Overview <ul><li>Indexing 101 </li></ul><ul><li>Profiling your queries  </li></ul><ul><li>Creating Indexes  </li></ul><ul>...
Indexing 101
Ack! My queries are slow! http://michaelprescott.typepad.com/.a/6a00d83451574c69e20134858a87a2970c-800wi
Index FTW! http://musformation.com/pics/excited.jpg
Why did that happen? Magic More Magic
Table scans 1 2 3 4 5 6 7 Looked at 7 objects Find where x equals 7
Tree Lookup 7 6 5 4 3 2 1 Looked at 3 objects Find where x equals 7
O(n) vs. O(log n) Table scan Index Number of records Number of comparisons
Analyzing Query Performance
Using the Profiler <ul><li>db.setProfilingLevel( level )  </li></ul><ul><ul><li>0 == off  </li></ul></ul><ul><ul><li>1 == ...
Profiler Output <ul><li>db.system.profile.find ({millis:{$gt:5}}); </li></ul><ul><li>{ </li></ul><ul><li>&quot;ts&quot; : ...
Use explain <ul><li>query = db.coll.find({title:”My blog”}) </li></ul><ul><li>query.explain(); </li></ul>{ &quot;cursor&qu...
Creating Indexes
Index a field <ul><li>db.posts.ensureIndex( { ‘name’: 1 }) </li></ul><ul><li>1 = ascending </li></ul><ul><li>-1 = descendi...
Compound indexes <ul><li>db.posts.ensureIndex({name: 1, date: -1}) </li></ul>
Unique Indexes <ul><li>db.posts.ensureIndex({title: 1}, {unique: true}) </li></ul>
Embedded documents <ul><li>db.posts.save({ </li></ul><ul><li>title: “My First blog”, </li></ul><ul><li>comments: [  </li><...
Multikeys <ul><li>{ “ tags ” : [ “ mongodb ” ,  “ cool ” ], ...} </li></ul><ul><li>db.posts.ensureIndex({ “ tags ” : 1}) <...
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 exclu...
Sparse Indexes
Geospatial Indexes
Managing Indexes
Listing indexes db.posts.getIndexes()
Dropping indexes db.posts.dropIndex({“tags”: 1})
Background building db.posts.ensureIndex(..., {background: true})
Query Planning
Query Planning
When isn’t an index used?
Picking an Index scan Index on x Index on y Remember Terminate
Review <ul><li>Understand your workload </li></ul><ul><ul><li>Profile your queries  </li></ul></ul><ul><ul><li>Use explain...
Upcoming SlideShare
Loading in...5
×

Indexing & query optimization

2,191

Published on

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,191
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
59
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "Indexing & query optimization"

  1. 1. Indexing & Query Optimization <ul><li>Jared Rosoff ( [email_address] ) </li></ul>
  2. 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. 3. Indexing 101
  4. 4. Ack! My queries are slow! http://michaelprescott.typepad.com/.a/6a00d83451574c69e20134858a87a2970c-800wi
  5. 5. Index FTW! http://musformation.com/pics/excited.jpg
  6. 6. Why did that happen? Magic More Magic
  7. 7. Table scans 1 2 3 4 5 6 7 Looked at 7 objects Find where x equals 7
  8. 8. Tree Lookup 7 6 5 4 3 2 1 Looked at 3 objects Find where x equals 7
  9. 9. O(n) vs. O(log n) Table scan Index Number of records Number of comparisons
  10. 10. Analyzing Query Performance
  11. 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. 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. 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. 14. Creating Indexes
  15. 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. 16. Compound indexes <ul><li>db.posts.ensureIndex({name: 1, date: -1}) </li></ul>
  17. 17. Unique Indexes <ul><li>db.posts.ensureIndex({title: 1}, {unique: true}) </li></ul>
  18. 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. 19. Multikeys <ul><li>{ “ tags ” : [ “ mongodb ” , “ cool ” ], ...} </li></ul><ul><li>db.posts.ensureIndex({ “ tags ” : 1}) </li></ul>
  20. 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. 21. Sparse Indexes
  22. 22. Geospatial Indexes
  23. 23. Managing Indexes
  24. 24. Listing indexes db.posts.getIndexes()
  25. 25. Dropping indexes db.posts.dropIndex({“tags”: 1})
  26. 26. Background building db.posts.ensureIndex(..., {background: true})
  27. 27. Query Planning
  28. 28. Query Planning
  29. 29. When isn’t an index used?
  30. 30. Picking an Index scan Index on x Index on y Remember Terminate
  31. 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>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×