Skip to content

Commit 1d99717

Browse files
authored
chore: optimise post analytics query (#3256)
Signed-off-by: Viktor Poluksht <viktor@daily.dev>
1 parent fa35616 commit 1d99717

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

clickhouse/migrations/20251104112214_post_analytics_events_mv.down.sql

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ALTER TABLE api.post_analytics_events_mv
2+
MODIFY QUERY
3+
SELECT target_id AS post_id,
4+
sumIf(toUInt64(1), event_name = 'share post') AS shares_external,
5+
sumIf(toUInt64(1), event_name = 'share to squad') AS shares_internal,
6+
uniqStateIf(user_id, event_name = 'bookmark post') AS bookmarks,
7+
sumIf(toUInt64(1), (event_name = 'impression') AND (target_type = 'post')) AS impressions,
8+
uniqStateIf(user_id, (event_name = 'impression') AND (target_type = 'post')) AS reach,
9+
uniqStateIf(user_id, (event_name = 'impression') AND (target_type = 'post')) AS reach_all,
10+
sumIf(toUInt64(1), (event_name = 'click') AND (target_type = 'post')) AS clicks,
11+
sumIf(toUInt64(1), (event_name = 'go to link') AND (target_type = 'post')) AS go_to_link,
12+
max(server_timestamp) AS created_at
13+
FROM events.raw_events
14+
WHERE (event_name IN ('share post', 'bookmark post', 'impression', 'share to squad', 'click', 'go to link'))
15+
AND (target_id IS NOT NULL)
16+
AND (target_type = 'post')
17+
AND (server_timestamp > '2025-10-08 13:00:00')
18+
GROUP BY target_id
19+
SETTINGS materialized_views_ignore_errors = 1;

src/cron/postAnalyticsClickhouse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const postAnalyticsClickhouseCron: Cron = {
5757
sum(clicks_ads) AS "clicksAds",
5858
sum(go_to_link) AS "goToLink"
5959
FROM api.post_analytics
60-
FINAL
60+
WHERE "created_at" > {lastRunAt: DateTime}
6161
GROUP BY id
6262
HAVING "updatedAt" > {lastRunAt: DateTime}
6363
ORDER BY "updatedAt" DESC;

0 commit comments

Comments
 (0)