Part 6 of 10
Graph-like data models
The kind of links in your data points to a model. Mostly one-to-many trees with few cross-links: documents. Simple many-to-many: tables. Many-to-many everywhere, with complex connections: a graph.
A graph has vertices (nodes, entities) and edges (relationships, arcs). Examples:
- Social graph: people, and who knows whom.
- Web graph: pages, and links between them.
- Road or rail map: junctions, and the roads or tracks between them.
Known algorithms run on them, like shortest path for directions and PageRank, which scores page popularity on the web graph for ranking.
Two ways to hold a graph:
- Adjacency list: each vertex stores the IDs of its direct neighbors, good for traversals.
- Adjacency matrix: a 2D grid with 1 where two vertices link and 0 elsewhere, good for ML.
Vertices need not all be one kind; a strong use is many object types in one store. A big social network can keep one graph with vertices for people, places, events, check-ins, and comments, and edges for friendships, check-in locations, who commented on what, and who attended what. Search engines keep knowledge graphs of often-searched companies, people, and places, built by crawling and analyzing the web.
Two graph models follow:
- Property graphs: Neo4j, Memgraph, KùzuDB, and others.
- Triple stores: Datomic, AllegroGraph, Blazegraph, and others.
They are about equally expressive, and some databases, like Amazon Neptune, do both. Four query languages follow too (Cypher, SPARQL, Datalog, GraphQL), plus graphs in SQL.
Property graphs
In a property graph (labeled property graph), a vertex has:
- A unique ID and a label saying what kind of thing it is.
- Its edges going out and its edges coming in.
- Properties: key-value pairs.
An edge has:
- A unique ID and a label saying what kind of link it is.
- A tail vertex (start) and a head vertex (end).
- Properties.
You could keep this in two relational tables, vertices and edges, with properties in a JSON column (like PostgreSQL's jsonb), and index the tail and head columns to find a vertex's edges fast.
What matters:
- Any vertex can link to any other; no schema limits what connects to what.
- From any vertex you can quickly find edges in and out, and walk forward or backward.
- Labels let one graph hold many kinds of data and stay tidy.
The edges table is a general join table holding every kind of link. Indexes on labels and properties let you find vertices or edges by value.
One limit: an edge joins exactly two vertices. A relational join table can model a three-way link with several foreign keys per row, like a doctor, patient, and clinic for one visit. A graph needs an extra "visit" vertex with edges to each, or a hypergraph.
Graphs handle data that a fixed relational schema finds awkward:
- Countries divide land differently (states in one, provinces in another), with odd cases like a territory inside another country.
- Mixed detail: a home known to the town, a birthplace only to the country.
They are easy to extend. To track spoken languages, just add language vertices and edges from people and countries to them. So graphs suit evolvability as app data changes.
Cypher
Cypher queries property graphs. It was made for Neo4j, then became the open standard openCypher, and Memgraph, KùzuDB, Amazon Neptune, and Apache AGE (on PostgreSQL) support it.
Inserting with CREATE:
- Each vertex gets a short name used only inside that query, not stored.
- A label like Person or Place gives its kind; properties go in braces.
- Edges are arrows: an arrow from a town to its region makes a PART_OF edge with the town as tail and the region as head.
Example question: names of people who studied in Asia and now live in Africa. In graph terms: a person with a STUDIED_IN edge to some place inside Asia, and a LIVES_IN edge to some place inside Africa.
MATCH uses the same arrows. Matching vertices can bind to variables (like person), and the rest can stay unnamed. PART_OF*0.. means "follow PART_OF edges zero or more times", so whether the place is a town, region, or country, the chain climbs to the continent.
Walking the graph
Step 1 of 5
A small property graph. Asha is a Person vertex, the places are vertices too, and labeled edges join them.
Being declarative, Cypher lets the engine pick the order: scan every person, or start at the two continents (quick with an index on name), collect every place inside via incoming PART_OF edges, then follow incoming STUDIED_IN and LIVES_IN edges to the people.