What is Graph DB
- In graph DB entities(are nodes) and edges are relationships how these nodes are connected
Represent Entities & Relations
Small Social Graph(Eg: Twitter) | Ruth has published Messages |
---|---|
- Ruth has published a string of messages. Her most recent message can be found marked CURRENT. - The PREVIOUS relationships then create Ruth’s timeline. |
Comments on Photos by users
-
Consider facebook social graph, User1 uploaded a photo1
User2, User3 added comment2, comment3 on the photo1.
![]() |
Nodes: - User Node: Represents each user (e.g., User1, User2) - Photo Node: Represents photo uploaded - Comment Node: Represents each comment made on the photo
Relationships: - COMMENTED_ON: Relationship from a User node to the Photo node indicating that the user commented on that photo. - HAS_COMMENT: Relationship from the Photo node to each Comment node indicating that the photo has these comments. - WRITTEN_BY: Relationship from each Comment node to the User node who wrote it.
|
Count Comments: Reach Photo node, count the number of HAS_COMMENT relationships
All photos commented on by a specific user:
|
3 Friends(A,B,C). A created video. B commented. C shared and liked
Nodes User: Represents users like UserA, UserB, and UserC. Video: Represents each video uploaded. Comment: Represents comments on videos. Like: Represents likes on videos. Relationships: FRIEND: Connects User nodes (e.g., UserA is FRIEND with UserB and UserC). CREATED: Connects User to Video (e.g., UserA CREATED Video1). COMMENTED: Connects User to Comment (e.g., UserB COMMENTED Comment1). LIKED: Connects User to Video (e.g., UserC LIKED Video1). SHARED: Connects User to Video (e.g., UserC SHARED Video1). ON: Connects Comment to Video (e.g., Comment1 ON Video1). |
![]() |
|
How efficiently particular node is searched in graphDB?
Method-1 (Store address of user nodes)
-
In seperate database directly store addresses of user nodes, whenever
user quries, directly start from that node
Method-2 (Indexing)
Advatanges over SQL
Connected Data
SQL | Graph DB | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Which DB to choose for Connected Dataset | join-intensive query performance deteriorates as the dataset gets bigger Since need to traverse whole graph |
performance remain relatively constant, even as the dataset grows, because queries are
localized to a portion of the graph Execution time for each query is proportional only to the size of the part of the graph traversed rather than the size of the overall graph |
||||||||||||||||||||
Friends of Friend Query / Friends of Friends of Friend Query |
Query to get friend of Bob on SQL database is complex When we want Friends of friends of Bob(it will get more complicated), bcoz it will involve Recursive JOINS Traversing whole DB
|
We can add any number of relationships in graph database.
Execution time comparison
|