BigBear.ai
  • Home
  • Industries
    • Academia
    • Government
    • Healthcare
    • Manufacturing
  • Solutions
    • Cyber
    • Data Analytics
    • Enterprise Planning and Logistics
    • Intelligent Automation
    • Modeling Solutions
    • Professional Services
  • Company
    • About
    • Investor Relations
    • Partners
    • Team
  • Careers
    • Benefits
    • Culture
    • Explore Jobs
    • Military and Veterans
    • Applicant Login
    • Employee Login
  • Resources
    • Blog
    • Events
    • Newsroom
    • Resource Library
    • Online Store
  • Contact
Search

Home Artificial Intelligence Use Graph Databases for Complex Hierarchies

Blog

Use Graph Databases for Complex Hierarchies

Jim McHugh
June 7, 2022
  • Share
  • Share

If you have been paying attention over the past several years, you should have noticed the rise in graph databases. Although we are still in the early adopter’s phase of the Technology Adoption Lifecycle, a few innovative companies are leveraging graph database technology to change how we think about database-driven applications.

For over 40 years, the relational database has been the default concept, even when it should not have been. We have found ways to make relational databases decompose hierarchies with recursive queries and use windowing functions to compare data from the previous or next row. Although these queries can provide one with the correct answer, they are sub-optimal solutions.

Along comes the graph database, and we start to see a more effortless and optimal way to answer these questions. Graph databases were created because of performance and scalability challenges with relational databases.

Let’s take a simple (or not so simple) hierarchy, Elizabeth Taylor’s family tree. I have created a relational data model (Figure 2.) to mimic the data captured and displayed in the graph database (Figure 1.).

Elizabeth Taylor’s Family Tree – Graph Database

Figure 1 – Elizabeth Taylor’s Family Tree – Graph Database

Not only does the graph model allow us to connect and traverse the relationships between the individuals optimally, but it is also visually pleasing. From this knowledge graph, we can answer many questions using less CPU and memory than the relational database equivalent.

Figure 2 – Relational Family Tree Data Model

Now that we have created competing database designs, let’s compare usage and performance by asking questions and reviewing the queries.

Q1: How many times was Elizabeth Taylor married?

Graph DB:

MATCH (lt:Person {Name: “Elizabeth Taylor”})-[m:MARRIED]-()

RETURN COUNT(m);

Relational Family Tree Data Model

Relational DB:

SELECT count(*)

FROM MARRIAGE

WHERE spouse1_person_id in

(SELECT person_id

FROM PERSON WHERE person_name_last = ‘Taylor’

AND person_name_first = ‘Elizabeth’)

OR

spouse2_person_id in

(SELECT person_id

FROM PERSON

WHERE person_name_last = ‘Taylor’

AND person_name_first = ‘Elizabeth’);

relational database

Because of the relational database design and the need for joins between the PERSON and MARRIAGE tables, the graph database will outperform the relational database for these types of queries.

Q2: Were any of Elizabeth Taylor’s children adopted?

Graph DB:

MATCH (lt:Person {Name: “Elizabeth Taylor”})-[c:CHILD_OF {Adopted_Flag: 1}]-(n:Person)

RETURN n

Relational Database profile

Relational Database

SELECT person_name_first, person_name_last

FROM PERSON

WHERE person_mother_id =

(SELECT person_id

FROM PERSON

WHERE person_name_last = “Taylor”

AND person_name_first = “Elizabeth”)

AND person_adopted_fl = 1;

relational query

Here is an instance where the relational query is simple, easy to understand, scales well if indexed appropriately, and performs on par with the graph database.

 

Q3: How many Actors did Elizabeth Taylor marry?

Graph DB:

MATCH (lt:Person {Name: “Elizabeth Taylor”})-[m:MARRIED]-(h:`Actor`)

RETURN COUNT(h)

How many Actors did Elizabeth Taylor marry

Relational Database:

SELECT count(person_id)

FROM PERSON_PROFESSION PP

JOIN PROFESSION P ON (PP.profession_id = P.profession_id)

WHERE PP.person_id in (

SELECT spouse1_person_id as spouse_id

FROM MARRIAGE

WHERE spouse1_person_id in

(SELECT person_id

FROM PERSON WHERE person_name_last = ‘Taylor’

AND person_name_first = ‘Elizabeth’)

UNION

SELECT spouse2_person_id as spouse_id

FROM MARRIAGE

WHERE spouse2_person_id in

(SELECT person_id

FROM PERSON

WHERE person_name_last = ‘Taylor’

AND person_name_first = ‘Elizabeth’)

)

