From ab253d12b5c7935ce4ef16d9f09dcba58300cda5 Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Tue, 19 Sep 2023 18:42:46 +0100 Subject: [PATCH] chore: even faster max timestamp query --- posthog/clickhouse/system_status.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/posthog/clickhouse/system_status.py b/posthog/clickhouse/system_status.py index 387934eeaf33e..2317e41c39e1d 100644 --- a/posthog/clickhouse/system_status.py +++ b/posthog/clickhouse/system_status.py @@ -103,7 +103,12 @@ def system_status() -> Generator[SystemStatusRow, None, None]: # This timestamp is a naive timestamp (does not include a timezone) # ClickHouse always stores timezone agnostic unix timestamp # See https://clickhouse.com/docs/en/sql-reference/data-types/datetime#usage-remarks - last_event_ingested_timestamp = sync_execute("SELECT max(_timestamp) FROM events")[0][0] + last_event_ingested_timestamp = sync_execute( + """ + SELECT max(_timestamp) FROM events + WHERE timestamp >= now() - INTERVAL 1 HOUR + """ + )[0][0] # Therefore we can confidently apply the UTC timezone last_event_ingested_timestamp_utc = last_event_ingested_timestamp.replace(tzinfo=ZoneInfo("UTC"))