Skip to main content
To get the latest row for each key (a “last point” query), use 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:
For columns that are not Nullable, an aggregation gives the same result: argMax(status, updated_at) returns the status of the row with the largest updated_at:
Three things to know about this query:
  • argMax skips the rows in which its first argument is NULL, so with Nullable columns the values of one result row can come from different source rows. If status were Nullable(String) and the latest row of a device had NULL in it, argMax(status, updated_at) would return the status of an older row, next to the battery of the latest one. To take all values from the same row, pass them as one tuple, for example argMax((status, battery), updated_at): the tuple itself is not NULL, so the row is not skipped and the NULL inside 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 that argMax picks is not deterministic. To break ties, pass a tuple as the second argument, for example argMax(status, (updated_at, battery)).
  • Give the results names that differ from the source columns. With max(updated_at) AS updated_at in the same query, updated_at inside argMax refers to the alias, and the query fails with Aggregate function max(updated_at) AS updated_at is found inside another aggregate function in query. (ILLEGAL_AGGREGATION). The setting prefer_column_name_to_alias changes 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:
The view only sees rows inserted after it was created, so backfill the existing rows with the same 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.
New rows now update the target table. Read it with 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:
A projection with 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:
The layout of the plan depends on the version. The line to look for is 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 ... DELETE mutation rebuilds it. A lightweight DELETE on a table with projections fails with SUPPORT_IS_DISABLED unless you change the MergeTree setting lightweight_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.
For the full comparison, see Materialized views versus projections and the projections guide.