23. In reality, it’s still even better than that...
•MongoDB stores data and indexes in extents
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. 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. 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}
35. Things to know...
• Indexes maintain order
• Indexes will slow writes
• Indexes take up space
36. Things to know...
• Indexes maintain order
• Indexes will slow writes
• Indexes take up space
• Index maintenance will cause writes
37. Things to know...
• Indexes maintain order
• Indexes will slow writes
• Indexes take up space
• Index maintenance will cause writes
• “unique” parameter forces uniqueness
40. How to create an index
db.people.ensureIndex({...})
{_id:1} is already created for each collection
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. 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.
45. Indexes on missing fields
null is assumed if a field is missing
db.people.insert({name:‘Fred’, children:[‘Pebbles’]});
db.people.insert({name:‘Pebbles’})
db.people.ensureIndex({children:1})
db.people.find({children:null});
(returns Pebbles and uses the index)
46. Indexes on missing fields
null is assumed if a field is missing
db.people.insert({name:‘Fred’, children:[‘Pebbles’]});
db.people.insert({name:‘Pebbles’})
db.people.ensureIndex({children:1})
db.people.find({children:null});
(returns Pebbles and uses the index)
Optional sparse indexes exclude
empty fields for efficiency
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. 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’ ...}
58. Covered indexes
Queries 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. Covered indexes
Queries 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.find({name:‘George’}, {_id:0, age:1});
children: [‘Judy’, ‘Elroy’]
...
}
61. How does it know which index to use?
db.people.ensureIndex({name:1});
db.people.ensureIndex({age:1});
db.people.find({age:{$gte:21}});
62. How does it know which index to use?
db.people.ensureIndex({name:1});
db.people.ensureIndex({age:1});
db.people.find({age:{$gte:21}});
scan
index on name
index on age
63. How does it know which index to use?
db.people.ensureIndex({name:1});
db.people.ensureIndex({age:1});
db.people.find({age:{$gte:21}});
scan
terminated
index on name
index on age remember
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