Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Property Graphs

Last Updated: December 13th, 2024

Property Graphs

Nodes

Edges

Property Graph

At the high level, just “two representations”: nodes and edges.

Easily supports graph traversal and path queries:

Property Graphs in RDBMSes

CREATE TABLE nodes
(node_id INTEGER PRIMARY KEY,
 type STRING,
 properties JSON);

CREATE TABLE edges 
(edge_id INTEGER
   PRIMARY KEY, 
 head_id INTEGER
   REFERENCES Nodes(node_id), 
 tail_id INTEGER
   REFERENCES Nodes(node_id), 
 type STRING,
 properties JSON);
Property Graph in RDBMSes

Path Traversal Queries in SQL

Suppose we want to find all the US locations in the graph database.

WITH RECURSIVE in_usa (node_id) AS (
  ( SELECT node_id
    FROM nodes
    WHERE properties->>'name' = 'United States'
  ) UNION (
    SELECT tail_id
    FROM edges
    JOIN in_usa ON edges.head_id = in_usa.node_id
    WHERE edges.type = 'within'
  )
)
SELECT * FROM in_usa;

Path traversals of graphs in SQL are achieved using recursive CTEs.

If we wanted rapid path traversal on incoming/outgoing edges, we could build additional indexes:

CREATE INDEX edge_tails 
  ON edges(tail_id); 
CREATE INDEX edge_heads 
  ON edges(head_id);