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







