#AYearWithMongoDB             The Tips            By: @rizkyabdilahEmail: rizky@abdi.la / rizky@mindtalk.com
What is MongoDB?Document databaseSchema-freeMemory mappedOk, the tips..
#1. Avoid RegExpExample case, Mindtalk username:Valid regex ([a-zA-Z0-9_.]+)
No!db.user.ensureIndex({name: 1})db.user.insert({name: “Danny”})db.user.find({name: /dAnny/i})
Yesdb.user.ensureIndex({lower_name: 1})db.user.insert({name: “Danny”,  lower_name: “danny”})db.user.find({lower_name:  “dA...
ComparissonIndex not used when query using RegExp
#2. Tokenize field nameBecause of schema-free, every document  have a own schema (field-name or key in  json)
#2. Tokenize field nameBecause of schema-free, every document  have a own schema (field-name or key in  json)All of your k...
#2. Tokenize field nameBecause of schema-free, every document  have a own schema (field-name or key in  json)All of your k...
Example Case: Ndayak Stream
How we tackle thisOriginal            Now:  Implementation:                    Index size ~ 12GB> 136 million doc         ...
#3. Use single compound-key index Instead of many single-key index
Nodb.post.ensureIndex({user_id: 1, timepos:  -1})db.post.ensureIndex({channel_id: 1,  timepos: -1})Query finddb.post.find(...
Yesdb.post.ensureIndex({user_id:1,  channel_id: 1, timepos: -1})Query find, it will use the same indexdb.post.find({user_i...
Later DiscussionsGlobal write lock (introducing yield on  MongoDB 2.0)Normalize own schemaSharding?
Thank You       Ps: dont forget to join channel#EngineerBlog if you interested in advance :)
Upcoming SlideShare
Loading in …5
×

A Year With MongoDB: The Tips

1,320 views

Published on

Tips for every mongodb user, the tips itself is coming from self experience after 1 (more) year using MongoDB as main database

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,320
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

A Year With MongoDB: The Tips

  1. 1. #AYearWithMongoDB The Tips By: @rizkyabdilahEmail: rizky@abdi.la / rizky@mindtalk.com
  2. 2. What is MongoDB?Document databaseSchema-freeMemory mappedOk, the tips..
  3. 3. #1. Avoid RegExpExample case, Mindtalk username:Valid regex ([a-zA-Z0-9_.]+)
  4. 4. No!db.user.ensureIndex({name: 1})db.user.insert({name: “Danny”})db.user.find({name: /dAnny/i})
  5. 5. Yesdb.user.ensureIndex({lower_name: 1})db.user.insert({name: “Danny”, lower_name: “danny”})db.user.find({lower_name: “dAnny”.toLowerCase()})
  6. 6. ComparissonIndex not used when query using RegExp
  7. 7. #2. Tokenize field nameBecause of schema-free, every document have a own schema (field-name or key in json)
  8. 8. #2. Tokenize field nameBecause of schema-free, every document have a own schema (field-name or key in json)All of your key-name go into your valuable RAM
  9. 9. #2. Tokenize field nameBecause of schema-free, every document have a own schema (field-name or key in json)All of your key-name go into your valuable RAMImagine if youve very-very long key- name, and it repeated in every document. No need to explain
  10. 10. Example Case: Ndayak Stream
  11. 11. How we tackle thisOriginal Now: Implementation: Index size ~ 12GB> 136 million doc Space size ~ 15GBIndex size > 60GB AvgObjSize 112Space size > 24GB bytesAvgObjSize 210
  12. 12. #3. Use single compound-key index Instead of many single-key index
  13. 13. Nodb.post.ensureIndex({user_id: 1, timepos: -1})db.post.ensureIndex({channel_id: 1, timepos: -1})Query finddb.post.find({user_id: “uid”}).sort({timepos: -1})db.post.find({channel_id: “ch_id”}).sort({timepos: -1})
  14. 14. Yesdb.post.ensureIndex({user_id:1, channel_id: 1, timepos: -1})Query find, it will use the same indexdb.post.find({user_id: “uid”}).sort({timepos:-1})db.post.find({channel_id: “chid”}).sort({timepos:-1})
  15. 15. Later DiscussionsGlobal write lock (introducing yield on MongoDB 2.0)Normalize own schemaSharding?
  16. 16. Thank You Ps: dont forget to join channel#EngineerBlog if you interested in advance :)

×