Webinar: Indexing and Query Optimization

44
Solutions Architect, 10gen Thomas Boyd Indexing and Query Optimization

description

Having the right indexes in place are crucial to performance in MongoDB. In this talk, we’ll explain how indexes work and the various indexing options. Then, we'll cover the tools available to optimize your queries and avoid common pitfalls. This session will use real-world examples to demonstrate the importance of proper indexing.

Transcript of Webinar: Indexing and Query Optimization

Page 1: Webinar: Indexing and Query Optimization

Solutions Architect, 10gen

Thomas Boyd

Indexing and Query Optimization

Page 2: Webinar: Indexing and Query Optimization

Agenda

• What are indexes?

• Why do I need them?

• Working with indexes in MongoDB

• Optimize your queries

• Avoiding common mistakes

Page 3: Webinar: Indexing and Query Optimization

What are indexes?

Page 4: Webinar: Indexing and Query Optimization

What are indexes?

Imagine you're looking for a recipe in a cookbook ordered by recipe name. Looking up a recipe by name is quick and easy.

Page 5: Webinar: Indexing and Query Optimization

What are indexes?

• How would you find a recipe using chicken?

• How about a 250-350 calorie recipe using chicken?

Page 6: Webinar: Indexing and Query Optimization

KRISTINE TO INSERT IMAGE OF COOKBOOK

Consult the index!

Page 7: Webinar: Indexing and Query Optimization

Linked List

Page 8: Webinar: Indexing and Query Optimization

Finding 7 in Linked List

Page 9: Webinar: Indexing and Query Optimization

Finding 7 in Tree

Page 10: Webinar: Indexing and Query Optimization

Indexes in MongoDB are B-trees

Page 11: Webinar: Indexing and Query Optimization

Queries, inserts and deletes: O(log(n)) time

Page 12: Webinar: Indexing and Query Optimization

Indexes are the single biggest tunable performance factor in MongoDB

Page 13: Webinar: Indexing and Query Optimization

Absent or suboptimal indexes are the most common avoidable MongoDB performance problem.

Page 14: Webinar: Indexing and Query Optimization

Why do I need indexes?A brief story

Page 15: Webinar: Indexing and Query Optimization

Working with Indexes in MongoDB

Page 16: Webinar: Indexing and Query Optimization

// Create an index if one does not existdb.recipes.createIndex({ main_ingredient: 1 })

// The client remembers the index and raises no errorsdb.recipes.ensureIndex({ main_ingredient: 1 })

* 1 means ascending, -1 descending

How do I create indexes?

Page 17: Webinar: Indexing and Query Optimization

// Multiple fields (compound key indexes)db.recipes.ensureIndex({ main_ingredient: 1, calories: -1})

// Arrays of values (multikey indexes){ name: 'Chicken Noodle Soup’, ingredients : ['chicken', 'noodles'] }

db.recipes.ensureIndex({ ingredients: 1 })

What can be indexed?

Page 18: Webinar: Indexing and Query Optimization

// Subdocuments{ name : 'Apple Pie', contributor: { name: 'Joe American', id: 'joea123' }}

db.recipes.ensureIndex({ 'contributor.id': 1 })

db.recipes.ensureIndex({ 'contributor': 1 })

What can be indexed?

Page 19: Webinar: Indexing and Query Optimization

// List a collection's indexes

db.recipes.getIndexes()

db.recipes.getIndexKeys()

// Drop a specific index

db.recipes.dropIndex({ ingredients: 1 })

// Drop all indexes and recreate them

db.recipes.reIndex()

// Default (unique) index on _id

How do I manage indexes?

Page 20: Webinar: Indexing and Query Optimization

// Index creation is a blocking operation that can take a long time

// Background creation yields to other operations

db.recipes.ensureIndex(

{ ingredients: 1 },

{ background: true }

)

Background Index Builds

Page 21: Webinar: Indexing and Query Optimization

Options

• Uniqueness constraints (unique, dropDups)

• Sparse Indexes

• Geospatial (2d) Indexes

• TTL Collections (expireAfterSeconds)

Page 22: Webinar: Indexing and Query Optimization

// Only one recipe can have a given value for name

db.recipes.ensureIndex( { name: 1 }, { unique: true } )

// Force index on collection with duplicate recipe names – drop the duplicates

db.recipes.ensureIndex(

{ name: 1 },

{ unique: true, dropDups: true }

)

* dropDups is probably never what you want

Uniqueness Constraints

Page 23: Webinar: Indexing and Query Optimization

// Only documents with field calories will be indexed

db.recipes.ensureIndex(

{ calories: -1 },

{ sparse: true }

)

// Allow multiple documents to not have calories field

db.recipes.ensureIndex(

{ name: 1 , calories: -1 },

{ unique: true, sparse: true }

)

* Missing fields are stored as null(s) in the index

Sparse Indexes

Page 24: Webinar: Indexing and Query Optimization

// Add GeoJSON with longitude & latitude coordinates

{

name: '10gen Palo Alto’,

loc: { type: “Point”, coordinates: [-122.158574 , 37.449157] }

}

// Index the coordinates

db.locations.ensureIndex( { loc : '2dsphere' } )

// Query for locations 'near' a particular coordinate

