MongoLA - Indexing

Preview:

DESCRIPTION

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.

Citation preview

Alvin  Richards  -­‐  alvin@10gen.com

Indexing

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:  7,  b:  1}

{a:  3,  b:  2}

{a:  3,  b:  5}{a:  3,  b:  7}

{a:  9,  b:  1}

Indexes Maintain Order

Index  on  a:  ascending,  b:  descending

{a:  0,  b:  9}{a:  2,  b:  0}

{a:  7,  b:  1}

{a:  3,  b:  7}

{a:  3,  b:  2}{a:  3,  b:  5}

{a:  9,  b:  1}

B-tree StructureIndex  on  a:  ascending

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

[-­‐∞,  5)[5,  10)

[10,  ∞)

[5,  7) [7,  9) [9,  10)[10,  ∞)  buckets[-­‐∞,  5)  buckets

Query for {a: 7}

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

[-­‐∞,  5)[5,  10)

[10,  ∞)

[5,  7) [7,  9) [9,  10)[10,  ∞)  buckets[-­‐∞,  5)  buckets

With  Index

Without  index  -­‐  Scan

Creating Indexes

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

1  =  ascending-­‐1  =  descending

An  index  on  _id  is  automatic.

For  more  use  ensureIndex:

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

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

Partially:

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

   index  on  x

   index  on  y remember

terminate

When are Indexes Needed?

Frequently  used  queriesLow  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 documentsdb.coll.find({title:”My  blog”}).explain();

{        "cursor"  :  "BasicCursor",        "indexBounds"  :  [  ],        "nscanned"  :  57594,        "nscannedObjects"  :  57594,        "n"  :  3,        "millis"  :  108}

Explain - Index used

{        "cursor"  :  "BtreeCursor  x_1",        "indexBounds"  :  [  ],        "nscanned"  :  123,        "nscannedObjects"  :  123,        "n"  :  10,        "millis"  :  4}

db.coll.ensureIndex({title:1});db.coll.find({title:”My  blog”}).explain();

Explain - Covered Index used

{        "cursor"  :  "BtreeCursor  x_1",        "indexBounds"  :  [  ],        "nscanned"  :  123,        "nscannedObjects"  :  123,        "n"  :  10,        "millis"  :  4,        "indexOnly"  :  true}

db.coll.ensureIndex({title:1});db.coll.find({title:”My  blog”},                          {title:1,  _id:0}).explain();

@mongodb

conferences,  appearances,  and  meetupshttp://www.10gen.com/events

http://bit.ly/mongoF  Facebook                    |                  Twitter                  |                  LinkedIn

http://linkd.in/joinmongo

download at mongodb.org

We’re Hiring !alvin@10gen.com

Win   F r ee   L i f e t ime  Ho s t i ngf r om

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

Best  picture  wins!

Recommended