SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDB
MongoDB
 
MongoDB Performance Debugging
MongoDB Performance DebuggingMongoDB Performance Debugging
MongoDB Performance Debugging
MongoDB
 

Tendances (20)

Intro To MongoDB
Intro To MongoDBIntro To MongoDB
Intro To MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Introduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerIntroduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizer
 
Indexing with MongoDB
Indexing with MongoDBIndexing with MongoDB
Indexing with MongoDB
 
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Mongo DB Presentation
Mongo DB PresentationMongo DB Presentation
Mongo DB Presentation
 
MongoDB Fundamentals
MongoDB FundamentalsMongoDB Fundamentals
MongoDB Fundamentals
 
검색엔진이 데이터를 다루는 법 김종민
검색엔진이 데이터를 다루는 법 김종민검색엔진이 데이터를 다루는 법 김종민
검색엔진이 데이터를 다루는 법 김종민
 
Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략
 
Inside MongoDB: the Internals of an Open-Source Database
Inside MongoDB: the Internals of an Open-Source DatabaseInside MongoDB: the Internals of an Open-Source Database
Inside MongoDB: the Internals of an Open-Source Database
 
MongoDB
MongoDBMongoDB
MongoDB
 
Mongodb basics and architecture
Mongodb basics and architectureMongodb basics and architecture
Mongodb basics and architecture
 
Migrating from RDBMS to MongoDB
Migrating from RDBMS to MongoDBMigrating from RDBMS to MongoDB
Migrating from RDBMS to MongoDB
 
MongoDB at Scale
MongoDB at ScaleMongoDB at Scale
MongoDB at Scale
 
MongoDB - Aggregation Pipeline
MongoDB - Aggregation PipelineMongoDB - Aggregation Pipeline
MongoDB - Aggregation Pipeline
 
Json in Postgres - the Roadmap
 Json in Postgres - the Roadmap Json in Postgres - the Roadmap
Json in Postgres - the Roadmap
 
MongoDB World 2015 - A Technical Introduction to WiredTiger
MongoDB World 2015 - A Technical Introduction to WiredTigerMongoDB World 2015 - A Technical Introduction to WiredTiger
MongoDB World 2015 - A Technical Introduction to WiredTiger
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDB
 
MongoDB Performance Debugging
MongoDB Performance DebuggingMongoDB Performance Debugging
MongoDB Performance Debugging
 

En vedette

Availability and scalability in mongo
Availability and scalability in mongoAvailability and scalability in mongo
Availability and scalability in mongo
Md. Khairul Anam
 
Trading up: Adding Flexibility and Scalability to Bouygues Telecom with MongoDB
Trading up: Adding Flexibility and Scalability to Bouygues Telecom with MongoDBTrading up: Adding Flexibility and Scalability to Bouygues Telecom with MongoDB
Trading up: Adding Flexibility and Scalability to Bouygues Telecom with MongoDB
MongoDB
 
MongoDB Basic Concepts
MongoDB Basic ConceptsMongoDB Basic Concepts
MongoDB Basic Concepts
MongoDB
 

En vedette (8)

Availability and scalability in mongo
Availability and scalability in mongoAvailability and scalability in mongo
Availability and scalability in mongo
 
Performance Tuning and Optimization
Performance Tuning and OptimizationPerformance Tuning and Optimization
Performance Tuning and Optimization
 
Trading up: Adding Flexibility and Scalability to Bouygues Telecom with MongoDB
Trading up: Adding Flexibility and Scalability to Bouygues Telecom with MongoDBTrading up: Adding Flexibility and Scalability to Bouygues Telecom with MongoDB
Trading up: Adding Flexibility and Scalability to Bouygues Telecom with MongoDB
 
Agility and Scalability with MongoDB
Agility and Scalability with MongoDBAgility and Scalability with MongoDB
Agility and Scalability with MongoDB
 
MongoDB Basic Concepts
MongoDB Basic ConceptsMongoDB Basic Concepts
MongoDB Basic Concepts
 
MongoDB: How it Works
MongoDB: How it WorksMongoDB: How it Works
MongoDB: How it Works
 
Scaling and Transaction Futures
Scaling and Transaction FuturesScaling and Transaction Futures
Scaling and Transaction Futures
 
Developing with the Modern App Stack: MEAN and MERN (with Angular2 and ReactJS)
Developing with the Modern App Stack: MEAN and MERN (with Angular2 and ReactJS)Developing with the Modern App Stack: MEAN and MERN (with Angular2 and ReactJS)
Developing with the Modern App Stack: MEAN and MERN (with Angular2 and ReactJS)
 

Similaire à Indexing

Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27
MongoDB
 
Mongo db勉強会20110730
Mongo db勉強会20110730Mongo db勉強会20110730
Mongo db勉強会20110730
Akihiro Okuno
 
Indexing and Query Optimisation
Indexing and Query OptimisationIndexing and Query Optimisation
Indexing and Query Optimisation
MongoDB
 
MongoDB (Advanced)
MongoDB (Advanced)MongoDB (Advanced)
MongoDB (Advanced)
TO THE NEW | Technology
 
