LIMIT 1 BY or the argMax aggregate function. Both read every row of the table. To avoid that, keep the argMax states precomputed, either in an AggregatingMergeTree table filled by a materialized view, or in an aggregate projection of the source table. Both store the same states. They differ in how you define, query and maintain them.
Example data
Compute the latest row at query time
LIMIT 1 BY keeps the first row of each group in the order given by ORDER BY:
Nullable, an aggregation gives the same result: argMax(status, updated_at) returns the status of the row with the largest updated_at:
argMaxskips the rows in which its first argument isNULL, so withNullablecolumns the values of one result row can come from different source rows. IfstatuswereNullable(String)and the latest row of a device hadNULLin it,argMax(status, updated_at)would return thestatusof an older row, next to thebatteryof the latest one. To take all values from the same row, pass them as one tuple, for exampleargMax((status, battery), updated_at): the tuple itself is notNULL, so the row is not skipped and theNULLinside it is returned. The states that the materialized view and the projection below store behave in the same way.- If several rows of a group share the largest
updated_at, the row thatargMaxpicks is not deterministic. To break ties, pass a tuple as the second argument, for exampleargMax(status, (updated_at, battery)). - Give the results names that differ from the source columns. With
max(updated_at) AS updated_atin the same query,updated_atinsideargMaxrefers to the alias, and the query fails withAggregate function max(updated_at) AS updated_at is found inside another aggregate function in query. (ILLEGAL_AGGREGATION). The settingprefer_column_name_to_aliaschanges this rule.
Precompute with a materialized view
Create a target table that stores aggregate function states, and a materialized view that writes them with the-State combinator:
SELECT. Because max and argMax return the same result when a row is counted twice, it does no harm if a row arrives through both the view and the backfill.
GROUP BY and the -Merge combinator, because it holds several rows per device_id until background merges combine them:
Precompute with a projection
A projection needs no second table and no backfill.MATERIALIZE PROJECTION builds it for the parts that already exist, and every later insert writes it together with the new part:
GROUP BY stores aggregate function states, like the target table of the materialized view above. The system.projection_parts_columns table shows them: the column argMax(status, updated_at) of this projection has the type AggregateFunction(argMax, String, DateTime).
You do not query a projection by name. Run the aggregation on the source table, and ClickHouse reads the projection when the query groups by the same key and only calls aggregate functions that the projection contains, with the same arguments:
ReadFromMergeTree (latest_by_device), which names the projection instead of default.device_status. After the query has run, the projections column of system.query_log lists the projection as well; see How to confirm if a projection is used by the query. The LIMIT 1 BY query, or an aggregation with different arguments such as argMax(status, (updated_at, battery)), reads the base table instead.
Which one to choose
- A projection is shorter to define, your queries stay unchanged, and it follows the source table: an
ALTER TABLE ... DELETEmutation rebuilds it. A lightweightDELETEon a table with projections fails withSUPPORT_IS_DISABLEDunless you change theMergeTreesettinglightweight_mutation_projection_mode. - The target of a materialized view is an ordinary table with its own sorting key, partitioning and TTL, and the view can filter or join before it aggregates. You have to backfill it and query it explicitly, and it does not change when rows of the source table are deleted or updated.