1. Query router engine
Distributed is not a storage engine — it routes queries across the shards of a cluster.
ENGINE = Distributed(Typorithm · medium
Step 1 of 8 · intro card
Scale & Ops
ENGINE = Distributed(cluster, db, local_table, sharding_key) is a query router over shards: reads fan out and merge, writes route by the sharding key.
A Distributed table stores no data itself — it is a thin layer that points at a local table on every shard of a cluster.
On reads, the query fans out to each shard, runs locally, and the coordinator merges the partial results into one answer.
On writes, the sharding key (here cityHash64(user_id)) decides which shard each row belongs to, spreading data evenly.
Distributed routes queries; it stores no data.
Reads fan out to shards, then merge.
The sharding key routes each written row.
CREATE TABLE events_dist AS events
ENGINE = Distributed(
my_cluster,
default,
events,
cityHash64(user_id)
);Connect each part of the code to the concept before you type.
Distributed is not a storage engine — it routes queries across the shards of a cluster.
ENGINE = Distributed(The cluster name resolves to the set of shards and replicas defined in the server config.
my_clusterEach shard stores its slice in this local MergeTree table; the Distributed table just points at it.
eventsThe sharding key decides which shard each inserted row lands on; hashing user_id spreads load evenly.
cityHash64(user_id)Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.