Course contentsShow
← Databases & SQL
Lesson 1425 of 1,95313. Specialized Data StorageFree lesson

Adjacency List vs. Graph Databases

Compare the performance of path traversal in SQL versus native graph stores.

In this lesson, you will learn why native graph databases outperform SQL adjacency lists when performing multi-hop relationship traversals.

The Adjacency List Limitation

In a standard SQL database, you typically model relationships using an "Adjacency List," where a table contains a parent_id or foreign_key column pointing to another row. For simple one-level lookups, this is efficient. However, as soon as you need to traverse multiple levels—like finding "friends of friends of friends"—you hit a wall.

To perform a 3-hop traversal in SQL, you must perform multiple JOIN operations:

SELECT f3.name 
FROM friends f1
JOIN friends f2 ON f1.target_id = f2.source_id
JOIN friends f3 ON f2.target_id = f3.source_id
WHERE f1.source_id = 'user_123';

As the depth of the path increases, the number of joins grows, forcing the database engine to perform expensive index lookups and join computations for every single step.

The Native Graph Advantage

Native graph databases (like Neo4j) use a technique called index-free adjacency. Instead of looking up an index to find the next "join," each record physically stores pointers to the memory addresses of its connected nodes.

When a graph database traverses a relationship, it simply follows the memory pointer from one node to the next. This operation takes constant time, regardless of the total size of the dataset. While the SQL approach slows down linearly or exponentially as you traverse deeper (because the join space expands), the graph database performance is determined only by the size of the path being traversed, not the size of the entire database.

Summary

While SQL is excellent for structured, tabular data, the recursive nature of JOINs makes it mathematically taxing for deep relationship queries. Graph databases treat relationships as first-class citizens, moving from a "search-based" traversal model (SQL) to a "pointer-chasing" traversal model (Graph).

Key Takeaway: While SQL adjacency lists rely on expensive index lookups for every hop in a path, native graph databases utilize index-free adjacency to traverse relationships in constant time per hop.