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||phone|
Now, we will also a need another table to capture the friendship/relationship between users/friends. Our friendship table will look something like this:
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|
Now, we’d look for all tuples in friendship table where the user_id is 3. Resulting relation would be something like this:
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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Denormalization in Databases
- Indexing in Databases | Set 1
- Types of Databases
- Active Databases
- Primary Indexing in Databases
- Concepts of Distributed databases
- How to Store and Extract XML Documents from Databases
- Clausal Form in Deductive Databases
- Horizontal and Vertical Scaling In Databases
- Horn Clauses in Deductive Databases
- Graph Based Concurrency Control Protocol in DBMS
- Precedence Graph For Testing Conflict Serializability in DBMS
- PL/SQL Introduction
- Introduction of MS SQL Server
- Introduction of MariaDB
- Introduction to Couchbase
- Neo4j Introduction
- Introduction of B+ Tree
- Introduction to NoSQL
- Introduction of B-Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.