Related Articles

Related Articles

Introduction to Graph Databases
  • Last Updated : 09 Sep, 2019

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.

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 email phone
1 Anay Agarwal 555-111-5555
2 Bhagya Kumar 555-222-5555
3 Chaitanya Nayak 555-333-5555
4 Dilip Jain 555-444-5555
5 Erica Emmanuel 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 email phone
3 Chaitanya Nayak 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:

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :