liveBook · Manning

Cardinality

As explained in Graph Algorithms for Data Science

Query cardinality is the number of rows or records in the input or intermediate result stream available to a query operation.

Query cardinality is the number of rows or records entering a Cypher operation. Every clause operates once for each incoming row, so cardinality determines both what an aggregation counts and how much work a query performs. section 4.3 section 4.7

A graph pattern can produce many rows for the same node. Matching users with incoming MENTIONS relationships yields one row per matched pattern, not one row per unique user. Therefore count(u) counts non-null occurrences, while count(distinct u) counts unique user values. section 4.3

Occurrences versus distinct users

MATCH (u:User)<-[:MENTIONS]-(:Tweet) produces 12,379 rows. Counting u returns 12,379 occurrences; counting distinct u returns 1,632 users mentioned at least once. Inspect the input rows before deciding which count answers the question. section 4.3

Intermediate cardinality matters when clauses run in sequence. A later MATCH runs once for every row emitted by the earlier MATCH, so two independent matches can multiply into a very large result stream and produce misleading aggregate counts. section 4.5

Keep cardinality under control by choosing a query shape that matches the analysis goal:

  • Use distinct when an aggregation should count unique nodes, relationships, properties, or expressions rather than repeated matches. section 4.3 section 4.7
  • Aggregate with WITH between MATCH clauses to reduce the intermediate stream before the next match runs. section 4.5
  • Use count { pattern } to count a node’s matching graph patterns without increasing the main query cardinality. section 4.4 section 4.7
  • Use PROFILE to compare query performance when cardinality may be high. section 4.3
Listing 4.17 Reducing cardinality between multiple MATCH clauses in a sequence
MATCH (u:User)
WITH count(u) AS countOfUsers
MATCH (t:Tweet)
RETURN count(*) AS numberOfRows, countOfUsers, count(t) AS countOfTweets  #1

This query reduces the user stream to one aggregate row with WITH before matching tweets, preventing the two independent MATCH clauses from multiplying their rows. section 4.5

How Graph Algorithms for Data Science covers it

How deep it goes
Defines row-stream cardinality and demonstrates distinct counts, counting subqueries, and intermediate reduction with Cypher queries.
Examples
Uses Twitter mentions, independent User and Tweet matches, and mention-distribution queries to expose repeated and multiplied rows.
What it assumes
Assumes a Neo4j database containing imported Twitter graph data for following the executable query examples.
What you can do after
Diagnose/debug with Cypher by identifying row multiplication, selecting distinct counts, and reducing intermediate streams with aggregation.
How it connects
Connects cardinality to Cypher aggregation, DISTINCT, MATCH, OPTIONAL MATCH, WITH, count {}, and PROFILE.

Also explained in

Compare how 8 books explain cardinality →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Algorithms for Data Science. The section links point to the parts of the book it draws on.