Your SlideShare is downloading. ×
0
#mongodbdays       Schema Design       Emily Stolfo       Ruby Engineer/Evangelist, 10gen       @EmStolfoTuesday, January ...
Agenda       • Working with documents       • Common patterns       • Evolving a Schema       • Queries and IndexesTuesday...
RDBMS              MongoDB       Database           ➜ Database       Table              ➜ Collection       Row            ...
Working with DocumentsTuesday, January 29, 13
Documents       Provide flexibility and       performanceTuesday, January 29, 13
Example Schema (MongoDB)Tuesday, January 29, 13
Embedding       Example Schema (MongoDB)Tuesday, January 29, 13
Embedding                          Linking       Example Schema (MongoDB)Tuesday, January 29, 13
Relational Schema Design       Focuses on data storageTuesday, January 29, 13
Document Schema Design       Focuses on data useTuesday, January 29, 13
Schema Design Considerations    • What is a priority?           – High consistency           – High read performance      ...
Tools for Data Access       • Flexible Schemas       • Embedded data structures       • Secondary Indexes       • Multi-Ke...
Data Manipulation       • Conditional Query Operators             – Scalar: $ne, $mod, $exists, $type, $lt, $lte, $gt, $gt...
Schema Design ExampleTuesday, January 29, 13
Library Management Application       • Patrons       • Books       • Authors       • PublishersTuesday, January 29, 13
One to One Relations       exampleTuesday, January 29, 13
Modeling Patrons       patron = {                   patron = {         _id: "joe"                   _id: "joe"         nam...
One to One Relations    • “Contains” relationships are often embedded.    • Document provides a holistic representation of...
One To Many Relations       examplesTuesday, January 29, 13
Patrons with many addresses       patron = {         _id: "joe"         name: "Joe Bookreader",         join_date: ISODate...
One to Many Relations       example 2       Publishers and BooksTuesday, January 29, 13
Publishers and Books relation       • Publishers put out many books       • Books have one publisherTuesday, January 29, 13
Book Data       MongoDB: The Definitive Guide,       By Kristina Chodorow and Mike Dirolf       Published: 9/24/2010      ...
Book Model with Embedded Publisher       book = {         title: "MongoDB: The Definitive Guide",         authors: [ "Kris...
Book Model with Normalized Publisher       publisher = {         name: "O’Reilly Media",         founded: "1980",         ...
Link with Publisher _id as a Reference       publisher = {         _id: "oreilly",         name: "O’Reilly Media",        ...
Link with Book _ids as a Reference       publisher = {         name: "O’Reilly Media",         founded: "1980",         lo...
Where do you put the reference?       • Reference to single publisher on books             – Use when items have unbounded...
One to Many Relations       example 3       Books and PatronsTuesday, January 29, 13
Books and Patrons       • Book can be checked out by one Patron at a time       • Patrons can check out many books (but no...
Modeling Checkouts       patron = {         _id: "joe"         name: "Joe Bookreader",         join_date: ISODate("2011-10...
Modeling Checkouts         patron = {           _id: "joe"           name: "Joe Bookreader",           join_date: ISODate(...
De-normalization       Provides data localityTuesday, January 29, 13
Modeling Checkouts - de-normalized       patron = {         _id: "joe"         name: "Joe Bookreader",         join_date: ...
Referencing vs. Embedding      • Embedding is a bit like pre-joining data      • Document level operations are easy for th...
Many to Many Relations       exampleTuesday, January 29, 13
Books and Authors       book = {         title: "MongoDB: The Definitive Guide",         published_date: ISODate("2010-09-...
Relation stored in Book document       book = {         title: "MongoDB: The Definitive Guide",         authors : [       ...
Relation stored in Author document       book = {         _id: 123456789         title: "MongoDB: The Definitive Guide",  ...
Relation stored in both documents       book = {         _id: 123456789         title: "MongoDB: The Definitive Guide",   ...
Where do you put the reference?     Think about common queries       book = {         title: "MongoDB: The Definitive Guid...
Where do you put the reference?     Think about indexes       book = {         title: "MongoDB: The Definitive Guide",    ...
Trees       exampleTuesday, January 29, 13
Parent References       book = {         title: "MongoDB: The Definitive Guide",         authors: [ "Kristina Chodorow", "...
Child References       book = {         _id: 123456789,         title: "MongoDB: The Definitive Guide",         authors: [...
Modeling Trees       • Parent References            - Each node is stored as a document            - Contains the id of th...
Array of Ancestors        book = {          title: "MongoDB: The Definitive Guide",          authors: [ "Kristina Chodorow...
Single Table Inheritance       exampleTuesday, January 29, 13
Single Table Inheritance            book = {              title: "MongoDB: The Definitive Guide",              authors: [ ...
Queues       exampleTuesday, January 29, 13
Update highest priority request       db.loans.insert({            _id: 123456789,            book_id: 987654321,         ...
Summary       • Schema design is different in MongoDB       • Basic data design principals apply       • Focus on how appl...
#mongodbdays       Thank You       Emily Stolfo       Ruby Engineer/Evangelist, 10gen       @EmStolfoTuesday, January 29, 13
Upcoming SlideShare
Loading in...5
×

Schema Design

1,126

Published on

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

No Downloads
Views
Total Views
1,126
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
33
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "Schema Design"

  1. 1. #mongodbdays Schema Design Emily Stolfo Ruby Engineer/Evangelist, 10gen @EmStolfoTuesday, January 29, 13
  2. 2. Agenda • Working with documents • Common patterns • Evolving a Schema • Queries and IndexesTuesday, January 29, 13
  3. 3. RDBMS MongoDB Database ➜ Database Table ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference TerminologyTuesday, January 29, 13
  4. 4. Working with DocumentsTuesday, January 29, 13
  5. 5. Documents Provide flexibility and performanceTuesday, January 29, 13
  6. 6. Example Schema (MongoDB)Tuesday, January 29, 13
  7. 7. Embedding Example Schema (MongoDB)Tuesday, January 29, 13
  8. 8. Embedding Linking Example Schema (MongoDB)Tuesday, January 29, 13
  9. 9. Relational Schema Design Focuses on data storageTuesday, January 29, 13
  10. 10. Document Schema Design Focuses on data useTuesday, January 29, 13
  11. 11. Schema Design Considerations • What is a priority? – High consistency – High read performance – High write performance • How does the application access and manipulate data? – Read/Write Ratio – Types of Queries / Updates – Data life-cycle and growth – Analytics (Map Reduce, Aggregation)Tuesday, January 29, 13
  12. 12. Tools for Data Access • Flexible Schemas • Embedded data structures • Secondary Indexes • Multi-Key Indexes • Aggregation Framework – Pipeline operators: $project, $match, $limit, $skip, $sort, $group, $unwind • No JoinsTuesday, January 29, 13
  13. 13. Data Manipulation • Conditional Query Operators – Scalar: $ne, $mod, $exists, $type, $lt, $lte, $gt, $gte, $ne – Vector: $in, $nin, $all, $size • Atomic Update Operators – Scalar: $inc, $set, $unset – Vector: $push, $pop, $pull, $pushAll, $pullAll, $addToSetTuesday, January 29, 13
  14. 14. Schema Design ExampleTuesday, January 29, 13
  15. 15. Library Management Application • Patrons • Books • Authors • PublishersTuesday, January 29, 13
  16. 16. One to One Relations exampleTuesday, January 29, 13
  17. 17. Modeling Patrons patron = { patron = { _id: "joe" _id: "joe" name: "Joe Bookreader” name: "Joe Bookreader", } address: { address = { street: "123 Fake St. ", patron_id = "joe", city: "Faketon", street: "123 Fake St. ", state: "MA", city: "Faketon", zip: 12345 state: "MA", zip: 12345 } } }Tuesday, January 29, 13
  18. 18. One to One Relations • “Contains” relationships are often embedded. • Document provides a holistic representation of objects with embedded entities. • Optimized read performance.Tuesday, January 29, 13
  19. 19. One To Many Relations examplesTuesday, January 29, 13
  20. 20. Patrons with many addresses patron = { _id: "joe" name: "Joe Bookreader", join_date: ISODate("2011-10-15"), addresses: [ {street: "1 Vernon St.", city: "Newton", state: "MA", …}, {street: "52 Main St.", city: "Boston", state: "MA", …}, ] }Tuesday, January 29, 13
  21. 21. One to Many Relations example 2 Publishers and BooksTuesday, January 29, 13
  22. 22. Publishers and Books relation • Publishers put out many books • Books have one publisherTuesday, January 29, 13
  23. 23. Book Data MongoDB: The Definitive Guide, By Kristina Chodorow and Mike Dirolf Published: 9/24/2010 Pages: 216 Language: English Publisher: O’Reilly Media, CATuesday, January 29, 13
  24. 24. Book Model with Embedded Publisher book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ] published_date: ISODate("2010-09-24"), pages: 216, language: "English", publisher: { name: "O’Reilly Media", founded: "1980", location: "CA" } }Tuesday, January 29, 13
  25. 25. Book Model with Normalized Publisher publisher = { name: "O’Reilly Media", founded: "1980", location: "CA" } book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ] published_date: ISODate("2010-09-24"), pages: 216, language: "English" }Tuesday, January 29, 13
  26. 26. Link with Publisher _id as a Reference publisher = { _id: "oreilly", name: "O’Reilly Media", founded: "1980", location: "CA" } book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ] published_date: ISODate("2010-09-24"), pages: 216, language: "English", publisher_id: "oreilly" }Tuesday, January 29, 13
  27. 27. Link with Book _ids as a Reference publisher = { name: "O’Reilly Media", founded: "1980", location: "CA" books: [ "123456789", ... ] } book = { _id: "123456789", title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ] published_date: ISODate("2010-09-24"), pages: 216, language: "English" }Tuesday, January 29, 13
  28. 28. Where do you put the reference? • Reference to single publisher on books – Use when items have unbounded growth (unlimited # of books) • Array of books in publisher document – Optimal when many means a handful of items – Use when there is a bound on potential growthTuesday, January 29, 13
  29. 29. One to Many Relations example 3 Books and PatronsTuesday, January 29, 13
  30. 30. Books and Patrons • Book can be checked out by one Patron at a time • Patrons can check out many books (but not 1000s)Tuesday, January 29, 13
  31. 31. Modeling Checkouts patron = { _id: "joe" name: "Joe Bookreader", join_date: ISODate("2011-10-15"), address: { ... } } book = { _id: "123456789" title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], ... }Tuesday, January 29, 13
  32. 32. Modeling Checkouts patron = { _id: "joe" name: "Joe Bookreader", join_date: ISODate("2011-10-15"), address: { ... }, checked_out: [ { _id: "123456789", checked_out: "2012-10-15" }, { _id: "987654321", checked_out: "2012-09-12" }, ... ] }Tuesday, January 29, 13
  33. 33. De-normalization Provides data localityTuesday, January 29, 13
  34. 34. Modeling Checkouts - de-normalized patron = { _id: "joe" name: "Joe Bookreader", join_date: ISODate("2011-10-15"), address: { ... }, checked_out: [ { _id: "123456789", title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], checked_out: ISODate("2012-10-15") }, { _id: "987654321" title: "MongoDB: The Scaling Adventure", ... }, ... ] }Tuesday, January 29, 13
  35. 35. Referencing vs. Embedding • Embedding is a bit like pre-joining data • Document level operations are easy for the server to handle • Embed when the “many” objects always appear with (viewed in the context of) their parents. • Reference when you need more flexibility How does your application access and manipulate data?Tuesday, January 29, 13
  36. 36. Many to Many Relations exampleTuesday, January 29, 13
  37. 37. Books and Authors book = { title: "MongoDB: The Definitive Guide", published_date: ISODate("2010-09-24"), pages: 216, language: "English" } author = { _id: "kchodorow", name: "Kristina Chodorow", hometown: "New York" } author = { _id: "mdirolf", name: "Mike Dirolf", hometown: "Albany" }Tuesday, January 29, 13
  38. 38. Relation stored in Book document book = { title: "MongoDB: The Definitive Guide", authors : [ { _id: "kchodorow", name: "Kristina Chodorow” }, { _id: "mdirolf", name: "Mike Dirolf” } ] published_date: ISODate("2010-09-24"), pages: 216, language: "English" } author = { _id: "kchodorow", name: "Kristina Chodorow", hometown: "New York" } author = { _id: "mdirolf", name: "Mike Dirolf", hometown: "Albany" }Tuesday, January 29, 13
  39. 39. Relation stored in Author document book = { _id: 123456789 title: "MongoDB: The Definitive Guide", published_date: ISODate("2010-09-24"), pages: 216, language: "English" } author = { _id: "kchodorow", name: "Kristina Chodorow", hometown: "Cincinnati", books: [ {book_id: 123456789, title : "MongoDB: The Definitive Guide" }] }Tuesday, January 29, 13
  40. 40. Relation stored in both documents book = { _id: 123456789 title: "MongoDB: The Definitive Guide", authors = [ kchodorow, mdirolf ] published_date: ISODate("2010-09-24"), pages: 216, language: "English" } author = { _id: "kchodorow", name: "Kristina Chodorow", hometown: "New York", books: [ 123456789, ... ] } author = { _id: "mdirolf", name: "Mike Dirolf", hometown: "Albany", books: [ 123456789, ... ] }Tuesday, January 29, 13
  41. 41. Where do you put the reference? Think about common queries book = { title: "MongoDB: The Definitive Guide", authors : [ { _id: "kchodorow", name: "Kristina Chodorow” }, { _id: "mdirolf", name: "Mike Dirolf” } ] published_date: ISODate("2010-09-24"), pages: 216, language: "English" } author = { _id: "kchodorow", name: "Kristina Chodorow", hometown: "New York" } db.books.find( { authors.name : "Kristina Chodorow" } )Tuesday, January 29, 13
  42. 42. Where do you put the reference? Think about indexes book = { title: "MongoDB: The Definitive Guide", authors : [ { _id: "kchodorow", name: "Kristina Chodorow” }, { _id: "mdirolf", name: "Mike Dirolf” } ] published_date: ISODate("2010-09-24"), pages: 216, language: "English" } author = { _id: "kchodorow", name: "Kristina Chodorow", hometown: "New York" } db.books.createIndex( { authors.name : 1 } )Tuesday, January 29, 13
  43. 43. Trees exampleTuesday, January 29, 13
  44. 44. Parent References book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", category: "MongoDB" } category = { _id: MongoDB, parent: Databases } category = { _id: Databases, parent: Programming }Tuesday, January 29, 13
  45. 45. Child References book = { _id: 123456789, title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English" } category = { _id: MongoDB, children: [ 123456789, … ] } category = { _id: Databases, children: [ MongoDB, Postgres } category = { _id: Programming, children: [ Databases, Languages ] }Tuesday, January 29, 13
  46. 46. Modeling Trees • Parent References - Each node is stored as a document - Contains the id of the parent • Child References - Each node contains ids of its children - Can support graphs (multiple parents / child)Tuesday, January 29, 13
  47. 47. Array of Ancestors book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", categories: [ Programming, Databases, MongoDB ] } book = { title: "MySQL: The Definitive Guide", authors: [ "Michael Kofler" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", parent: "MySQL", ancestors: [ Programming, Databases, MySQL ] }Tuesday, January 29, 13
  48. 48. Single Table Inheritance exampleTuesday, January 29, 13
  49. 49. Single Table Inheritance book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ] published_date: ISODate("2010-09-24"), kind: loanable locations: [ ... ] pages: 216, language: "English", publisher: { name: "O’Reilly Media", founded: "1980", location: "CA" } }Tuesday, January 29, 13
  50. 50. Queues exampleTuesday, January 29, 13
  51. 51. Update highest priority request db.loans.insert({ _id: 123456789, book_id: 987654321, pending: false, approved: false, priority: 3 }) //Find the highest priority request and mark as pending approval request = db.loans.findAndModify({ query: { pending: false, book_id: 987654321 }, sort: { priority: -1}, update: { $set: { pending : true, started: new ISODate() } } })Tuesday, January 29, 13
  52. 52. Summary • Schema design is different in MongoDB • Basic data design principals apply • Focus on how application accesses and manipulates data • Evolve schema to meet changing requirements • Application-level logic is important!Tuesday, January 29, 13
  53. 53. #mongodbdays Thank You Emily Stolfo Ruby Engineer/Evangelist, 10gen @EmStolfoTuesday, January 29, 13
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×