What is a Graph Database?
A Graph Database is a designed to treat the relationships between data as equally important to the data itself.
Why do Graph Databases matter?
Because graphs are good at handling relationships, some databases store data in the form of a graph.
Example
We have a social network in which five friends are all connected. These friends are Anay, Bhagya, Chaitanya, Dilip, and Erica. A graph database that will store their personal information may look something like this:

Above Example but using traditional database approach
We will now use a Relational database to store information. It is very likely that we use two tables — one for storing the information of each user, and the other one for storing the relationships between the users. Here is how the Users table looks like:
| id | first name | last name | phone | |
|---|---|---|---|---|
| 1 | Anay | Agarwal | anay@example.net | 555-111-5555 |
| 2 | Bhagya | Kumar | bhagya@example.net | 555-222-5555 |
| 3 | Chaitanya | Nayak | chaitanya@example.net | 555-333-5555 |
| 4 | Dilip | Jain | dilip@example.net | 555-444-5555 |
| 5 | Erica | Emmanuel | erica@example.net | 555-555-5555 |
Now, we will also a need another table to capture the friendship/relationship between users/friends. Our friendship table will look something like this:
| user_id | friend_id |
|---|---|
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 1 | 5 |
| 2 | 1 |
| 2 | 3 |
| 2 | 4 |
| 2 | 5 |
| 3 | 1 |
| 3 | 2 |
| 3 | 4 |
| 3 | 5 |
| 4 | 1 |
| 4 | 2 |
| 4 | 3 |
| 4 | 5 |
| 5 | 1 |
| 5 | 2 |
| 5 | 3 |
| 5 | 4 |
We will avoid going deep into the Database(primary key & foreign key) theory. Instead just assume that the friendship table uses id’s of both the friends.
Assume that our social network here has a feature that allows every user to see the personal information of his/her friends. So, If Chaitanya were requesting information then it would mean she needs information about Anay, Bhagya, Dilip and Erica. We will approach this problem the traditional way(Relational database). We must first identify Chaitanya’s id in the User’s table:
| id | first name | last name | phone | |
|---|---|---|---|---|
| 3 | Chaitanya | Nayak | chaitanya@example.net | 555-333-5555 |
Now, we’d look for all tuples in friendship table where the user_id is 3. Resulting relation would be something like this:
| user_id | friend_id |
|---|---|
| 3 | 1 |
| 3 | 2 |
| 3 | 4 |
| 3 | 5 |
Now, let’s analyse the time taken in this Relational database approach. This will be approximately log(N) times where N represents the number of tuples in friendship table or number of relations. Here, the database maintains the rows in the order of id’s. So, in general for ‘M’ no of queries, we have a time complexity of M*log(N)
Only if we had used a graph database approach, the total time complexity would have been O(N). Because, once we’ve located Cindy in the database, we have to take only a single step for finding her friends. Here is how our query would be executed:

Disadvantages:
Note that graph databases aren’t always the best solution for an application. We will need to assess the needs of application before deciding the architecture.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Recommended Posts:
- Indexing in Databases | Set 1
- Denormalization in Databases
- Horizontal and Vertical Scaling In Databases
- Active Databases
- Types of Databases
- Concepts of Distributed databases
- How to Store and Extract XML Documents from Databases
- Clausal Form in Deductive Databases
- Horn Clauses in Deductive Databases
- Primary Indexing in Databases
- Precedence Graph For Testing Conflict Serializability in DBMS
- Graph Based Concurrency Control Protocol in DBMS
- Introduction of Relational Algebra in DBMS
- Introduction of Database Normalization
- Introduction of ER Model
- Introduction of DBMS (Database Management System) | Set 1
- Introduction of 3-Tier Architecture in DBMS | Set 2
- Introduction of Relational Model and Codd Rules in DBMS
- PL/SQL Introduction
- Introduction to TimeStamp and Deadlock Prevention Schemes in DBMS
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

