Upcoming SlideShare
×

# Indexing with MongoDB

990 views
841 views

Published on

Published in: Technology
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
990
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
0
0
Likes
2
Embeds 0
No embeds

No notes for slide
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• ### Indexing with MongoDB

1. 1. Indexing with MongoDB Luke EhresmanCopperEgg - www.copperegg.com
2. 2. What is an index?
3. 3. What is an index?• Pointers to documents
4. 4. What is an index?• Pointers to documents• Efﬁciently organized for quick scanning
5. 5. What is an index?• Pointers to documents• Efﬁciently organized for quick scanning• Maintained in a tree structure
6. 6. How do we ﬁnd x = 6 without an index?{x:0} {x:1} {x:2} {x:3} {x:4} {x:5} {x:6} {x:7} {x:8} {x:9}
7. 7. How do we ﬁnd x = 6 without an index?{x:0} {x:1} {x:2} {x:3} {x:4} {x:5} {x:6} {x:7} {x:8} {x:9}
8. 8. How do we ﬁnd x = 6 without an index?{x:0} {x:1} {x:2} {x:3} {x:4} {x:5} {x:6} {x:7} {x:8} {x:9}
9. 9. How do we ﬁnd x = 6 without an index?{x:0} {x:1} {x:2} {x:3} {x:4} {x:5} {x:6} {x:7} {x:8} {x:9}
10. 10. How do we ﬁnd x = 6 without an index?{x:0} {x:1} {x:2} {x:3} {x:4} {x:5} {x:6} {x:7} {x:8} {x:9}
11. 11. How do we ﬁnd x = 6 without an index?{x:0} {x:1} {x:2} {x:3} {x:4} {x:5} {x:6} {x:7} {x:8} {x:9}
12. 12. How do we ﬁnd x = 6 without an index?{x:0} {x:1} {x:2} {x:3} {x:4} {x:5} {x:6} {x:7} {x:8} {x:9}
13. 13. How do we ﬁnd x = 6 without an index?{x:0} {x:1} {x:2} {x:3} {x:4} {x:5} {x:6} {x:7} {x:8} {x:9}
14. 14. How do we ﬁnd x = 6 without an index?{x:0} {x:1} {x:2} {x:3} {x:4} {x:5} {x:6} {x:7} {x:8} {x:9} 6 documents scanned
15. 15. How do we ﬁnd x = 6 with an index?
16. 16. How do we ﬁnd x = 6 with an index? {x:4} < > {x:2} {x:6} < > < >{x:1} {x:3} {x:5} {x:7}
17. 17. How do we ﬁnd x = 6 with an index? {x:4} < > {x:2} {x:6} < > < >{x:1} {x:3} {x:5} {x:7}
18. 18. How do we ﬁnd x = 6 with an index? {x:4} < > {x:2} {x:6} < > < >{x:1} {x:3} {x:5} {x:7} Only 2 documents scanned
19. 19. In reality, it’s even better than that...
20. 20. In reality, it’s even better than that... {x:0, name:‘George’, ....} {x:1, name:‘Jane’, ....} {x:2, name:‘Judy’, ....} {x:3, name:‘Elroy’, ....} {x:4, name:‘Astro’, ....} {x:5, name:‘Rosie’, ....} {x:6, name:‘Fred’, ....} {x:7, name:‘Wilma’, ....} {x:8, name:‘Barney’, ....} {x:9, name:‘Betty’, ....}
21. 21. In reality, it’s even better than that... {x:4} {x:0, name:‘George’, ....} {x:1, name:‘Jane’, ....} < > {x:2, name:‘Judy’, ....} {x:3, name:‘Elroy’, ....} {x:4, name:‘Astro’, ....} {x:5, name:‘Rosie’, ....} {x:2} {x:6} {x:6, name:‘Fred’, ....} {x:7, name:‘Wilma’, ....} < > < > {x:8, name:‘Barney’, ....} {x:9, name:‘Betty’, ....}{x:1} {x:3} {x:5} {x:7}
22. 22. In reality, it’s still even better than that...
23. 23. In reality, it’s still even better than that...•MongoDB stores data and indexes in extents
24. 24. In reality, it’s still even better than that...•MongoDB stores data and indexes in extents•Only loads extents into memory as needed
25. 25. In reality, it’s still even better than that...•MongoDB stores data and indexes in extents•Only loads extents into memory as needed•The full index does not need to be in memory
26. 26. In reality, it’s still even better than that...•MongoDB stores data and indexes in extents•Only loads extents into memory as needed•The full index does not need to be in memory {x:4} < > {x:2} {x:6} < > < > {x:1} {x:3} {x:5} {x:7}
27. 27. When to index?
28. 28. When to index?• Frequently queried ﬁelds
29. 29. When to index?• Frequently queried ﬁelds• Low response time
30. 30. When to index?• Frequently queried ﬁelds• Low response time• Sorting
31. 31. When to index?• Frequently queried ﬁelds• Low response time• Sorting• Avoid full collection scans
32. 32. Things to know...
33. 33. Things to know...• Indexes maintain order
34. 34. Things to know...• Indexes maintain order• Indexes will slow writes
35. 35. Things to know...• Indexes maintain order• Indexes will slow writes• Indexes take up space
36. 36. Things to know...• Indexes maintain order• Indexes will slow writes• Indexes take up space• Index maintenance will cause writes
37. 37. Things to know...• Indexes maintain order• Indexes will slow writes• Indexes take up space• Index maintenance will cause writes• “unique” parameter forces uniqueness
38. 38. How to create an index
39. 39. How to create an index db.people.ensureIndex({...})
40. 40. How to create an index db.people.ensureIndex({...}){_id:1} is already created for each collection
41. 41. How to create an index db.people.ensureIndex({...}) {_id:1} is already created for each collection You can call ensureIndex multiple times(it only creates an index if it doesn’t already exist)
42. 42. How to create an index db.people.ensureIndex({...}) {_id:1} is already created for each collection You can call ensureIndex multiple times(it only creates an index if it doesn’t already exist) Blocks your mongod process unless you specify background:true.
43. 43. Indexes on missing ﬁelds
44. 44. Indexes on missing ﬁeldsnull is assumed if a ﬁeld is missing
45. 45. Indexes on missing ﬁelds null is assumed if a ﬁeld is missingdb.people.insert({name:‘Fred’, children:[‘Pebbles’]});db.people.insert({name:‘Pebbles’})db.people.ensureIndex({children:1})db.people.ﬁnd({children:null});(returns Pebbles and uses the index)
46. 46. Indexes on missing ﬁelds null is assumed if a ﬁeld is missingdb.people.insert({name:‘Fred’, children:[‘Pebbles’]});db.people.insert({name:‘Pebbles’})db.people.ensureIndex({children:1})db.people.ﬁnd({children:null});(returns Pebbles and uses the index) Optional sparse indexes exclude empty ﬁelds for efﬁciency
47. 47. Compound indexes
48. 48. Compound indexes{ name: “George”, age:45, gender:”M”, ...}
49. 49. Compound indexes{ name: “George”, {gender:1, age:-1} age:45, gender:”M”, ...}
50. 50. Compound indexes{ name: “George”, {gender:1, age:-1} age:45, gender:”M”, ...} gender:”M” age:45 {name:‘George’, ....} age:12 {name:‘Jane’, ....} age:3 {name:‘Judy’, ....} gender:”F” {name:‘Elroy’, ....} age:44 {name:‘Astro’, ....} age:16 {name:‘Rosie’, ....} age:10
51. 51. Indexes on arrays
52. 52. Indexes on arrays{ name: “George”, age:45, gender:”M”, children: [‘Judy’, ‘Elroy’] ...}
53. 53. Indexes on arrays{ name: “George”, age:45, {children:1} gender:”M”, children: [‘Judy’, ‘Elroy’] ...}
54. 54. Indexes on arrays{ name: “George”, age:45, {children:1} gender:”M”, children: [‘Judy’, ‘Elroy’] ...} Actually creates a new entry in the index for each element of the array.
55. 55. Indexes on arrays { name: “George”, age:45, {children:1} gender:”M”, children: [‘Judy’, ‘Elroy’] ... } Actually creates a new entry in the index for each element of the array.children:‘Judy’ {name:‘George’, ....}children:‘Elroy’ {name:‘Fred’, ...}children:‘Bam-Bam’ {name:‘Barney’ ...}
56. 56. Covered indexes
57. 57. Covered indexesQueries that can be resolved with only the index(does not need to fetch the original document)
58. 58. Covered indexesQueries that can be resolved with only the index(does not need to fetch the original document){ name: “George”, age:45, gender:”M”, children: [‘Judy’, ‘Elroy’] ...}
59. 59. Covered indexesQueries that can be resolved with only the index(does not need to fetch the original document){ name: “George”, age:45, db.people.ensureIndex({name:1, age:1}); gender:”M”, db.people.ﬁnd({name:‘George’}, {_id:0, age:1}); children: [‘Judy’, ‘Elroy’] ...}
60. 60. How does it know which index to use?
61. 61. How does it know which index to use?db.people.ensureIndex({name:1});db.people.ensureIndex({age:1});db.people.ﬁnd({age:{\$gte:21}});
62. 62. How does it know which index to use?db.people.ensureIndex({name:1});db.people.ensureIndex({age:1});db.people.ﬁnd({age:{\$gte:21}}); scan index on name index on age
63. 63. How does it know which index to use?db.people.ensureIndex({name:1});db.people.ensureIndex({age:1});db.people.ﬁnd({age:{\$gte:21}}); scan terminated index on name index on age remember
64. 64. Query performance analysisdb.people.ﬁnd({name:‘Fred’}).explain(){ “cursor” : “BasicCursor”, “indexBounds” : {}, “nscanned” : 88731, “nscannedObjects” : 88731, “n” : 1, “millis” : 108}
65. 65. Query performance analysisdb.people.ensureIndex({name:1})db.people.ﬁnd({name:‘Fred’}).explain(){ “cursor” : “BtreeCursor name_1”, ...}
66. 66. Query performance analysisdb.people.ensureIndex({name:1})db.people.ﬁnd({name:‘Fred’}).explain(){ “cursor” : “BtreeCursor name_1”, ...} Demo
67. 67. Credits• Much of this information was gleaned from these two presentations:• http://speakerdeck.com/u/mongodb/p/ indexing-and-query-optimizer-kevin- hanson-10gen• http://www.slideshare.net/mongodb/ indexing-with-mongodb