AND p.profession_title = ‘Actor’;

complex query

The relational query above is a complex query that doesn’t scale well. It requires more CPU and memory than the graph database to answer the question.

 

Q3: How many grandchildren did Elizabeth Taylor have?

Graph DB:

MATCH (lt:Person {Name: “Elizabeth Taylor”})-[r:CHILD_OF*2..2]->(gc:Person)

RETURN COUNT (gc)

Relational Database WITH RECURSIVE grandchildren

Relational Database

WITH RECURSIVE grandchildren as (

SELECT person_id

,person_name_first

,person_name_last

,person_mother_id

,person_father_id

,0 AS ancestor_level

FROM person

WHERE person_name_last = ‘Taylor’

AND person_name_first = ‘Elizabeth’

UNION

SELECT p.person_id

,p.person_name_first

,p.person_name_last

,p.person_mother_id

,p.person_father_id

,ancestor_level + 1

FROM person p

JOIN grandchildren gc on p.person_mother_id= gc.person_id or p.person_father_id= gc.person_id

)

SELECT * FROM grandchildren

WHERE ancestor_level = 2;

hard to understand query

The recursive query above is difficult to write and hard to understand. In addition, this query doesn’t scale well and will require more CPU and memory from the database to answer the same question asked of the graph database. Although relational databases can store and return hierarchical data, they were not designed to do so. Therefore, recursive queries like this should be avoided if possible.

As can be seen from these examples, the graph DB queries are much easier to write and understand and, in general, will scale better than the relational database queries for the example queries above.

The main point I hope you take away from this blog post is to know the application’s requirements and use the correct database or databases as a part of your design. I am not saying that one database is superior to another. They are simply different.

In conclusion, relational and graph databases can harmoniously coexist if designed correctly, providing accuracy, speed, and agility to application developers and users.

 

About the Author

Jim McHugh is the Vice President of National Intelligence Service – Emerging Markets Portfolio. Jim is responsible for the delivery of Analytics and Data Management to the Intelligence Community.

Posted in Artificial Intelligence, Data Modeling, Machine Learning.
BigBear.ai
  • Home
  • Industries
  • Solutions
  • Company
  • Careers
  • Blog
  • Investor Relations
  • Contact
  • Twitter
  • Facebook
  • Linkedin
  • Google My business for BigBear.ai
1-410-312-0885
[email protected]
  • Privacy Policy
  • Terms of Use
  • Accessibility
  • Site Map
© BigBear.ai 2023
We value your privacy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Privacy Policy | Do not sell my personal information
AcceptCookie Settings
Manage Consent

Cookies Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
JSESSIONIDsessionThe JSESSIONID cookie is used by New Relic to store a session identifier so that New Relic can monitor session counts for an application.
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
CookieDurationDescription
__atuvc1 year 1 monthAddThis sets this cookie to ensure that the updated count is seen when one shares a page and returns to it, before the share count cache is updated.
__atuvs30 minutesAddThis sets this cookie to ensure that the updated count is seen when one shares a page and returns to it, before the share count cache is updated.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
CookieDurationDescription
_ga2 yearsThe _ga cookie, installed by Google Analytics, calculates visitor, session and campaign data and also keeps track of site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognize unique visitors.
_ga_NK4L4Q320Q2 yearsThis cookie is installed by Google Analytics.
_gat_gtag_UA_163894009_21 minuteSet by Google to distinguish users.
_gid1 dayInstalled by Google Analytics, _gid cookie stores information on how visitors use a website, while also creating an analytics report of the website's performance. Some of the data that are collected include the number of visitors, their source, and the pages they visit anonymously.
at-randneverAddThis sets this cookie to track page visits, sources of traffic and share counts.
CONSENT2 yearsYouTube sets this cookie via embedded youtube-videos and registers anonymous statistical data.
uvc1 year 1 monthSet by addthis.com to determine the usage of addthis.com service.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
CookieDurationDescription
f5avraaaaaaaaaaaaaaaa_session_sessionbusinesswire.com cookie
loc1 year 1 monthAddThis sets this geolocation cookie to help understand the location of users who share the information.
VISITOR_INFO1_LIVE5 months 27 daysA cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface.
YSCsessionYSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages.
yt-remote-connected-devicesneverYouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
yt-remote-device-idneverYouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
Save & Accept