Quentin Smith
07/07/2026, 2:30 AMSELECT * FROM observation WHERE greptime_timestamp = (SELECT greptime_timestamp FROM observation ORDER BY greptime_timestamp DESC LIMIT 1);
but it appears to be doing a full table scan and takes many seconds. I'll attach an explain analyze verbose in a thread.Quentin Smith
07/07/2026, 2:30 AMdiscord9
07/07/2026, 6:20 AMSELECT
t.*
FROM (
SELECT
greptime_timestamp
FROM
<table_name>
ORDER BY
greptime_timestamp DESC
LIMIT
1
) AS latest
JOIN <table_name> AS t
ON t.greptime_timestamp = latest.greptime_timestamp;Quentin Smith
07/07/2026, 8:51 PMQuentin Smith
07/07/2026, 10:50 PMmetric_name would fetch)
I tried
SELECT greptime_timestamp, dst_port FROM netflow_raw WHERE src = '172.30.96.104' AND rank() OVER (PARTITION BY dst_port ORDER BY greptime_timestamp DESC) = 1 ORDER BY greptime_timestamp DESC LIMIT 10;
to get the latest row for each value of the dst_port tag, but that produces an error
This feature is not implemented: Physical plan does not support logical expression WindowFunction(WindowFunction { fun: WindowUDF(WindowUDF { inner: Rank { name: "rank", signature: Signature { type_signature: Nullary, volatility: Immutable, parameter_names: None }, rank_type: Basic } }), params: WindowFunctionParams { args: [], partition_by: [Column(Column { relation: Some(Bare { table: "netflow_raw" }), name: "dst_port" })], order_by: [Sort { expr: Column(Column { relation: Some(Bare { table: "netflow_raw" }), name: "greptime_timestamp" }), asc: false, nulls_first: true }], window_frame: WindowFrame { units: Range, start_bound: Preceding(IntervalMonthDayNano("NULL")), end_bound: CurrentRow, is_causal: false }, filter: None, null_treatment: None, distinct: false } })
Obviously for this trivial query I could use
SELECT MAX(greptime_timestamp), dst_port FROM netflow_raw WHERE src = '172.30.96.104' GROUP BY dst_port ORDER BY MAX(greptime_timestamp) DESC LIMIT 10;
but I want to access all the fields of the matching row, not just the timestamp value.Quentin Smith
07/08/2026, 12:13 AMSELECT DISTINCT ON (dst_port) * FROM netflow_raw WHERE src = '172.30.96.104' ORDER BY dst_port, greptime_timestamp DESC LIMIT 10;
with an added WHERE greptime_timestamp > (now() - interval '1 day') to control the lookback window?discord9
07/08/2026, 3:36 AMSELECT
dst_port,
last_value(greptime_timestamp ORDER BY greptime_timestamp) AS latest_ts,
last_value(dst_addr ORDER BY greptime_timestamp) AS dst_addr,
last_value(byte_count ORDER BY greptime_timestamp) AS byte_count,
FROM netflow_raw
WHERE src = '172.30.96.104'
AND greptime_timestamp > now() - INTERVAL '1 day'
GROUP BY dst_port
ORDER BY latest_ts DESC
LIMIT 10;
here is a longer explain:
DISTINCT ON is a valid and concise way to express the query, but for better performance in GreptimeDB, especially on large time-series tables, prefer the ordered aggregate form with last_value(... ORDER BY greptime_timestamp). It can use the last-row selector optimization. The tradeoff is that the columns to return must be listed explicitly instead of using SELECT *.
Quentin Smith
07/08/2026, 3:37 AMlast_value can only operate on one column at a time, thoughQuentin Smith
07/08/2026, 3:37 AMQuentin Smith
07/08/2026, 3:38 AMdiscord9
07/08/2026, 4:51 AMSELECT *
FROM (
SELECT DISTINCT ON (dst_port) *
FROM netflow_raw
WHERE src = '172.30.96.104'
AND greptime_timestamp > now() - INTERVAL '1 day'
ORDER BY dst_port, greptime_timestamp DESC
) latest_per_port
ORDER BY greptime_timestamp DESC
LIMIT 10;
or like this:
SELECT
greptime_timestamp,
src,
dst_port,
dst_addr,
byte_count,
flow_id
FROM (
SELECT
*,
rank() OVER (
PARTITION BY dst_port
ORDER BY greptime_timestamp DESC
) AS r
FROM netflow_raw
WHERE src = '172.30.96.104'
AND greptime_timestamp > now() - INTERVAL '1 day'
) t
WHERE r = 1
ORDER BY greptime_timestamp DESC
LIMIT 10;