SlideShare a Scribd company logo
Indexing & Query Optimization Jared Rosoff ( [email_address] )
Overview Indexing 101 Profiling your queries  Creating Indexes  Managing Index
Indexing 101
Ack! My queries are slow! http://michaelprescott.typepad.com/.a/6a00d83451574c69e20134858a87a2970c-800wi
Index FTW! http://musformation.com/pics/excited.jpg
Why did that happen? Magic More Magic
Table scans 1 2 3 4 5 6 7 Looked at 7 objects Find where x equals 7
Tree Lookup 7 6 5 4 3 2 1 Looked at 3 objects Find where x equals 7
O(n) vs. O(log n) Table scan Index Number of records Number of comparisons
Analyzing Query Performance
Using the Profiler db.setProfilingLevel( level )  0 == off  1 == slow operations (>100ms) 2 == all operations
Profiler Output db.system.profile.find ({millis:{$gt:5}}); { "ts" : "Thu Jan 29 2009 15:21:27 GMT-0500 (EST)" ,  "info" : "query test.foo ntoreturn:0 exception  bytes:53" ,  "millis" : 88 }
Use explain query = db.coll.find({title:”My blog”}) query.explain(); { "cursor" : " BasicCursor ", "indexBounds" : [ ], "nscanned" : 57594, "nscannedObjects" : 57594, "n" : 3, "millis" : 108 }
Creating Indexes
Index a field db.posts.ensureIndex( { ‘name’: 1 }) 1 = ascending -1 = descending
Compound indexes db.posts.ensureIndex({name: 1, date: -1})
Unique Indexes db.posts.ensureIndex({title: 1}, {unique: true})
Embedded documents db.posts.save({ title: “My First blog”, comments: [  {author: “James”, ts : new Date()} ] }); db.posts.ensureIndex({“ comments.author ” : 1})
Multikeys { “ tags ” : [ “ mongodb ” ,  “ cool ” ], ...} db.posts.ensureIndex({ “ tags ” : 1})
Covered Indexes New in 1.7.4 Query can be 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}))
Sparse Indexes
Geospatial Indexes
Managing Indexes
Listing indexes db.posts.getIndexes()
Dropping indexes db.posts.dropIndex({“tags”: 1})
Background building db.posts.ensureIndex(..., {background: true})
Query Planning
Query Planning
When isn’t an index used?
Picking an Index scan Index on x Index on y Remember Terminate
Review Understand your workload Profile your queries  Use explain on the slow ones  Create indexes for slow operations

More Related Content

What's hot (20)

PDF
Spock and Geb in Action
Christian Baranowski
 
PDF
Building Real Time Systems on MongoDB Using the Oplog at Stripe
MongoDB
 
PDF
UI components for large amounts of data
Cenarion Information Systems GmbH
 
PPTX
MySql:Basics
DataminingTools Inc
 
PPTX
Config BuildConfig
NexThoughts Technologies
 
PPTX
Grails queries
Husain Dalal
 
PPTX
Unit testing powershell
Matt Wrock
 
KEY
Scala on Your Phone
Michael Galpin
 
PDF
MySQL flexible schema and JSON for Internet of Things
Alexander Rubin
 
PPTX
Cassandra 2.2 & 3.0
Victor Coustenoble
 
PDF
Experiment no 2
Ankit Dubey
 
PPTX
Introduction to the new official C# Driver developed by 10gen
MongoDB
 
PDF
MongoDB With Style
Gabriele Lana
 
PDF
20110514 mongo dbチューニング
Yuichi Matsuo
 
PDF
Using web2py's DAL in other projects or frameworks
Bruno Rocha
 
PDF
Rのスコープとフレームと環境と
Takeshi Arabiki
 
PDF
The Ring programming language version 1.10 book - Part 37 of 212
Mahmoud Samir Fayed
 
PPTX
Code refactoring of existing AutoTest to PageObject pattern
Anton Bogdan
 
Spock and Geb in Action
Christian Baranowski
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
MongoDB
 
UI components for large amounts of data
Cenarion Information Systems GmbH
 
MySql:Basics
DataminingTools Inc
 
Config BuildConfig
NexThoughts Technologies
 
Grails queries
Husain Dalal
 
Unit testing powershell
Matt Wrock
 
Scala on Your Phone
Michael Galpin
 
MySQL flexible schema and JSON for Internet of Things
Alexander Rubin
 
Cassandra 2.2 & 3.0
Victor Coustenoble
 
Experiment no 2
Ankit Dubey
 
Introduction to the new official C# Driver developed by 10gen
MongoDB
 
