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 ofts:
logs_ts, with the microsecond timestamp first, reads 5 granules. High precision does no harm here.logs_secondreads the whole table. Its first key column isevent_time, the query filters onts, and ClickHouse does not derive a condition on one stored column from a condition on another.logs_hourreads the 52 granules of that hour. The key contains the expressiontoStartOfHour(ts), and ClickHouse applies a condition ontsto it, so queries do not have to change.logs_servicereads 101 granules: about two for each of the 50 services.
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 (onevent_time for logs_second, on ts for the other tables):
logs_tsandlogs_secondread every granule of the hour.servicecomes 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 sameevent_timenever fill a granule.logs_hourreads 5 granules andlogs_servicereads 2, because all rows of one service within an hour, or of one service overall, are stored together.
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: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 onts. - If dashboards filter and group by seconds, minutes or hours, add a
DateTimecolumn next to theDateTime64one, asevent_timeabove, 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.