I have a few billions rows in table for 4k mutable params and I need to get last values for 500 of them My table is partitioned by day and order by param ids so i need just to find last record with needed id
SELECT max(time)
FROM obj_ntgres.param_values_history
PREWHERE param_id = 4171
work slow: Elapsed: 0.437 sec. Processed 2.56 million rows, 5.21 MB (5.87 million rows/s., 11.92 MB/s.)
SELECT *
FROM obj_ntgres.param_values_history
PREWHERE param_id = 4171
ORDER BY time DESC
LIMIT 1
slower: 1 rows in set. Elapsed: 3.413 sec. Processed 2.56 million rows, 5.45 MB (751.21 thousand rows/s., 1.60 MB/s.)
Table
CREATE TABLE obj_ntgres.param_values_history (
time DateTime,
param_id UInt16,
param_value Float32,
param_value_quality Decimal(1, 0),
msec Decimal(3, 0)
) ENGINE = MergeTree PARTITION BY toStartOfDay(time)
ORDER BY
param_id SETTINGS index_granularity = 8192
Maybe you have some ideas how to make it faster?
I mean: find last element without using max() on all table