MongoDB With Style
Gabriele Lana
 
20110514 mongo dbチューニング
Yuichi Matsuo
 
Using web2py's DAL in other projects or frameworks
Bruno Rocha
 
Rのスコープとフレームと環境と
Takeshi Arabiki
 
The Ring programming language version 1.10 book - Part 37 of 212
Mahmoud Samir Fayed
 
Code refactoring of existing AutoTest to PageObject pattern
Anton Bogdan
 

Similar to Indexing & query optimization (20)

PPTX
Indexing In MongoDB
Kishor Parkhe
 
PPTX
Indexing with MongoDB
MongoDB
 
PPTX
MongoDB (Advanced)
TO THE NEW | Technology
 
PDF
Building Apps with MongoDB
Nate Abele
 
PDF
Indexing
Mike Dirolf
 
PPTX
Indexing documents
MongoDB
 
PDF
Mongo indexes
Mehmet Çetin
 
PDF
Indexing and Query Optimizer (Mongo Austin)
MongoDB
 
KEY
Mongo db勉強会20110730
Akihiro Okuno
 
PDF
Latinoware
kchodorow
 
PPTX
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rick Copeland
 
PDF
Full Text Search In PostgreSQL
Karwin Software Solutions LLC
 
PPTX
Mongo db readingdocumentusecases
zarigatongy
 
PDF
PyCon 2010 SQLAlchemy tutorial
jbellis
 
KEY
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
Alex Nguyen
 
PDF
Postgres can do THAT?
alexbrasetvik
 
PDF
MongoDB World 2016: Deciphering .explain() Output
MongoDB
 
PDF
Indexing and Query Optimizer
MongoDB
 
PDF
Fazendo mágica com ElasticSearch
Pedro Franceschi
 
PDF
Mongoseattle indexing-2010-07-27
MongoDB
 
Indexing In MongoDB
Kishor Parkhe
 
Indexing with MongoDB
MongoDB
 
MongoDB (Advanced)
TO THE NEW | Technology
 
Building Apps with MongoDB
Nate Abele
 
Indexing
Mike Dirolf
 
Indexing documents
MongoDB
 
Mongo indexes
Mehmet Çetin
 
Indexing and Query Optimizer (Mongo Austin)
MongoDB
 
Mongo db勉強会20110730
Akihiro Okuno
 
Latinoware
kchodorow
 
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rick Copeland
 
Full Text Search In PostgreSQL
Karwin Software Solutions LLC
 
Mongo db readingdocumentusecases
zarigatongy
 
PyCon 2010 SQLAlchemy tutorial
jbellis
 
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
Alex Nguyen
 
Postgres can do THAT?
alexbrasetvik
 
MongoDB World 2016: Deciphering .explain() Output
MongoDB
 
Indexing and Query Optimizer
MongoDB
 
Fazendo mágica com ElasticSearch
Pedro Franceschi
 
Mongoseattle indexing-2010-07-27
MongoDB
 
Ad

More from Jared Rosoff (9)

PPTX
MongoDB Advanced Schema Design - Inboxes
Jared Rosoff
 
PPTX
Mongosv 2011 - Sharding
Jared Rosoff
 
PPTX
Mongosv 2011 - Replication
Jared Rosoff
 
PPTX
Mongosv 2011 - MongoDB on Amazon EC2
Jared Rosoff
 
PPTX
MongoDB Deployment Tips
Jared Rosoff
 
PPTX
Scaling with mongo db - SF Mongo User Group 7-19-2011
Jared Rosoff
 
PPTX
MongoDB on EC2 and EBS
Jared Rosoff
 
PPTX
Web performance meetup bos 11 18-2010
Jared Rosoff
 
PPTX
Scalable Event Analytics with MongoDB & Ruby on Rails
Jared Rosoff
 
MongoDB Advanced Schema Design - Inboxes
Jared Rosoff
 
Mongosv 2011 - Sharding
Jared Rosoff
 
Mongosv 2011 - Replication
Jared Rosoff
 
Mongosv 2011 - MongoDB on Amazon EC2
Jared Rosoff
 
MongoDB Deployment Tips
Jared Rosoff
 
Scaling with mongo db - SF Mongo User Group 7-19-2011
Jared Rosoff
 
MongoDB on EC2 and EBS
Jared Rosoff
 
Web performance meetup bos 11 18-2010
Jared Rosoff
 
Scalable Event Analytics with MongoDB & Ruby on Rails
Jared Rosoff
 
Ad

Recently uploaded (20)

PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Digital Circuits, important subject in CS
contactparinay1
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 

Indexing & query optimization