You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
:description: This page explores the concepts of graph databases from a relational developer's point of view.
2
+
= Comparing relational to graph database
3
+
:description: This page explores the conceptual differences between relational and graph database structures and data models.
5
4
6
5
This page explores the conceptual differences between relational and graph database structures and data models.
7
-
It also gives a high-level overview of how working with each database type is similar or different - from the relational and graph query languages to interacting with the database from applications.
6
+
For a comparison between programming languages, see xref:cypher-intro/cypher-sql.adoc[Comparing Cypher with SQL].
8
7
9
8
[#relational-vs-graph]
10
9
== Relational database overview
11
10
12
-
Relational databases store highly-structured data in tables with predetermined columns of specific types and rows of those defined types of information.
11
+
Relational databases store highly-structured data in tables with predetermined columns and rows of specific types of information.
13
12
Due to the rigidity of their organization, relational databases require developers and applications to strictly structure the data used in their applications.
14
13
15
14
In relational databases, references to other rows and tables are indicated by referring to primary key attributes via foreign key columns.
16
15
Joins are computed at query time by matching primary and foreign keys of all rows in the connected tables.
17
16
These operations are compute-heavy and memory-intensive, and have an exponential cost.
18
17
19
-
When many-to-many relationships occur in the model, you must introduce a _JOIN_ table (or associative entity table) that holds foreign keys of both the participating tables, further increasing join operation costs, as shown in the diagram:
18
+
When many-to-many relationships occur in the model, you must introduce a `JOIN` table (or associative entity table) that holds foreign keys of both the participating tables, further increasing join operation costs:
20
19
21
-
.Relational model
22
-
image::relational_model.svg[Depiction of a relational database with connecting points in each table,role=popup,width=600]
20
+
image::relational-model.svg[Depiction of a relational database with connecting points in each table,role=popup,width=400]
23
21
24
-
The diagram shows the concept of connecting a `Person` (from the `Person` table) to a `Department` (in `Department` table) by creating a `Person-Department` join table that contains the ID of the person in one column and the ID of the associated department in the next column.
22
+
The diagram shows the concept of connecting an `Employee` (from the `Employee` table) to a `Department` (in the `Departments` table) by creating a `Dpt_Members` join table that contains the ID of the employee in one column and the ID of the associated department in another column.
25
23
26
-
This structure makes understanding the connections cumbersome, because you must know the person ID and department ID values (performing additional lookups to find them) in order to know which person connects to which departments.
27
-
These types of costly join operations are often addressed by denormalizing the data to reduce the number of joins necessary, therefore breaking the data integrity of a relational database.
24
+
This structure makes understanding the connections cumbersome, because you must know the employee and the department's ID values (performing additional lookups to find them) in order to know which employee connects to which department.
28
25
29
-
Graph databases cater for use cases that weren't a good fit for relational data models and offer new possibilities to connect data.
26
+
Additionally, these types of costly join operations are often addressed by denormalizing the data to reduce the number of joins necessary, therefore breaking the data integrity of a relational database.
27
+
Graph databases stand where relational databases don't cover the needs of developers, and offer other ways to connect data.
30
28
31
-
[#relational-to-graph]
32
29
== Translating relational knowledge to graphs
33
30
34
-
Unlike other database management systems, relationships are of equal importance in the graph data model to the data itself.
35
-
This means we are not required to infer connections between entities using special properties such as foreign keys or out-of-band processing like map-reduce.
31
+
Unlike other database management systems, relationships are of equal importance to the data itself in a graph data model.
32
+
This means you are not required to infer connections between entities using special properties such as foreign keys or out-of-band processing like map-reduce.
36
33
37
-
By assembling nodes and relationships into connected structures, graph databases enable us to build simple and sophisticated models that map closely to our problem domain.
38
-
The data stays remarkably similar to its form in the real world - small, normalized, yet richly connected entities.
39
-
This allows you to query and view your data from any imaginable point of interest, supporting many different use cases.
34
+
By assembling nodes and relationships into connected structures, graph databases enable building models that map closely to a problem domain.
35
+
Each node contains relationships to other nodes, and these relationship are organized by type and direction, holding or not additional attributes.
40
36
41
-
Each node (entity or attribute) in the graph database model directly and physically contains a list of relationship records that represent the relationships to other nodes.
42
-
These relationship records are organized by type and direction and may hold additional attributes.
43
-
Whenever you run the equivalent of a _JOIN_ operation, the graph database uses this list, directly accessing the connected nodes and eliminating the need for expensive search-and-match computations.
37
+
Whenever you run xref:cypher-intro/cypher-sql.adoc[the equivalent of a `JOIN` operation], the graph database uses these relationships, directly accessing the connected nodes and eliminating the need for expensive search-and-match computations.
44
38
45
39
This ability to pre-materialize relationships into the database structure allows Neo4j to provide performance of several orders of magnitude above others, especially for join-heavy queries, allowing users to leverage a _minutes to milliseconds_ advantage.
46
40
@@ -59,108 +53,45 @@ endif::[]
59
53
[#rdbms-graph-model]
60
54
== Data model differences
61
55
62
-
As you can probably imagine from the structural differences discussed above, the data models for relational versus graph are very different.
63
-
The straightforward graph structure results in much simpler and more expressive data models than those produced using traditional relational or other NoSQL databases.
56
+
If you are familiar with the relational data model that has tables, columns, relationship cardinalities, and other components, graph data modeling will not seem entirely foreign.
64
57
65
-
If you are used to modeling with relational databases, remember the ease and beauty of a well-designed, normalized entity-relationship diagram - a simple, easy-to-understand model you can quickly whiteboard with your colleagues and domain experts.
66
-
A graph is exactly that - a clear model of the domain, focused on the use cases you want to efficiently support.
58
+
The design of a xref:data-modeling/index.adoc[graph data model] still needs to be based upon requirements for access, queries, performance expectation, and business logic.
59
+
However, the structure is laid out differently.
67
60
68
-
Let's compare the two data models to show how the structure differs between relational and graph.
61
+
For example, if you want to know which departments Alice belongs too, this is how a relational and a graph databases would structure the same data:
69
62
70
-
.Relational - Person and Department tables
71
-
image::relational_as_graph.jpg[role="popup-link"]
63
+
image::relational-as-graph.svg[Representation of tabular data in a relational database and the comparison with the same data structured in a graph,role=popup]
72
64
73
-
In the above relational example, we search the Person table on the left (potentially millions of rows) to find the user Alice and her person ID of 815. Then, we search the Person-Department table (orange middle table) to locate all the rows that reference Alice's person ID (815). Once we retrieve the 3 relevant rows, we go to the Department table on the right to search for the actual values of the department IDs (111, 119, 181).
74
-
Now we know that Alice is part of the 4Future, P0815, and A42 departments.
65
+
In the relational example, to the left, you need to:
. Search the `Employees` table (potentially with millions of rows) to find the user Alice and her ID of 815.
68
+
. Search the `Dept_Members` table to locate all the rows that reference Alice's ID of 815.
69
+
. Once the 3 relevant rows are found, you go for the `Departments` table to search for the actual values of the department IDs (111, 119, 181).
70
+
. Only now you know that Alice is part of the 4Future, P0815, and A42 departments.
78
71
79
-
In the above graph version, we have a single node for Alice with a label of Person.
80
-
Alice belongs to 3 different departments, so we create a node for each one and with a label of Department.
81
-
To find out which departments Alice belongs to, we would search the graph for Alice's node, then traverse all of the BELONGS_TO relationships from Alice to find the Department nodes she is connected to.
82
-
That's all we need - a single hop with no lookups involved.
72
+
In the case of the graph version, you need to:
83
73
84
-
[TIP]
85
-
====
86
-
More information on this topic can be found in the https://neo4j.com/docs/getting-started/current/data-modeling/[Data Modeling section].
87
-
====
74
+
. Search for Alice's `Employee` nod.
75
+
. Traverse all of the `BELONGS_TO` relationships from Alice and find the `Department` nodes she is connected to.
88
76
77
+
If you want to learn how to create a data model, follow the xref:data-modeling/tutorial-data-modeling.adoc[Tutorial: Create a graph data model] or see how to adapt an existing project with a relational model to a graph on xref:data-modeling/relational-to-graph-modeling.adoc[Modeling: relational to graph].
89
78
90
79
[#rdbms-graph-query]
91
80
== Data storage and retrieval
92
81
93
-
Querying relational databases is easy with SQL - a declarative query language that allows both easy ad-hoc querying in a database tool, as well as use-case-specific querying from application code.
94
-
Even object-relational mappers (ORMs) use SQL under the hood to talk to the database.
95
-
96
-
Do graph databases have something similar?
97
-
Yes!
98
-
99
-
Cypher, Neo4j's declarative graph query language, is built on the basic concepts and clauses of SQL but has a lot of additional graph-specific functionality to make it easy to work with your graph model.
100
-
101
-
If you have ever tried to write a SQL statement with a large number of joins, you know that you quickly lose sight of what the query actually does because of all the technical noise in SQL syntax.
102
-
In Cypher, the syntax remains concise and focused on domain components and the connections among them, expressing the pattern to find or create data more visually and clearly.
103
-
Other clauses outside of the basic pattern matching look very similar to SQL, as Cypher was built on the predecessor language's foundations.
104
-
105
-
We will cover Cypher query language syntax in an upcoming guide, but let us look at a brief example of how a SQL query differs from a Cypher query.
106
-
In the organizational domain from our data modeling example above, what would a SQL statement that *lists the employees in the IT Department* look like, and how does it compare to the Cypher statement?
107
-
108
-
.SQL Statement
109
-
[source,sql]
110
-
----
111
-
SELECT name FROM Person
112
-
LEFT JOIN Person_Department
113
-
ON Person.Id = Person_Department.PersonId
114
-
LEFT JOIN Department
115
-
ON Department.Id = Person_Department.DepartmentId
116
-
WHERE Department.name = "IT Department"
117
-
----
118
-
119
-
.Cypher Statement
120
-
[source,cypher]
121
-
----
122
-
MATCH (p:Person)-[:WORKS_AT]->(d:Dept)
123
-
WHERE d.name = "IT Department"
124
-
RETURN p.name
125
-
----
126
-
127
-
[TIP]
128
-
====
129
-
You can find more about Cypher syntax in the upcoming chapters for https://neo4j.com/docs/getting-started/current/cypher-intro[Cypher Query Language^] and transitioning https://neo4j.com/developer/guide-sql-to-cypher/[from SQL to Cypher^].
130
-
====
131
-
132
-
[#rdbms-graph-practice]
133
-
=== Transitioning from Relational to Graph - In Practice
134
-
135
-
If you do decide to move your data from a relational to a graph database, the steps to transition your applications to use Neo4j are actually quite simple.
136
-
You can connect to Neo4j with a driver or connector library designed for your stack or programing language, just as you can with other databases.
137
-
Thanks to Neo4j and its community, there are Neo4j drivers that mimic existing database driver idioms and approaches for nearly any popular programing language.
138
-
139
-
For instance, the Neo4j JDBC driver would be used like this to query the database for _John's departments_:
140
-
141
-
[source, clike]
142
-
----
143
-
Connection con = DriverManager.getConnection("jdbc:neo4j://localhost:7474/");
144
-
145
-
String query =
146
-
"MATCH (:Person {name:{1}})-[:EMPLOYEE]-(d:Department) RETURN d.name as dept";
For more information, you can visit our pages for https://neo4j.com/developer/language-guides/[Building Applications^] to see how to connect to Neo4j using different programming languages.
160
-
====
161
-
162
-
[#rdbms-graph-resources]
163
-
== Resources
164
-
* https://neo4j.com/resources/rdbms-developer-graph-white-paper/[Free eBook: Relational to Graph^]
165
-
* https://dzone.com/refcardz/from-relational-to-graph-a-developers-guide[DZone Refcard: From Relational to Graph^]
166
-
* https://neo4j.com/developer/data-modeling/[Data Modeling: Relational to Graph]
82
+
SQL is the query language used to query relational databases.
83
+
xref:cypher.adoc[Cypher] is Neo4j’s declarative query language built on the basic concepts and clauses of SQL, but with additional functionalities that make working with graph databases more efficient.
84
+
85
+
For example, when writing an SQL statement with a large number of joins, you can quickly lose sight of what the query actually does, since there is a lot of technical noise in SQL syntax.
86
+
In Cypher, the syntax remains concise and focused on domain components and their connections, thus expressing the pattern to find or create data more visually and clearly.
87
+
88
+
Other clauses outside of the basic pattern matching still look very similar to SQL, as Cypher was built on the predecessor language’s foundation.
89
+
You can see the similarities and differences in
90
+
If you want to learn how to move your data from a relational database to a graph, see
91
+
92
+
== Keep learning
93
+
94
+
* xref:/cypher-intro/cypher-sql.adoc[Comparing Cypher with SQL] -> See examples comparing SQL queries with Cypher.
95
+
* xref:data-import/relational-to-graph-import.adoc[Import: RDBMS to graph] -> Learn how to import data from a relational database to a graph.
96
+
* xref:data-modeling/relational-to-graph-modeling.adoc[Modeling: relational to graph] -> Find more comparisons between relational and graph data modeling.
97
+
* https://neo4j.com/resources/rdbms-developer-graph-white-paper/[The Definitive Guide to Graph Databases for the RDBMS Developer] -> Download the free e-book.
image::relational-model.svg[Depiction of a relational database with connecting points in each table,role=popup,width=400]
37
37
38
38
In a graph, you do not need to worry about table joins and index lookups because graph data is structured by each, individual entity and its relationships with other individual entities.
39
39
@@ -59,7 +59,7 @@ The steps to help you with the transformation of a relational diagram are listed
59
59
If you apply the items in the list above to our example finding Alice's departments, we can come to a graph like the one shown below.
image::relational-graph-model-arr.svg[Equivalent representation of the previous tabular data into graph,role=popup,width=300]
63
63
64
64
Though the two models have similarities such as categorizing data by using either a table structure or a label, the graph model does not confine data to a pre-defined and strict table/column layout.
65
65
We will look at another example in the next section.
0 commit comments