indexing

27
Mike Dirolf @mdirolf 10gen, Inc. http://www.mongodb.org Indexing

Upload: mike-dirolf

Post on 15-Jan-2015

12.669 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Indexing

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

http://www.mongodb.org

Indexing

Page 2: Indexing

What’s Easy About MongoDB Indexing?

It’s  almost  the  same  as  in  your  RDBMS

Page 3: Indexing

What’s Hard About MongoDB Indexing?

It’s  almost  the  same  as  in  your  RDBMS

Page 4: Indexing

What is an Index?

Magic  scaling  sauce?

Page 5: Indexing

What is an Index?

A  data  structure  that  can  be  used  to  make  certain  queries  more  efficient.

Page 6: Indexing

Indexes Maintain OrderIndex  on  {a:  1}

{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 7: Indexing

Indexes Maintain OrderIndex  on  {a:  1,  b:  -­‐1}

{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 8: Indexing

B-tree StructureIndex  on  {a:  1}

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

[-∞, 5) [5, 10) [10, ∞)

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

Page 9: Indexing

Query for {a: 7}

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

[-∞, 5) [5, 10) [10, ∞)

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

Index

Scan

Page 10: Indexing

Creating Indexes

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

An  index  on  _id  is  automatic.

For  more,  ensureIndex:

Page 11: Indexing

Compound Indexes

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

Page 12: Indexing

Unique Indexes

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

Page 13: Indexing

Background Builds

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

Page 14: Indexing

Indexing Embedded Documents

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

Page 15: Indexing

Multikeys

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

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

Page 16: Indexing

Geospatial

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

Page 17: Indexing

Listing Indexes

db.posts.getIndexes()

Page 18: Indexing

Dropping an Index

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

Page 19: Indexing

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

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

Page 20: Indexing

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.

Page 21: Indexing

Picking an Indexfind({x:  10,  y:  “foo”})

   scan

   index  on  x

   index  on  y remember

terminate

Page 22: Indexing

When are Indexes Needed?

Frequently  used  queries

Low  response  time

Page 23: Indexing

Indexes Take Up Space

db.collection.totalIndexSize()

Page 24: Indexing

Indexes Slow Down Writes

Page 25: Indexing

Explaindb.collection.find(query).explain();

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

Page 26: Indexing

Explain

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