Graphs provide an alternative to tabular data structures, allowing for a more natural way to store and retrieve data
father
, who starts a conversation about going to get ice cream.mother
, who comments that she would also like ice cream.child
, who likes the idea of the family going to get ice cream.people
, posts
,
comments
, and reactions
.
A graph data model is different from a relational model. A graph focuses on the
relationships between information, whereas a relational model focuses on storing
similar information in a list. The graph model received its name because it
resembles a graph when illustrated.
reaction
is an example of an edge, in which a person reacts to a
post.father
, mother
, child
, post
, and
comment
are nodes. The name of the people, the post’s title, and text of the
comment are the predicates. The natural relationships between the authors of the
posts, authors of the comments, and the comments’ topics are edges.
As you can see, a graph models data in a natural way that shows the
relationships (edges) between the entities (nodes) that contain predicates.
People
, Posts
, and Comments
Posts
to People
, to track contributors (authors,
editors, etc.) of a Post
Comments
to People
, to track the author of the
commentComments
to Posts
, to track on which post comments
were madeComments
table, to track comments made
in reply to other comments (a self-reference relationship)people_like_posts
and people_like_comments
tables. None of these solutions is perfect, though, and there is a trade-off
between having a lower table count or having more empty fields in our tables
(also known as “sparse data”).
People
table and then retrieve their primary key and associate it
with the new row in the Posts
table.
person
may have
predicates for their name, age, and gender. A post
might have a predicate
value showing when it was posted, and a value containing the contents of the
post. A comment
would most likely have a predicate containing the comment
string. However, any one node could have other predicates that aren’t contained
on any other node. Each node represents an individual item, hence the singular
naming structure.
father
and the mother
are linked together with a spouse
edge, and both parents are
related to the child along a child
edge.
Post
has an Author
and a Person
has
Posts
. A Post
has Comments
and a Comment
is on a Post
. A Comment
has
an Author
, and a Person
has Comments
. A Parent
has a Child
, and a
Child
has a Parent
.
You create many-to-many relationships in the same way that you make one-to-many
relationships, with an edge between nodes.
Adding groups of related data occurs naturally within a graph. The data is sent
as a complete object instead of separate pieces of information that needs to be
connected afterwards. Adding a new person and a new post to our graph is a
one-step process. New data coming in doesn’t have to be related to any existing
data. You can insert this whole data object with 3 people, a post, and a comment
all in one step.
When new data is added to the model, the model changes to accept the data. Every
change to a graph model is received naturally. When you add a new node with a
data type, you are simply creating a new dot in space and applying a type to it.
The new node doesn’t include any predicates or relationships other than what you
define for it. When you want to add a new predicate onto an existing data type,
the model changes and adds the new property onto the items that you define.
Other items not specifically given the new property type aren’t changed. When
you add a new data type to the database, a new node is created, ready to receive
new edges and predicates.
People
,
Posts
, Comments
, and Likes
.
Mother
,
Child
, and Father
. Because there is only one post as the root of the join,
the post is duplicated to join to each comment.
person
.
posts
and duplicate comments
. Another side effect of
this response approach is that it’s likely that empty data exists in the
response.
In the next section, you’ll see that querying a graph data model avoids the
issues that you would face when querying a relational data model.
person
, post
or comment
) is stored
as a data object (sometimes also called a node). In the example social media
app described in this tutorial, there are objects for individual people, posts,
and comments.
post
, “Ice Cream?”, we traverse the graph to arrive at the
post’s comments
. To find the post’s author
, we traverse the next step to
arrive at the people who authored the comment. This process follows the natural
progression of related data, and graph data models allow us to query our data to
follow this progression efficiently.
What do we mean by efficiently? A graph data model lets you traverse from one
node to a distantly related node without the need for anything like pivot
tables. This means that queries based on edges can be updated easily, with no
need to change the schema to support new many-to-many relationships. And, with
no need to build tables specifically for query optimization, you can adjust your
schema quickly to accommodate new types of data without adversely impacting
existing queries.
comment
on your
post or the last person
to like the comment, filters can be applied to the
edge.