Indexing
Upcoming SlideShare
Loading in...5
×
 

Indexing

on

  • 12,733 views

 

Statistics

Views

Total Views
12,733
Views on SlideShare
5,776
Embed Views
6,957

Actions

Likes
15
Downloads
252
Comments
0

27 Embeds 6,957

http://nosql.mypopescu.com 2740
http://blog.nosqlfan.com 2444
http://rafinguer.blogspot.com 562
http://cloud.csdn.net 538
http://rafinguer.blogspot.com.es 215
http://www.csdn.net 198
http://rafinguer.blogspot.mx 115
http://rafinguer.blogspot.com.ar 51
http://gurjarmehul.wordpress.com 22
http://mongospanish.blogspot.com 19
http://translate.googleusercontent.com 9
http://rafinguer.blogspot.fr 6
http://static.slidesharecdn.com 6
http://webcache.googleusercontent.com 6
http://xue.uplook.cn 5
http://cache.baidu.com 5
http://www.uplook.cn 4
http://m.baidu.com 3
http://rafinguer.blogspot.cz 1
http://rafinguer.blogspot.de 1
http://rafinguer.blogspot.nl 1
http://control.blog.sina.com.cn 1
http://blog.sina.com.cn 1
http://xianguo.com 1
http://reader.youdao.com 1
http://xss.yandex.net 1
http://rafinguer.blogspot.com.br 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Indexing Indexing Presentation Transcript

  • Indexing Mike  Dirolf  -­‐  @mdirolf  -­‐  10gen,  Inc. http://www.mongodb.org
  • 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  efficient.
  • 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}
  • 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}
  • B-tree Structure Index  on  {a:  1} [-∞, 5) [5, 10) [10, ∞) [-∞, 5) buckets [5, 7) [7, 9) [9, 10) [10, ∞) buckets {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}
  • Query for {a: 7} Index [-∞, 5) [5, 10) [10, ∞) [-∞, 5) buckets [5, 7) [7, 9) [9, 10) [10, ∞) buckets {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...}  {...} Scan
  • Creating Indexes An  index  on  _id  is  automatic. For  more,  ensureIndex: db.posts.ensureIndex({“name”:  1})
  • 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:  {$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})
  • 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.
  • Picking an 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
  • Explain db.collection.find(query).explain(); {        "cursor"  :  "BasicCursor",        "indexBounds"  :  [  ],        "nscanned"  :  57594,        "nscannedObjects"  :  57594,        "n"  :  3  ,        "millis"  :  108 }
  • Explain {        "cursor"  :  "BtreeCursor  x_1",        "indexBounds"  :  [  ],        "nscanned"  :  123,        "nscannedObjects"  :  123,        "n"  :  10  ,        "millis"  :  4 }
  • www.mongodb.org