Skip to main content
A DateTime64 timestamp with microsecond or nanosecond precision is fine in the primary key of a MergeTree table (the ORDER BY key, unless you define PRIMARY KEY separately) as long as it is the last key column. The primary index is sparse: it has one entry per granule (8192 rows by default), so it does not grow with the number of distinct values, and it finds a time range equally well at any precision. The precision matters for the key columns that come after the timestamp. When almost every row has its own timestamp, the rows are not ordered by those columns in any useful way, and the index cannot help with filters on them. If you filter on other columns as well, put them before the timestamp, or round the timestamp in the key. The rest of this article measures the difference.

Example data

Four tables hold the same 5 million log rows: 12 hours of data from 50 services, about 116 rows per second, every row with its own microsecond timestamp. event_time is a copy of the timestamp truncated to the second. The tables differ only in the sorting key.

Granules read by each sorting key

EXPLAIN ESTIMATE returns the number of granules (marks) that a query has to read after the primary index is applied. Each of the four tables has 611 granules.

A time range only

A filter on a five-minute window of ts:
  • logs_ts, with the microsecond timestamp first, reads 5 granules. High precision does no harm here.
  • logs_second reads the whole table. Its first key column is event_time, the query filters on ts, and ClickHouse does not derive a condition on one stored column from a condition on another.
  • logs_hour reads the 52 granules of that hour. The key contains the expression toStartOfHour(ts), and ClickHouse applies a condition on ts to it, so queries do not have to change.
  • logs_service reads 101 granules: about two for each of the 50 services.
For logs_second, the query has to filter on the key column. It then reads the same 5 granules as logs_ts, so rounding to the second gains nothing for this query:

A time range and another column

A filter on one service and one hour (on event_time for logs_second, on ts for the other tables):
  • logs_ts and logs_second read every granule of the hour. service comes after the timestamp in their keys and does not narrow the range. Rounding to the second does not change that: at 116 rows per second, a granule of 8192 rows spans about 70 seconds, so rows with the same event_time never fill a granule.
  • logs_hour reads 5 granules and logs_service reads 2, because all rows of one service within an hour, or of one service overall, are stored together.
A key column helps only when the columns before it have the same values over ranges that are much longer than a granule. This is the rule given in Selecting a primary key.

What a rounded copy is good for

A second-precision copy of the timestamp does not improve the index, but it is cheaper to store and to process. In a table that is sorted by time, consecutive rows share the same second, and the column compresses to almost nothing:
The sizes are from ClickHouse 26.12 with LZ4 compression, and they differ between versions and codecs. Queries that work at the granularity of seconds or coarser also run faster on the smaller column. On the test machine, SELECT toStartOfMinute(event_time) AS minute, count() FROM logs_ts GROUP BY minute ran three to four times faster than the same query with toStartOfMinute(ts).

Recommendations

  • If queries filter by time only, sort by the timestamp as it is (ORDER BY ts). Rounding it does not reduce the number of granules read.
  • If queries filter by other columns and a time range, put those columns first, in order of increasing cardinality, and the full-precision timestamp last: ORDER BY (service, ts).
  • If you need both, start the key with a rounded timestamp, for example ORDER BY (toStartOfHour(ts), service, ts). Choose the rounding so that one value covers many granules. With an expression in the key, queries keep filtering on ts.
  • If dashboards filter and group by seconds, minutes or hours, add a DateTime column next to the DateTime64 one, as event_time above, and use it in those queries. If you put the copy into the sorting key instead of an expression, queries must filter on the copy.
See also Choosing a primary key, Ordering key columns efficiently and Time granularity matters.