P. 1
MongoLA - Indexing

MongoLA - Indexing

|Views: 95|Likes:
Published by Alvin John Richards
What is an index, how do they work and when are they used... this talk will show you the in's and out's on indexing in MongoDB.
What is an index, how do they work and when are they used... this talk will show you the in's and out's on indexing in MongoDB.

More info:

Published by: Alvin John Richards on Jan 14, 2011
Copyright:Traditional Copyright: All rights reserved

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

01/14/2011

pdf

text

original

Indexing

Alvin  Richards  -­‐  alvin@10gen.com

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

Indexes Maintain Order
Index  on  a:  ascending

{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}

Indexes Maintain Order
Index  on  a:  ascending,  b:  descending

{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}

B-tree Structure
Index  on  a:  ascending

[-­‐

∞,  5)

[5,  10)

[10,  

∞)

[-­‐

∞,  5)  buckets

[5,  7)

[7,  9)

[9,  10)

[10,  

∞)  buckets

{...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}

Query for {a: 7}
With  Index
[-­‐

∞,  5)

[5,  10)

[10,  

∞)

[-­‐

∞,  5)  buckets

[5,  7)

[7,  9)

[9,  10)

[10,  

∞)  buckets

{...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}

Without  index  -­‐  Scan

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

db.posts.ensureIndex({“name”:  1}) 1  =  ascending -­‐1  =  descending

Compound Indexes

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

Unique Indexes

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

Background Index Creation

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

Indexing Embedded Documents
db.posts.save({    title:  “My  First  blog”,    comments:  [          {author:  “James”,  ts  :  new  Date()}  ] }); db.posts.ensureIndex({“comments.author”:  1})

Multikeys

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

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

Covered indexes
• New in 1.7.4 • Query can resolved in index only • Need to exclude _id from items projected
db.posts.ensureIndex({“title”:  1}) db.posts.find({“title”:  “My  blog  post:},                            {title:  1,  _id:0}))

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.coll.find({a:  0}) db.coll.find({a:  {$in:  [0,  2]}}) db.coll.find({a:  {$gt:  5}}) db.coll.count({a:  0}) db.coll.find().sort({a:  -­‐1}) db.coll.find({a:  0},  {a:1,  _id:0})
Partially:

db.coll.find({b:  0}).sort({a:  -­‐1})

When isn’t an Index Used?
Index  on  {a:  1,  b:  -­‐1}

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

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

   scan terminate    index  on  x

   index  on  y

remember

When are Indexes Needed?

Frequently  used  queries Low  response  time

Indexes Take Up Space

db.collection.totalIndexSize()

Indexes Slow Down Writes

Does my query use an Index?
db.collection.find(query).explain();

Explain - Scan all documents
db.coll.find({title:”My  blog”}).explain(); {        "cursor"  :  "BasicCursor",        "indexBounds"  :  [  ],        "nscanned"  :  57594,        "nscannedObjects"  :  57594,        "n"  :  3,        "millis"  :  108 }

Explain - Index used
db.coll.ensureIndex({title:1}); db.coll.find({title:”My  blog”}).explain(); {        "cursor"  :  "BtreeCursor  x_1",        "indexBounds"  :  [  ],        "nscanned"  :  123,        "nscannedObjects"  :  123,        "n"  :  10,        "millis"  :  4 }

Explain - Covered Index used
db.coll.ensureIndex({title:1}); db.coll.find({title:”My  blog”},                          {title:1,  _id:0}).explain(); {        "cursor"  :  "BtreeCursor  x_1",        "indexBounds"  :  [  ],        "nscanned"  :  123,        "nscannedObjects"  :  123,        "n"  :  10,        "millis"  :  4,        "indexOnly"  :  true }

download at mongodb.org

We’re Hiring !
alvin@10gen.com
conferences,  appearances,  and  meetups
http://www.10gen.com/events

http://bit.ly/mongoF  

Facebook                    |                  Twitter                  |                  LinkedIn
@mongodb

http://linkd.in/joinmongo

W i n   F r e e   L i f e t i m e   H o s t i n g from

Tweet  a  picture  of  ‘(mt)’  somewhere  in  this   office,  with  @mediatemple  and  #mongo_la.   Best  picture  wins!

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->