ClickHouse 00:00:01 Dinner orders arrive in a batch A spice shop records ingredient quantities at several sites. Tonight it needs an analytical total for cumin. Follow one block of unordered events through a ClickHouse MergeTree table. Rows arrive in any order. Ordered by site, then time. The site column contains repeated values next to one another after sorting. Encoding each column separately helps compress that repetition. The part is visible to queries. The sorting key determines physical locality inside a part. A columnar layout separately lets the reader avoid fetching fields that the analyst never asked for. 00:00:37 Merge sorted parts without rewriting every insert A second batch creates another sorted part. The storage engine can make both available before paying for a larger merge. Reads may temporarily consult more than one part. Each is sorted in itself. Both are read from the front, always taking the smaller key. Eight keys in one order. The new part becomes active and the old ones inactive, in one step. After the replacement, active readers can use the appropriate snapshot of parts. Retiring the old files is a later housekeeping action. 00:01:10 Skip salt when counting cumin The kitchen asks for cumin over one key range. The sparse primary index identifies candidate granules rather than maintaining one tree entry for every order. Eight orders, three columns. In the small example the first granule starts at key one and the second at key five. The filter lets us skip the first granule. Four values. The salt column is never opened. The four requested quantities are two, five, one and four. The sum is twelve. Two independent savings explain the result. The sorting key excludes a range of rows, while the projection avoids the salt column. Inspect both when a real query reads too much. 00:01:54 Tiny inserts leave a backlog of parts The application changes from sending batches to sending individual rows. Throughput at the network socket looks fine, yet parts are now being created faster than background merges can absorb them. 2,000 parts appear. Assume merges remove 200 a second. Under the example rates, incoming parts exceed the number that merges can retire. The backlog grows even though each inserted row is small. Aggregate two thousand rows into one insert per second in this exercise. The database sees far fewer small parts to merge. Batching changes the number of parts created for the same useful data. Watch active parts and merge backlog together with visible insert latency. 00:02:37 Which order status is current? For a separate order-status table built with ReplacingMergeTree, two versions of one order can live in different parts before a merge. An ordinary row count need not equal the count of logical orders. Each insert made a part. SELECT count() says 2. Not yet merged. For this versioned example, select the value associated with the largest version for each order, or use appropriate FINAL semantics with their query cost. Some time later only the latest row is left. When the answer must reflect the latest version now, express the version rule in the query or use appropriate deduplication semantics. An eventual storage merge is an unreliable business clock.