Indexing & query optimization

31
Indexing & Query Optimization Jared Rosoff ([email protected] )

description

My slides from MongoATL (2-8-2011)

Transcript of Indexing & query optimization

Page 1: Indexing & query optimization

Indexing & Query Optimization

Jared Rosoff ([email protected])

Page 2: Indexing & query optimization

Overview

•Indexing 101

•Profiling your queries

•Creating Indexes

•Managing Index

Page 3: Indexing & query optimization

Indexing 101

Page 4: Indexing & query optimization

Ack! My queries are slow!

http://michaelprescott.typepad.com/.a/6a00d83451574c69e20134858a87a2970c-800wi

Page 5: Indexing & query optimization

Index FTW!

http://musformation.com/pics/excited.jpg

Page 6: Indexing & query optimization

Why did that

happen?Magic

More Magic

Page 7: Indexing & query optimization

Table scans

1 2 3 4 5 6 7

Looked at 7 objects

Find where x equals 7

Page 8: Indexing & query optimization

Tree Lookup

7

6

5

4

3

2

1

Looked at 3 objects

Find where x equals 7

Page 9: Indexing & query optimization

O(n) vs. O(log n)

Table scan

Index

Number of records

Num

ber

of

com

pari

sons

Page 10: Indexing & query optimization

Analyzing Query Performance

Page 11: Indexing & query optimization

Using the Profiler

db.setProfilingLevel( level )

0 == off

1 == slow operations (>100ms)

2 == all operations

Page 12: Indexing & query optimization

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

}

Page 13: Indexing & query optimization

Use explainquery = db.coll.find({title:”My blog”})

query.explain();{ "cursor" : "BasicCursor", "indexBounds" : [ ], "nscanned" : 57594, "nscannedObjects" : 57594, "n" : 3, "millis" : 108}

Page 14: Indexing & query optimization

Creating Indexes

Page 15: Indexing & query optimization

Index a field

db.posts.ensureIndex( { ‘name’: 1 })

1 = ascending

-1 = descending

Page 16: Indexing & query optimization

Compound indexes

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

Page 17: Indexing & query optimization

Unique Indexes

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

Page 18: Indexing & query optimization

Embedded documents

db.posts.save({

title: “My First blog”,

comments: [

{author: “James”, ts : new Date()} ]

});

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

Page 19: Indexing & query optimization

Multikeys{“tags”: [“mongodb”, “cool”], ...}

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

Page 20: Indexing & query optimization

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

Page 21: Indexing & query optimization

Sparse Indexes

Page 22: Indexing & query optimization

Geospatial Indexes

Page 23: Indexing & query optimization

Managing Indexes

Page 24: Indexing & query optimization

Listing indexes

db.posts.getIndexes()

Page 25: Indexing & query optimization

Dropping indexes

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

Page 26: Indexing & query optimization

Background building

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

Page 27: Indexing & query optimization

Query Planning

Page 28: Indexing & query optimization

Query Planning

Page 29: Indexing & query optimization

When isn’t an index used?

Page 30: Indexing & query optimization

Picking an Index

scan

Index on x

Index on yRemember

Terminate

Page 31: Indexing & query optimization

Review

•Understand your workload

•Profile your queries

•Use explain on the slow ones

•Create indexes for slow operations