1403 app dev series - session 5 - analytics
1403   app dev series - session 5 - analytics1403   app dev series - session 5 - analytics
1403 app dev series - session 5 - analytics
MongoDB
 

Similaire à Indexing (20)

Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27
 
Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)
 
Indexing and Query Optimizer
Indexing and Query OptimizerIndexing and Query Optimizer
Indexing and Query Optimizer
 
Mongo indexes
Mongo indexesMongo indexes
Mongo indexes
 
Indexing & query optimization
Indexing & query optimizationIndexing & query optimization
Indexing & query optimization
 
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial IndexesBack to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
 
Indexing In MongoDB
Indexing In MongoDBIndexing In MongoDB
Indexing In MongoDB
 
Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)
 
Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26
 
Indexing documents
Indexing documentsIndexing documents
Indexing documents
 
Mongo db勉強会20110730
Mongo db勉強会20110730Mongo db勉強会20110730
Mongo db勉強会20110730
 
Webinar: Applikationsentwicklung mit MongoDB : Teil 5: Reporting & Aggregation
Webinar: Applikationsentwicklung mit MongoDB: Teil 5: Reporting & AggregationWebinar: Applikationsentwicklung mit MongoDB: Teil 5: Reporting & Aggregation
Webinar: Applikationsentwicklung mit MongoDB : Teil 5: Reporting & Aggregation
 
Indexing and Query Optimisation
Indexing and Query OptimisationIndexing and Query Optimisation
Indexing and Query Optimisation
 
MongoDB (Advanced)
MongoDB (Advanced)MongoDB (Advanced)
MongoDB (Advanced)
 
Building Your First MongoDB Application
Building Your First MongoDB ApplicationBuilding Your First MongoDB Application
Building Your First MongoDB Application
 
1403 app dev series - session 5 - analytics
1403   app dev series - session 5 - analytics1403   app dev series - session 5 - analytics
1403 app dev series - session 5 - analytics
 
Running Production MongoDB Lightning Talk
Running Production MongoDB Lightning TalkRunning Production MongoDB Lightning Talk
Running Production MongoDB Lightning Talk
 
OSDC 2012 | Scaling with MongoDB by Ross Lawley
OSDC 2012 | Scaling with MongoDB by Ross LawleyOSDC 2012 | Scaling with MongoDB by Ross Lawley
OSDC 2012 | Scaling with MongoDB by Ross Lawley
 
Query for json databases
Query for json databasesQuery for json databases
Query for json databases
 
Building DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language WorkbenchBuilding DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language Workbench
 

Plus de Mike Dirolf

FrozenRails Training
FrozenRails TrainingFrozenRails Training
FrozenRails Training
Mike Dirolf
 

Plus de Mike Dirolf (17)

Inside PyMongo - MongoNYC
Inside PyMongo - MongoNYCInside PyMongo - MongoNYC
Inside PyMongo - MongoNYC
 
FrozenRails Training
FrozenRails TrainingFrozenRails Training
FrozenRails Training
 
MongoDB at FrozenRails
MongoDB at FrozenRailsMongoDB at FrozenRails
MongoDB at FrozenRails
 
Python Development (MongoSF)
Python Development (MongoSF)Python Development (MongoSF)
Python Development (MongoSF)
 
MongoDB at ZPUGDC
MongoDB at ZPUGDCMongoDB at ZPUGDC
MongoDB at ZPUGDC
 
MongoDB hearts Django? (Django NYC)
MongoDB hearts Django? (Django NYC)MongoDB hearts Django? (Django NYC)
MongoDB hearts Django? (Django NYC)
 
MongoDB at CodeMash 2.0.1.0
MongoDB at CodeMash 2.0.1.0MongoDB at CodeMash 2.0.1.0
MongoDB at CodeMash 2.0.1.0
 
MongoDB at RubyConf
MongoDB at RubyConfMongoDB at RubyConf
MongoDB at RubyConf
 
MongoDB at RuPy
MongoDB at RuPyMongoDB at RuPy
MongoDB at RuPy
 
MongoDB at RubyEnRails 2009
MongoDB at RubyEnRails 2009MongoDB at RubyEnRails 2009
MongoDB at RubyEnRails 2009
 
MongoDB Strange Loop 2009
MongoDB Strange Loop 2009MongoDB Strange Loop 2009
MongoDB Strange Loop 2009
 
MongoDB Hadoop DC
MongoDB Hadoop DCMongoDB Hadoop DC
MongoDB Hadoop DC
 
MongoDB London PHP
MongoDB London PHPMongoDB London PHP
MongoDB London PHP
 
MongoDB EuroPython 2009
MongoDB EuroPython 2009MongoDB EuroPython 2009
MongoDB EuroPython 2009
 
MongoDB NYC Python
MongoDB NYC PythonMongoDB NYC Python
MongoDB NYC Python
 
MongoDB SF Python
MongoDB SF PythonMongoDB SF Python
MongoDB SF Python
 
MongoDB SF Ruby
MongoDB SF RubyMongoDB SF Ruby
MongoDB SF Ruby
 

Indexing