Phplx mongodb

1,680 views
1,586 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,680
On SlideShare
0
From Embeds
0
Number of Embeds
457
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Phplx mongodb

  1. 1. MongoDB Indexing and Query Optimization Quinta-feira, 8 de Agosto de 13
  2. 2. Agenda Indexes in MongoDB Optimizing Queries Mistakes Quinta-feira, 8 de Agosto de 13
  3. 3. About me 15 years programming experience Software Developer and MSSQL DBA @ Com- UT/Sedimap playing around with MongoDB about 3 years Quinta-feira, 8 de Agosto de 13
  4. 4. Com-UT/Sedimap an Fleet Management Company (Vehicle track) using MongoDB in Production about 2 years replicaset with 5 nodes about 3.5M of messages arrive from GPS devices and are processed by our communication server (in Node.js) before go to MongoDB can check in http://www.mongodb.org/about/production- deployments/ and find by COMUT Quinta-feira, 8 de Agosto de 13
  5. 5. MongoDB, some wiki font: wikipédia developed and supported by 10gen NoSQL DB, document-oriented database stores structured data as JSON-like documents with dynamic schemas BSON - binary-encoded serialization Quinta-feira, 8 de Agosto de 13
  6. 6. Indexes Quinta-feira, 8 de Agosto de 13
  7. 7. indexes are the single biggest tunable performance factor in MongoDB absent or suboptimal indexes are the most common avoidable MongoDB performance problem Quinta-feira, 8 de Agosto de 13
  8. 8. the application’s queries the relative frequency of each query in the application which indexes the most common queries use Indexing Strategies Quinta-feira, 8 de Agosto de 13
  9. 9. Create indexes db.collection.createIndex({field_name: 1}) db.collection.ensureIndex({field_name: -1}) db.collection.ensureIndex( {field_name: -1}, {background: true} ) Quinta-feira, 8 de Agosto de 13
  10. 10. Manage indexes db.collection.getIndexes() db.collection.getIndexStats({index:”name”}) db.collection.dropIndexes() db.collection.dropIndex(“index_name”) db.collection.reIndex() Quinta-feira, 8 de Agosto de 13
  11. 11. What can be indexed? single field {a: 1} compound keys / multiple fields {a: 1, b: -1} multikeys indexes / arrays of values {a: [1,2,3]} subdocuments "address": { "street": "Main” "zipcode": 53511 "state": "WI" } embedded fields {“address.state”: 1} Quinta-feira, 8 de Agosto de 13
  12. 12. _id index, is the primary key for the collection and every doc must have a unique _id field ObjectId("5038050ef719dd2122000004") Instead of using the ObjectID _id: {a: 1, b: 1, c: 1} _id Quinta-feira, 8 de Agosto de 13
  13. 13. Options Unique, dropDups Sparse indexes Geospatial indexes (2d) TTL collections (expireAfterSeconds) Quinta-feira, 8 de Agosto de 13
  14. 14. Unique & dropDups db.collection.ensureIndex({a: 1}, {unique: true}) db.collection.ensureIndex( {a: 1}, {unique: true, dropDups: true} ) Quinta-feira, 8 de Agosto de 13
  15. 15. Sparse indexes db.collection.ensureIndex({b: 1}, {sparse: true}) db.collection.ensureIndex( {b: 1}, {sparse: true, unique: true} ) Missing fields are stored as null(s) in the index 1 - db.collection.insert({a: 12}) 2 - db.collection.insert({a: 122, b:12}) Attention with sort Quinta-feira, 8 de Agosto de 13
  16. 16. Geospatial indexes db.collection.ensureIndex({loc:“2dsphere”}) { name:“phplx”, loc: { type:“Point”, coordinates: [-9.145858, 38.731103]} } db.collection.find({ loc: {$near: { $geometry: { $type:“Point”, coordinates: [-9.145858, 38.731103] }}}}) Quinta-feira, 8 de Agosto de 13
  17. 17. TTL collections Document must have a BSON UTC Date field { a: 12, b: 455, c: 2323, status: ISODate(“2013-08-08T12:00:00Z”) } db.collection.ensureIndex( {"status": 1}, {expireAfterSeconds: 3600} ) Documents are removed after ‘expireAfterSeconds’ seconds Quinta-feira, 8 de Agosto de 13
  18. 18. Limitations collections can not have > 64 indexes queries can only use one index indexes have storage requirements, and impacts insert/update speed to some degree operator $or Quinta-feira, 8 de Agosto de 13
  19. 19. Optimizing Queries Quinta-feira, 8 de Agosto de 13
  20. 20. Explain - case A > db.events.find().explain() Quinta-feira, 8 de Agosto de 13
  21. 21. Explain - case B > db.events.find({ tmx: ISODate("2012-09-09T17:00:50Z"), mid: 41638, eid: 46511 }).explain() Quinta-feira, 8 de Agosto de 13
  22. 22. Explain - case C db.events.find({ tmx:ISODate("2012-09-09T17:00:50Z"), mid:41638, eid:46511 }, { tmx: 1, mid: 1, eid: 1, _id: 0 }).explain() Quinta-feira, 8 de Agosto de 13
  23. 23. Profiling operations db.setProfilingLevel(level, slowms) level 0 = profiler off 1 = record ops longer than slowms 2 = record all queries db.system.profile.find() the profile collection is a capped collection and fixed in size Quinta-feira, 8 de Agosto de 13
  24. 24. Quinta-feira, 8 de Agosto de 13
  25. 25. Hint a index we can tell the database what index to use db.collection.find({b: {$gt: 120}}) .hint({a: 1}) or tell the database to not use an index db.collection.find({b: {$gt: 120}}) .hint({$natural: 1}) Quinta-feira, 8 de Agosto de 13
  26. 26. The query optimizer for each “type” of query, MongoDB periodically tries all useful indexes aborts the rest as soon as one plan win the winning plan is temporarily cached for each “type” of query Quinta-feira, 8 de Agosto de 13
  27. 27. Mistakes Quinta-feira, 8 de Agosto de 13
  28. 28. trying to use multiple indexes db.collection.ensureIndex({a: 1}) db.collection.ensureIndex({b: 1}) // only one of the above indexes is used db.collection.find({a: 3, b: 10}) Quinta-feira, 8 de Agosto de 13
  29. 29. compound indexes db.collection.ensureIndex({a: 1, b: 1, c: 1}) // can’t use the index db.collection.find({c: 100}) // but this can’t db.collection.find({a: 10, c: 20}) // and this ??? db.collection.find({c: 100}).sort({a: 1}); Quinta-feira, 8 de Agosto de 13
  30. 30. low selectivity indexes db.collection.distinct(“a”) [“java”,“php”,“c++”] db.collection.ensureIndex({a: 1}) // low selectivity provide little benefit db.collection.find({a:“php”}) db.collection.ensureIndex({a: 1, created: 1}) // good db.collection.find({a:“php”}).sort({created: 1}) Quinta-feira, 8 de Agosto de 13
  31. 31. regular expressions db.collection.ensureIndex({a: 1}) // left anchored regex queries can use index db.collection.find({a: /^php/}) // but not generic regex db.collection.find({a: /php/}) // or insensitive db.collection.find({a: /Php/i}) Quinta-feira, 8 de Agosto de 13
  32. 32. negation db.collection.ensureIndex({a: 1}) // not equal db.collection.find({a: {$ne:‘php’}}) // not in db.collection.find({a: {$nin: [‘java’,‘c++’]}}) // $not operador db.collection.ensureIndex({a: {$not:‘c#’}}) Quinta-feira, 8 de Agosto de 13
  33. 33. TO REMEMBER choosing the right indexes is one of the most important things you can do in MongoDB Quinta-feira, 8 de Agosto de 13
  34. 34. THANKYOU Quinta-feira, 8 de Agosto de 13
  35. 35. MongoDB online courses @ 10genEducation are FREE try.... Quinta-feira, 8 de Agosto de 13
  36. 36. QUESTIONS Quinta-feira, 8 de Agosto de 13

×