Schema Chalk Talk
Schema Chalk Talk
Schema Chalk Talk
Alvin Richards
alvin@10gen.com
Common patterns
• Single table inheritance
• One-to-Many & Many-to-Many
• Trees
• Queues
http://www.flickr.com/photos/42304632@N00/493639870/
Goals:
• Avoid anomalies when inserting, updating or deleting
• Minimize redesign when extending the schema
• Make the model informative to users
• Avoid bias towards a particular style of query
* source : wikipedia
Wednesday, May 25, 2011 4
Relational made normalized
data look like this
RDBMS MongoDB
Table Collection
Row(s) JSON
Document
Index Index
Join Embedding
&
Linking
Partition Shard
Partition
Key Shard
Key
shapes table
id type area radius d length width
1 circle 3.14 1
2 square 4 2
3 rect 10 5 2
//
create
index
>
db.shapes.ensureIndex({radius:
1})
Considerations
• Simple to query across sub-types
• Indexes on specialized values will be small
blogs:
{
author
:
"Hergé",
date
:
"Sat
Jul
24
2010
19:47:11
GMT-‐0700
(PDT)",
comments
:
[
{
author
:
"Kyle",
date
:
"Sat
Jul
24
2010
20:51:03
GMT-‐0700
(PDT)",
text
:
"great
book"
}
]}
Wednesday, May 25, 2011 16
One to Many
- Embedded tree
- Single document
- Natural
- Hard to query
blogs:
{
author
:
"Hergé",
date
:
"Sat
Jul
24
2010
19:47:11
GMT-‐0700
(PDT)",
comments
:
[
{
author
:
"Kyle",
date
:
"Sat
Jul
24
2010
20:51:03
GMT-‐0700
(PDT)",
text
:
"great
book",
replies:
[
{
author
:
“James”,
...}
]
}
]}
Embedding
• Simple data structure
• Limited to 16MB
• Larger documents
• How often do you update?
• Will the document grow and grow?
Linking
• More complex data structure
• Unlimited data size
• More, smaller documents
• What are the maintenance needs?
Wednesday, May 25, 2011 27
Trees
Full Tree in Document
{
comments:
[
{
author:
“Kyle”,
text:
“...”,
replies:
[
{author:
“James”,
text:
“...”,
replies:
[]}
]}
]
}
Wednesday, May 25, 2011 28
Trees
Parent Links
- Each node is stored as a document
- Contains the id of the parent
Child Links
- Each node contains the id’s of the children
- Can support graphs (multiple parents / child)
{
comments:
[
{
author:
“Kyle”,
text:
“initial
post”,
path:
“/”
},
{
author:
“Jim”,
text:
“jim’s
comment”,
path:
“/jim”
},
{
author:
“Kyle”,
text:
“Kyle’s
reply
to
Jim”,
path
:
“/jim/kyle”}
]
}
We’re Hiring !
alvin@10gen.com