db.locations.find({

loc: { $near: {$geometry: {type: “Point”, coordinates: [ -122.3, 37.4] }

})

Geospatial Indexes

Page 25: Webinar: Indexing and Query Optimization

// Documents must have a BSON UTC Date field

{ 'status' : ISODate('2012-10-12T05:24:07.211Z'), … }

// Documents are removed after 'expireAfterSeconds' seconds

db.recipes.ensureIndex(

{ submitted_date: 1 },

{ expireAfterSeconds: 3600 }

)

TTL Collections

Page 26: Webinar: Indexing and Query Optimization

Limitations

• Collections can not have > 64 indexes.

• Index keys can not be > 1024 bytes (1K).

• The name of an index, including the namespace,

must be < 128 characters.

• Queries can only use 1 index*

• Indexes have storage requirements, and impact the

performance of writes.

• In memory sort (no-index) limited to 32mb of return

data.

Page 27: Webinar: Indexing and Query Optimization

Optimize Your Queries

Page 28: Webinar: Indexing and Query Optimization

db.setProfilingLevel( n , slowms=100ms )

n=0 profiler off

n=1 record operations longer than slowms

n=2 record all queries

db.system.profile.find()

* The profile collection is a capped collection, and fixed in size

Profiling Slow Ops

Page 29: Webinar: Indexing and Query Optimization

db.recipes.find( { calories:

{ $lt : 40 } }

).explain( )

{

"cursor" : "BasicCursor" ,

"n" : 42,

"nscannedObjects” : 12345

"nscanned" : 12345,

...

"millis" : 356,

...

}

* Doesn’t use cached plans, re-evals and resets cache

The Explain Plan (Pre Index)

Page 30: Webinar: Indexing and Query Optimization

db.recipes.find( { calories:

{ $lt : 40 } }

).explain( )

{

"cursor" : "BtreeCursor calories_-1" ,

"n" : 42,

"nscannedObjects": 42

"nscanned" : 42,

...

"millis" : 0,

...

}

* Doesn’t use cached plans, re-evals and resets cache

The Explain Plan (Post Index)

Page 31: Webinar: Indexing and Query Optimization

The Query Optimizer

• For each "type" of query, MongoDB periodically tries all useful indexes• Aborts the rest as soon as one plan wins• The winning plan is temporarily cached for each “type” of query

Page 32: Webinar: Indexing and Query Optimization

// Tell the database what index to use

db.recipes.find({

calories: { $lt: 1000 } }

).hint({ _id: 1 })

// Tell the database to NOT use an index

db.recipes.find(

{ calories: { $lt: 1000 } }

).hint({ $natural: 1 })

Manually Select Index to Use

Page 33: Webinar: Indexing and Query Optimization

// Given the following indexdb.collection.ensureIndex({ a:1, b:1 , c:1, d:1 })

// The following query and sort operations can use the indexdb.collection.find( ).sort({ a:1 })db.collection.find( ).sort({ a:1, b:1 })

db.collection.find({ a:4 }).sort({ a:1, b:1 })db.collection.find({ b:5 }).sort({ a:1, b:1 })

Use Indexes to Sort Query Results

Page 34: Webinar: Indexing and Query Optimization

// Given the following index

db.collection.ensureIndex({ a:1, b:1, c:1, d:1 })

// These can not sort using the index

db.collection.find( ).sort({ b: 1 })

db.collection.find({ b: 5 }).sort({ b: 1 })

Indexes that won’t work for sorting query results

Page 35: Webinar: Indexing and Query Optimization

// MongoDB can return data from just the index

db.recipes.ensureIndex({ main_ingredient: 1, name: 1 })

// Return only the ingredients field

db.recipes.find(

{ main_ingredient: 'chicken’ },

{ _id: 0, name: 1 }

)

// indexOnly will be true in the explain plan

db.recipes.find(

{ main_ingredient: 'chicken' },

{ _id: 0, name: 1 }

).explain()

{

"indexOnly": true,

}

Index Covered Queries

Page 36: Webinar: Indexing and Query Optimization

Absent or suboptimal indexes are the most common avoidable MongoDB performance problem.

Page 37: Webinar: Indexing and Query Optimization

Avoiding Common Mistakes

Page 38: Webinar: Indexing and Query Optimization

// MongoDB can only use one index for a query

db.collection.ensureIndex({ a: 1 })

db.collection.ensureIndex({ b: 1 })

// Only one of the above indexes is used

db.collection.find({ a: 3, b: 4 })

Trying to Use Multiple Indexes

Page 39: Webinar: Indexing and Query Optimization

// Compound key indexes are very effective

db.collection.ensureIndex({ a: 1, b: 1, c: 1 })

// But only if the query is a prefix of the index

// This query can't effectively use the index

db.collection.find({ c: 2 })

// …but this query can

db.collection.find({ a: 3, b: 5 })

Compound Key Mistakes

Page 40: Webinar: Indexing and Query Optimization

db.collection.distinct('status’)

[ 'new', 'processed' ]

db.collection.ensureIndex({ status: 1 })

// Low selectivity indexes provide little benefit

db.collection.find({ status: 'new' })

// Better

db.collection.ensureIndex({ status: 1, created_at: -1 })

db.collection.find(

{ status: 'new' }

).sort({ created_at: -1 })

Low Selectivity Indexes

Page 41: Webinar: Indexing and Query Optimization

db.users.ensureIndex({ username: 1 })

// Left anchored regex queries can use the index

db.users.find({ username: /^joe smith/ })

// But not generic regexes

db.users.find({username: /smith/ })

// Or case insensitive queries

db.users.find({ username: /Joe/i })

Regular Expressions

Page 42: Webinar: Indexing and Query Optimization

// Indexes aren't helpful with negationsdb.things.ensureIndex({ x: 1 })

// e.g. "not equal" queries db.things.find({ x: { $ne: 3 } })

// …or "not in" queriesdb.things.find({ x: { $nin: [2, 3, 4 ] } })

// …or the $not operatordb.people.find({ name: { $not: 'John Doe' } })

Negation

Page 43: Webinar: Indexing and Query Optimization

Choosing the right indexes is one of the most important things you can do as a MongoDB developer so take the time to get your indexes right!

Page 44: Webinar: Indexing and Query Optimization

Solutions Architect, 10gen

Thomas Boyd

Thank you