Indexing & query optimization
-
Upload
jared-rosoff -
Category
Technology
-
view
2.124 -
download
0
description
Transcript of Indexing & query optimization
![Page 2: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/2.jpg)
Overview
•Indexing 101
•Profiling your queries
•Creating Indexes
•Managing Index
![Page 3: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/3.jpg)
Indexing 101
![Page 4: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/4.jpg)
Ack! My queries are slow!
http://michaelprescott.typepad.com/.a/6a00d83451574c69e20134858a87a2970c-800wi
![Page 5: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/5.jpg)
Index FTW!
http://musformation.com/pics/excited.jpg
![Page 6: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/6.jpg)
Why did that
happen?Magic
More Magic
![Page 7: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/7.jpg)
Table scans
1 2 3 4 5 6 7
Looked at 7 objects
Find where x equals 7
![Page 8: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/8.jpg)
Tree Lookup
7
6
5
4
3
2
1
Looked at 3 objects
Find where x equals 7
![Page 9: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/9.jpg)
O(n) vs. O(log n)
Table scan
Index
Number of records
Num
ber
of
com
pari
sons
![Page 10: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/10.jpg)
Analyzing Query Performance
![Page 11: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/11.jpg)
Using the Profiler
db.setProfilingLevel( level )
0 == off
1 == slow operations (>100ms)
2 == all operations
![Page 12: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/14.jpg)
Creating Indexes
![Page 15: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/15.jpg)
Index a field
db.posts.ensureIndex( { ‘name’: 1 })
1 = ascending
-1 = descending
![Page 16: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/16.jpg)
Compound indexes
db.posts.ensureIndex({name: 1, date: -1})
![Page 17: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/17.jpg)
Unique Indexes
db.posts.ensureIndex({title: 1}, {unique: true})
![Page 18: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/19.jpg)
Multikeys{“tags”: [“mongodb”, “cool”], ...}
db.posts.ensureIndex({“tags”: 1})
![Page 20: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/21.jpg)
Sparse Indexes
![Page 22: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/22.jpg)
Geospatial Indexes
![Page 23: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/23.jpg)
Managing Indexes
![Page 24: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/24.jpg)
Listing indexes
db.posts.getIndexes()
![Page 25: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/25.jpg)
Dropping indexes
db.posts.dropIndex({“tags”: 1})
![Page 26: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/26.jpg)
Background building
db.posts.ensureIndex(..., {background: true})
![Page 27: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/27.jpg)
Query Planning
![Page 28: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/28.jpg)
Query Planning
![Page 29: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/29.jpg)
When isn’t an index used?
![Page 30: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/30.jpg)
Picking an Index
scan
Index on x
Index on yRemember
Terminate
![Page 31: Indexing & query optimization](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b6ea914a7959b35f8b4752/html5/thumbnails/31.jpg)
Review
•Understand your workload
•Profile your queries
•Use explain on the slow ones
•Create indexes for slow operations