Choosing a Database · Lesson 67 of 112

Database Wide-Column and Graph Databases

What you will learn

  • The idea behind wide-column stores
  • The idea behind graph databases
  • How to feel the difference with small demos
  • When these specialised kinds are worth the extra learning

About the examples in this lesson. The Python code below is a small model of the idea, built from plain Python data (dictionaries, lists, JSON text) so you can run it in your browser. It is not a real database product. Real products of this kind are separate programs, usually run on a server, with many more features. This course does not run them; it teaches the idea so that you can recognise the kind and choose well.

1. Wide-column stores

A wide-column store (sometimes called a column-family store) keeps rows under a row key, and each row may hold many columns that differ from row to row. Think of a dictionary of dictionaries, kept sorted by key and spread over many machines. It suits huge amounts of data written steadily and read by key and range, such as readings from sensors over time. Apache Cassandra, for example, describes itself as a distributed NoSQL database that scales horizontally by partitioning and replicating data across nodes (checked on its website).

rows = {
    "sensor:1": {"2026-01-01": 21.5, "2026-01-02": 22.0, "2026-01-03": 20.8},
    "sensor:2": {"2026-01-02": 19.0},
}
print(sorted(rows["sensor:1"]), sorted(rows["sensor:2"]))
print(rows["sensor:1"]["2026-01-02"])
['2026-01-01', '2026-01-02', '2026-01-03'] ['2026-01-02']
22.0

Here the columns are dates. Sensor 1 has three, sensor 2 has one, and a new date simply adds a column. Reading "everything for sensor 1" is one lookup. The price is that questions must follow the layout: you plan the data around the questions, and ad-hoc joins are generally not the strength of this kind.

Do not confuse this with a column-oriented analytics database (lesson 9.5), which stores each column of an ordinary table together. The names sound alike; the ideas differ.

2. Graph databases

A graph has nodes (things) and edges (links between them). Neo4j, a well-known graph database, stores data as nodes, relationships and properties and queries it with a language called Cypher (checked in its documentation). A graph database is built for questions about connections: friends of friends, routes, "people who bought this also bought".

You can sketch the idea in SQL with a table of edges. Here Rina (1) follows Sam (2) and Joe (5), Sam follows Lee (3), and Lee follows Amy (4). Who can Rina reach in up to three steps?

import sqlite3

con = sqlite3.connect(":memory:")
con.executescript("""
CREATE TABLE person (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE follows (a INTEGER, b INTEGER, PRIMARY KEY (a, b));
INSERT INTO person VALUES (1,'Rina'),(2,'Sam'),(3,'Lee'),(4,'Amy'),(5,'Joe');
INSERT INTO follows VALUES (1,2),(2,3),(3,4),(1,5);
""")
q = """
WITH RECURSIVE reach(id, hops) AS (
  SELECT b, 1 FROM follows WHERE a = 1
  UNION
  SELECT f.b, r.hops + 1 FROM follows f JOIN reach r ON f.a = r.id WHERE r.hops < 3
)
SELECT p.name, MIN(r.hops) FROM reach r JOIN person p ON p.id = r.id GROUP BY p.name ORDER BY MIN(r.hops), p.name
"""
print(con.execute(q).fetchall())
[('Joe', 1), ('Sam', 1), ('Lee', 2), ('Amy', 3)]

Joe and Sam are one step away, Lee two, Amy three. The recursive query (WITH RECURSIVE) repeats a join; it works, but it is awkward. A graph database offers query languages designed for this kind of path question, and stores links so that following them stays quick as the network grows. If connections are the heart of your data, that matters. If you only look up a person's own details, it does not.

Worked example: which kind?

Task Better fit
Store billions of temperature readings, read by sensor and day range wide-column
Suggest friends of friends in a social app graph
Store invoices and customers relational

The tasks are examples, not recommendations of a product.

Common mistakes

  • Choosing a specialised kind early. Start with a relational database; move when a real need appears.
  • Assuming a wide-column store is a table with more columns. The data is arranged around the questions you plan to ask.
  • Using a graph database for data with few connections.

Key takeaways

  • Wide-column stores hold a row key with many, varying columns; they suit huge, time-ordered data read by key.
  • Graph databases store nodes and edges and make connection questions natural.
  • Both are specialised; a relational database handles small and medium versions of these tasks.
  • Wide-column is not the same as column-oriented analytics.

Practice

1. What is a node, and what is an edge?

Show answer

A node is a thing (a person); an edge is a link between two nodes (follows).

2. In the sensor demo, how many columns does sensor 2 have?

Show answer

One (2026-01-02).

3. Who was three steps from Rina in the graph demo?

Show answer

Amy.

4. Why is a graph query awkward in plain SQL?

Show answer

Following links of unknown length needs a repeated (recursive) join, which is harder to write and read.

5. Why should you not choose a specialised database first?

Show answer

A relational database handles small and medium versions of these tasks, and you can move later when a real need appears.

6. What is the difference between wide-column and column-oriented?

Show answer

Wide-column stores rows with many varying columns under a row key; column-oriented stores each column of an ordinary table together for fast analysis.