SlideShare una empresa de Scribd logo
1 de 27
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Mongodb introduction and_internal(simple)
Mongodb introduction and_internal(simple)Mongodb introduction and_internal(simple)
Mongodb introduction and_internal(simple)
Kai Zhao
 
Using MongoDB as a high performance graph database
Using MongoDB as a high performance graph databaseUsing MongoDB as a high performance graph database
Using MongoDB as a high performance graph database
Chris Clarke
 

La actualidad más candente (20)

Indexing with MongoDB
Indexing with MongoDBIndexing with MongoDB
Indexing with MongoDB
 
MongodB Internals
MongodB InternalsMongodB Internals
MongodB Internals
 
MongoDB Performance Tuning
MongoDB Performance TuningMongoDB Performance Tuning
MongoDB Performance Tuning
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World Examples
 
MongoDB at Scale
MongoDB at ScaleMongoDB at Scale
MongoDB at Scale
 
MongoDB: How it Works
MongoDB: How it WorksMongoDB: How it Works
MongoDB: How it Works
 
MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)
 
MongoDB Aggregation Performance
MongoDB Aggregation PerformanceMongoDB Aggregation Performance
MongoDB Aggregation Performance
 
MongoDB - Aggregation Pipeline
MongoDB - Aggregation PipelineMongoDB - Aggregation Pipeline
MongoDB - Aggregation Pipeline
 
MongoDB Fundamentals
MongoDB FundamentalsMongoDB Fundamentals
MongoDB Fundamentals
 
Mongodb introduction and_internal(simple)
Mongodb introduction and_internal(simple)Mongodb introduction and_internal(simple)
Mongodb introduction and_internal(simple)
 
Webinar: Working with Graph Data in MongoDB
Webinar: Working with Graph Data in MongoDBWebinar: Working with Graph Data in MongoDB
Webinar: Working with Graph Data in MongoDB
 
Federated Engine 실무적용사례
Federated Engine 실무적용사례Federated Engine 실무적용사례
Federated Engine 실무적용사례
 
Full Text Search In PostgreSQL
Full Text Search In PostgreSQLFull Text Search In PostgreSQL
Full Text Search In PostgreSQL
 
Introduction to mongodb
Introduction to mongodbIntroduction to mongodb
Introduction to mongodb
 
Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략
 
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
 
Using MongoDB as a high performance graph database
Using MongoDB as a high performance graph databaseUsing MongoDB as a high performance graph database
Using MongoDB as a high performance graph database
 
MongoDB 101
MongoDB 101MongoDB 101
MongoDB 101
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
 

Destacado

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
 

Destacado (7)

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

Similar a 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
 

Similar a 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
 

Más de Mike Dirolf

FrozenRails Training
FrozenRails TrainingFrozenRails Training
FrozenRails Training
Mike Dirolf
 

Más de Mike Dirolf (18)

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)
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
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