Indexing and Query Optimisation

45
Support Engineer, 10gen Australia Stephen Steneker ([email protected]) Indexing and Query Optimisation #MongoDBSydney

Transcript of Indexing and Query Optimisation

  • 1. #MongoDBSydneyIndexing and QueryOptimisationStephen Steneker ([email protected])Support Engineer, 10gen Australia

2. Agenda What are indexes? Why do I need them? Working with indexes in MongoDB Optimise your queries Avoiding common mistakes 3. What are indexes? 4. What are indexes?Imagine youre looking for a recipe in a cookbookordered by recipe name. Looking up a recipe byname is quick and easy. 5. What are indexes? How would you find a recipe using chicken? How about a 250-350 calorie recipe using chicken? 6. KRISTINE TO INSERT IMAGE OF COOKBOOKConsult the index! 7. 1 2 345 6 7Linked List 8. 123 45 6 7Finding 7 in Linked List 9. 42 6135 7Finding 7 in Tree 10. Indexes in MongoDB are B-trees 11. Queries, inserts and deletes: O(log(n)) time 12. Indexes are the singlebiggest tuneableperformance factor inMongoDB 13. Absent or suboptimalindexes are the mostcommon avoidableMongoDB performanceproblem. 14. Why do I need indexes?A brief story 15. Working with Indexes inMongoDB 16. How do I create indexes?// 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 17. What can be indexed?// 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 }) 18. What can be indexed?// Subdocuments{ name : Pavlova, contributor: { name: Ima Aussie, id: ima123 }}db.recipes.ensureIndex({ contributor.id: 1 })db.recipes.ensureIndex({ contributor: 1 }) 19. How do I manage indexes?// List a collections indexesdb.recipes.getIndexes()db.recipes.getIndexKeys()// Drop a specific indexdb.recipes.dropIndex({ ingredients: 1 })// Drop all indexes and recreate themdb.recipes.reIndex()// Default (unique) index on _id 20. Background Index Builds// Index creation is a blocking operation that can take a long time// Background creation yields to other operationsdb.recipes.ensureIndex({ ingredients: 1 },{ background: true }) 21. Options Uniqueness constraints (unique, dropDups) Sparse Indexes Geospatial (2d) Indexes TTL Collections (expireAfterSeconds) 22. Uniqueness Constraints// Only one recipe can have a given value for namedb.recipes.ensureIndex( { name: 1 }, { unique: true } )// Force index on collection with duplicate recipe names drop theduplicatesdb.recipes.ensureIndex({ name: 1 },{ unique: true, dropDups: true })* dropDups is probably never what you want 23. Sparse Indexes// Only documents with field calories will be indexeddb.recipes.ensureIndex({ calories: -1 },{ sparse: true })// Allow multiple documents to not have calories fielddb.recipes.ensureIndex({ name: 1 , calories: -1 },{ unique: true, sparse: true })* Missing fields are stored as null(s) in the index 24. Geospatial Indexes// Add latitude, longitude coordinates{ name: 10gen Sydney, loc: [ 151.21037, -33.88456 ]}// Index the coordinatesdb.locations.ensureIndex( { loc : 2d } )// Query for locations near a particular coordinatedb.locations.find({ loc: { $near: [ 151.21, -33.88 ] }}) 25. TTL Collections// Documents must have a BSON UTC Date field{ status : ISODate(2012-11-09T11:44:07.211Z), }// Documents are removed after expireAfterSeconds secondsdb.recipes.ensureIndex({ submitted_date: 1 },{ expireAfterSeconds: 3600 }) 26. 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