29
Alvin Richards [email protected] Indexing

MongoLA - Indexing

Embed Size (px)

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

Page 1: MongoLA - Indexing

Alvin  Richards  -­‐  [email protected]

Indexing

Page 2: MongoLA - Indexing

What’s Easy About MongoDB Indexing?

It’s  almost  the  same  as  in  your  RDBMS

Page 3: MongoLA - Indexing

What’s Hard About MongoDB Indexing?

It’s  almost  the  same  as  in  your  RDBMS

Page 4: MongoLA - Indexing

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}

Page 5: MongoLA - Indexing

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}

Page 6: MongoLA - Indexing

B-tree StructureIndex  on  a:  ascending

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

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

[10,  ∞)

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

Page 7: MongoLA - Indexing

Query for {a: 7}

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

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

[10,  ∞)

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

With  Index

Without  index  -­‐  Scan

Page 8: MongoLA - Indexing

Creating Indexes

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

1  =  ascending-­‐1  =  descending

An  index  on  _id  is  automatic.

For  more  use  ensureIndex:

Page 9: MongoLA - Indexing

Compound Indexes

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

Page 10: MongoLA - Indexing

Unique Indexes

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

Page 11: MongoLA - Indexing

Background Index Creation

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

Page 12: MongoLA - Indexing

Indexing Embedded Documents

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

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

Page 13: MongoLA - Indexing

Multikeys

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

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

Page 14: MongoLA - Indexing

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

Page 15: MongoLA - Indexing

Geospatial

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

Page 16: MongoLA - Indexing

Listing Indexes

db.posts.getIndexes()

Page 17: MongoLA - Indexing

Dropping an Index

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

Page 18: MongoLA - Indexing

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:

Page 19: MongoLA - Indexing

When isn’t an Index Used?

Index  on  {a:  1,  b:  -­‐1}

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

Page 20: MongoLA - Indexing

Picking an a Index

find({x:  10,  y:  “foo”})

   scan

   index  on  x

   index  on  y remember

terminate

Page 21: MongoLA - Indexing

When are Indexes Needed?

Frequently  used  queriesLow  response  time

Page 22: MongoLA - Indexing

Indexes Take Up Space

db.collection.totalIndexSize()

Page 23: MongoLA - Indexing

Indexes Slow Down Writes

Page 24: MongoLA - Indexing

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

Page 25: MongoLA - Indexing

Explain - Scan all documentsdb.coll.find({title:”My  blog”}).explain();

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

Page 26: MongoLA - Indexing

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();

Page 27: MongoLA - Indexing

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();

Page 28: MongoLA - Indexing

@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 [email protected]

Page 29: MongoLA - Indexing

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!