:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(query-performance): add daily aggregate view over query_log_archive (#63384)
Andy Zhao6a8c00d6429a
diff --git a/posthog/clickhouse/migrations/0274_daily_aggregated_query_log_archive_view.py b/posthog/clickhouse/migrations/0274_daily_aggregated_query_log_archive_view.pynew file mode 100644index 000000000000..4506b66ec5a5--- /dev/null+++ b/posthog/clickhouse/migrations/0274_daily_aggregated_query_log_archive_view.py@@ -0,0 +1,10 @@+from posthog.clickhouse.client.connection import NodeRole+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions+from posthog.clickhouse.query_log_archive import DAILY_AGGREGATED_QUERY_LOG_ARCHIVE_VIEW_SQL++operations = [+ run_sql_with_exceptions(+ DAILY_AGGREGATED_QUERY_LOG_ARCHIVE_VIEW_SQL(),+ node_roles=[NodeRole.OPS],+ ),+]diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txtindex c0a98f4e36a6..229f5602b449 100644--- a/posthog/clickhouse/migrations/max_migration.txt+++ b/posthog/clickhouse/migrations/max_migration.txt@@ -1 +1 @@-0273_query_log_archive_ops_json\ No newline at end of file+0274_daily_aggregated_query_log_archive_viewdiff --git a/posthog/clickhouse/query_log_archive.py b/posthog/clickhouse/query_log_archive.pyindex 1d54d0bb5f06..74283994a1e5 100644--- a/posthog/clickhouse/query_log_archive.py+++ b/posthog/clickhouse/query_log_archive.py@@ -799,3 +799,67 @@ def QUERY_LOG_ARCHIVE_OPS_MV_SQL(view_name, dest_table):dest_table=dest_table,select_sql=MV_SELECT_SQL_OPS,)+++# ============================================================================+# Daily aggregate exposed to the data warehouse+# ----------------------------------------------------------------------------+# A non-materialized view that rolls query_log_archive up to one row per day per+# (team, user, query-attribution) tuple. The data warehouse ClickHouse source+# (posthog/temporal/data_imports/sources/clickhouse) syncs it into an internal+# project incrementally on `day`, so query-cost analysis can live in PostHog+# instead of Metabase.+#+# Reads the OPS-local sharded_query_log_archive directly (it carries the lc_* /+# ProfileEvents_* aliases and, being replicated, holds the full dataset on every+# OPS replica) so the scan stays on OPS and never touches the distributed read path.+#+# is_initial_query drops the per-shard sub-queries that would otherwise double-count.+# event_date < today() exposes a day only once it is complete: the warehouse sync+# advances its `day` cursor and never revisits a day, so a partial final day must+# never be visible.+# ============================================================================+DAILY_AGGREGATED_QUERY_LOG_ARCHIVE_VIEW = "daily_aggregated_query_log_archive"+++def DAILY_AGGREGATED_QUERY_LOG_ARCHIVE_VIEW_SQL(view_name=DAILY_AGGREGATED_QUERY_LOG_ARCHIVE_VIEW):+ return f"""+CREATE VIEW IF NOT EXISTS {view_name} AS+SELECT+ event_date AS day,+ team_id,+ user,+ current_database,+ query_kind,+ lc_kind,+ lc_access_method,+ lc_query_type,+ lc_product,+ lc_name,+ lc_feature,+ lc_query__kind,+ lc_api_key_label,+ count() AS query_count,+ sum(read_bytes) AS read_bytes,+ sum(read_rows) AS read_rows,+ sum(query_duration_ms) AS query_duration_ms,+ sum(ProfileEvents_OSCPUVirtualTimeMicroseconds) AS cpu_microseconds,+ countIf(exception_code != 0) AS error_count,+ countIf(exception_code IN (159, 160, 241)) AS timeout_oom_count+FROM {SHARDED_QUERY_LOG_ARCHIVE_TABLE}+WHERE is_initial_query AND event_date < today()+GROUP BY+ day,+ team_id,+ user,+ current_database,+ query_kind,+ lc_kind,+ lc_access_method,+ lc_query_type,+ lc_product,+ lc_name,+ lc_feature,+ lc_query__kind,+ lc_api_key_label+"""diff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.pyindex 05f5c76e858a..c910bc867954 100644--- a/posthog/clickhouse/schema.py+++ b/posthog/clickhouse/schema.py@@ -84,6 +84,7 @@ from posthog.clickhouse.property_values import (PROPERTY_VALUES_TABLE_SQL,)from posthog.clickhouse.query_log_archive import (+ DAILY_AGGREGATED_QUERY_LOG_ARCHIVE_VIEW_SQL,DISTRIBUTED_QUERY_LOG_ARCHIVE_OPS_TABLE_SQL,QUERY_LOG_ARCHIVE_OPS_MV,QUERY_LOG_ARCHIVE_OPS_MV_SQL,@@ -512,6 +513,7 @@ CREATE_VIEW_QUERIES = (SESSIONS_VIEW_SQL,RAW_SESSIONS_CREATE_OR_REPLACE_VIEW_SQL,RAW_SESSIONS_CREATE_OR_REPLACE_VIEW_SQL_V3,+ DAILY_AGGREGATED_QUERY_LOG_ARCHIVE_VIEW_SQL,)