Indexing
Mike	
  Dirolf	
  -­‐	
  @mdirolf	
  -­‐	
  10gen,	
  Inc.

                                            http://ww...
What’s Easy About
MongoDB Indexing?
 It’s	
  almost	
  the	
  same	
  as	
  in	
  your	
  RDBMS
What’s Hard About
MongoDB Indexing?
 It’s	
  almost	
  the	
  same	
  as	
  in	
  your	
  RDBMS
What is an Index?

   Magic	
  scaling	
  sauce?
What is an Index?

A	
  data	
  structure	
  that	
  can	
  be	
  used	
  to	
  
make	
  certain	
  queries	
  more	
  effic...
Indexes Maintain Order
         Index	
  on	
  {a:	
  1}

       {a:	
  0,	
  b:	
  9}
       {a:	
  2,	
  b:	
  0}
      ...
Indexes Maintain Order
      Index	
  on	
  {a:	
  1,	
  b:	
  -­‐1}

       {a:	
  0,	
  b:	
  9}
       {a:	
  2,	
  b:	...
B-tree Structure
                                   Index	
  on	
  {a:	
  1}

                             [-∞, 5) [5, 10)...
Query for {a: 7}
                                            Index
                             [-∞, 5) [5, 10) [10, ∞)


...
Creating Indexes
     An	
  index	
  on	
  _id	
  is	
  automatic.
         For	
  more,	
  ensureIndex:

db.posts.ensureI...
Compound Indexes


db.posts.ensureIndex({name:	
  1,	
  date:	
  -­‐1})
Unique Indexes


db.posts.ensureIndex({title:	
  1},	
  {unique:	
  true})
Background Builds


db.posts.ensureIndex(...,	
  {background:	
  true})
Indexing Embedded
        Documents

db.posts.ensureIndex({“comments.author”:	
  1})
Multikeys

{“tags”:	
  [“mongodb”,	
  “indexing”],	
  ...}


db.posts.ensureIndex({“tags”:	
  1})
Geospatial


db.posts.ensureIndex({“location”:	
  “2d”})
Listing Indexes


 db.posts.getIndexes()
Dropping an Index


db.posts.dropIndex({“tags”:	
  1})
When is an Index Used?
                 Index	
  on	
  {a:	
  1}
db.collection.find({a:	
  0})
db.collection.find({a:	
  {...
When isn’t an Index Used?
                Index	
  on	
  {a:	
  1,	
  b:	
  -­‐1}

      db.collection.find({b:	
  0})



...
Picking an Index
          find({x:	
  10,	
  y:	
  “foo”})


	
  	
  scan
                                    terminate
	...
When are Indexes
   Needed?
   Frequently	
  used	
  queries
      Low	
  response	
  time
Indexes Take Up
     Space

db.collection.totalIndexSize()
Indexes Slow Down
      Writes
Explain
db.collection.find(query).explain();

{
	
  	
  	
  	
  "cursor"	
  :	
  "BasicCursor",
	
  	
  	
  	
  "indexBoun...
Explain

{
	
  	
  	
  	
  "cursor"	
  :	
  "BtreeCursor	
  x_1",
	
  	
  	
  	
  "indexBounds"	
  :	
  [	
  ],
	
  	
  	
...
www.mongodb.org
Upcoming SlideShare
Loading in...5
×

Indexing

13,467

Published on

0 Comments
17 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
13,467
On Slideshare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
297
Comments
0
Likes
17
Embeds 0
No embeds

No notes for slide

Indexing

  1. 1. Indexing Mike  Dirolf  -­‐  @mdirolf  -­‐  10gen,  Inc. http://www.mongodb.org
  2. 2. What’s Easy About MongoDB Indexing? It’s  almost  the  same  as  in  your  RDBMS
  3. 3. What’s Hard About MongoDB Indexing? It’s  almost  the  same  as  in  your  RDBMS
  4. 4. What is an Index? Magic  scaling  sauce?
  5. 5. What is an Index? A  data  structure  that  can  be  used  to   make  certain  queries  more  efficient.
  6. 6. Indexes Maintain Order Index  on  {a:  1} {a:  0,  b:  9} {a:  2,  b:  0} {a:  3,  b:  2} {a:  3,  b:  7} {a:  3,  b:  5} {a:  7,  b:  1} {a:  9,  b:  1}
  7. 7. Indexes Maintain Order Index  on  {a:  1,  b:  -­‐1} {a:  0,  b:  9} {a:  2,  b:  0} {a:  3,  b:  7} {a:  3,  b:  5} {a:  3,  b:  2} {a:  7,  b:  1} {a:  9,  b:  1}
  8. 8. B-tree Structure Index  on  {a:  1} [-∞, 5) [5, 10) [10, ∞) [-∞, 5) buckets [5, 7) [7, 9) [9, 10) [10, ∞) buckets {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}
  9. 9. Query for {a: 7} Index [-∞, 5) [5, 10) [10, ∞) [-∞, 5) buckets [5, 7) [7, 9) [9, 10) [10, ∞) buckets {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...} Scan
  10. 10. Creating Indexes An  index  on  _id  is  automatic. For  more,  ensureIndex: db.posts.ensureIndex({“name”:  1})
  11. 11. Compound Indexes db.posts.ensureIndex({name:  1,  date:  -­‐1})
  12. 12. Unique Indexes db.posts.ensureIndex({title:  1},  {unique:  true})
  13. 13. Background Builds db.posts.ensureIndex(...,  {background:  true})
  14. 14. Indexing Embedded Documents db.posts.ensureIndex({“comments.author”:  1})
  15. 15. Multikeys {“tags”:  [“mongodb”,  “indexing”],  ...} db.posts.ensureIndex({“tags”:  1})
  16. 16. Geospatial db.posts.ensureIndex({“location”:  “2d”})
  17. 17. Listing Indexes db.posts.getIndexes()
  18. 18. Dropping an Index db.posts.dropIndex({“tags”:  1})
  19. 19. When is an Index Used? Index  on  {a:  1} db.collection.find({a:  0}) db.collection.find({a:  {$in:  [0,  2]}}) db.collection.find({a:  {$gt:  5}}) db.collection.count({a:  0}) db.collection.find().sort({a:  -­‐1}) Partially: db.collection.find({b:  0}).sort({a:  -­‐1})
  20. 20. When isn’t an Index Used? Index  on  {a:  1,  b:  -­‐1} db.collection.find({b:  0}) As  a  rule:  try  imagining  how  the   sorted  representation  could  help  the   server  with  your  query.
  21. 21. Picking an Index find({x:  10,  y:  “foo”})    scan terminate    index  on  x    index  on  y remember
  22. 22. When are Indexes Needed? Frequently  used  queries Low  response  time
  23. 23. Indexes Take Up Space db.collection.totalIndexSize()
  24. 24. Indexes Slow Down Writes
  25. 25. Explain db.collection.find(query).explain(); {        "cursor"  :  "BasicCursor",        "indexBounds"  :  [  ],        "nscanned"  :  57594,        "nscannedObjects"  :  57594,        "n"  :  3  ,        "millis"  :  108 }
  26. 26. Explain {        "cursor"  :  "BtreeCursor  x_1",        "indexBounds"  :  [  ],        "nscanned"  :  123,        "nscannedObjects"  :  123,        "n"  :  10  ,        "millis"  :  4 }
  27. 27. www.mongodb.org
  1. A particular slide catching your eye?

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

×