Indexing In MongoDB

22
MongoDB Indexing & Query Optimization

description

 

Transcript of Indexing In MongoDB

Page 1: Indexing In MongoDB

MongoDB

Indexing & Query Optimization

Page 2: Indexing In MongoDB

Overview

• Indexing • Profiling your queries• Creating indexes• Managing indexes

Page 3: Indexing In MongoDB

What is an Index?

A data structure that can used to make certain query more efficient ?

Page 4: Indexing In MongoDB

Table scans

1 2 3 4 5 6 7

Looked at 7 objects

Find where x equals 7

Page 5: Indexing In MongoDB

Tree Lookup

7

6

5

4

3

2

1

Looked at 3 objects

Find where x equals 7

Page 6: Indexing In MongoDB

Use explainquery = db.user.find({title:”Blog”})query.explain();

{ "cursor" : "BasicCursor", "indexBounds" : [ ], "nscanned" : 57594, "nscannedObjects" : 57594, "n" : 3, "millis" : 108}

Page 7: Indexing In MongoDB

Creating Indexes

Page 8: Indexing In MongoDB

Index a field

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

1 = ascending-1 = descending

Test last query with explain method.

Page 9: Indexing In MongoDB

Compound indexes

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

Indexes maintain order index{a:1}{a: 0, b : 1}{a: 1, b : 2}{a: 1, b : 3}{a: 2, b : 1}{a: 3, b : 2}{a: 3, b : 7}{a: 3, b : 9}

Page 10: Indexing In MongoDB

index{a:1,b:-1}{a: 0, b : 1}{a: 1, b : 3}{a: 1, b : 2}{a: 2, b : 1}{a: 3, b : 9}{a: 3, b : 7}{a: 3, b : 2}

Page 11: Indexing In MongoDB

Unique Indexes

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

Page 12: Indexing In MongoDB

Embedded documents

db.user.save({ title: “My First blog”, comments: [ {author: “Ram”, time : new Date()} ]});

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

Page 13: Indexing In MongoDB

Multikeys

{ “tags”: [“mongodb”,

“cool”,”balck], ...}

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

Page 14: Indexing In MongoDB

Covered Indexes

• New in 1.7.4• Query can be resolved in index only• Need to exclude _id from items projected

db.user.ensureIndex({“title”: 1})

db.user.find({“title”: “My blog post:}, {title: 1, _id:0}))

Page 15: Indexing In MongoDB

Sparse Indexes

• Included if and only if indexed values is present• Limited to single field

db.user.ensureIndex({ title : 1} , {sparse :true} )db.user.save({ name : Rohan})db.user.save({ name : Ram , title : princes })Return only Ramdb.user.find ( { title : { $ne : null} } ) .sort({ title : 1} )

Page 16: Indexing In MongoDB

Geospatial Indexes

Page 17: Indexing In MongoDB

Managing Indexes

Page 18: Indexing In MongoDB

Listing indexesdb.user.getIndexes();Dropping indexesdb.user.dropIndex({tag :1})Background Building Indexesdb.user.ensureIndex( { … },{background : true} })

Page 19: Indexing In MongoDB

Indexing Strategies

Page 20: Indexing In MongoDB

Strategies

• The application queries.• The relative frequency of query in application.• Current indexes for your application.• Which indexes most common query use.

Page 21: Indexing In MongoDB

• Use indexes to sort query result• Ensure that your indexes fit entirely in RAM• Consider Performance when Creating Indexes for Write-

heavy Applications

Page 22: Indexing In MongoDB

Review

• Understand your workload– Profile your queries – Use explain on the slow ones

• Create indexes for slow operations