From c033b0582df5282545c77d76a37f754b670011cb Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Mon, 25 Mar 2024 19:13:54 -0700 Subject: [PATCH] create join table and remove subquery from insert --- db/clickhouse/migrations/02_add_visit_id.sql | 43 +++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/db/clickhouse/migrations/02_add_visit_id.sql b/db/clickhouse/migrations/02_add_visit_id.sql index 8aa5e6a1..5dd7bcd1 100644 --- a/db/clickhouse/migrations/02_add_visit_id.sql +++ b/db/clickhouse/migrations/02_add_visit_id.sql @@ -1,3 +1,22 @@ +CREATE TABLE umami.website_event_join +( + session_id UUID, + visit_id UUID, + created_at DateTime('UTC') +) + engine = MergeTree + ORDER BY (session_id, created_at) + SETTINGS index_granularity = 8192; + +INSERT INTO umami.website_event_join +SELECT DISTINCT + s.session_id, + generateUUIDv4() visit_id, + s.created_at +FROM (SELECT DISTINCT session_id, + date_trunc('hour', created_at) created_at + FROM website_event) s; + -- create new table CREATE TABLE umami.website_event_new ( @@ -33,7 +52,7 @@ CREATE TABLE umami.website_event_new INSERT INTO umami.website_event_new SELECT we.website_id, we.session_id, - we2.visit_id, + j.visit_id, we.event_id, we.hostname, we.browser, @@ -56,15 +75,17 @@ SELECT we.website_id, we.created_at, we.job_id FROM umami.website_event we -JOIN (SELECT DISTINCT - s.session_id, - generateUUIDv4() visit_id, - s.created_at -FROM (SELECT DISTINCT session_id, - date_trunc('hour', created_at) created_at - FROM umami.website_event) s) we2 - ON we.session_id = we2.session_id - and date_trunc('hour', we.created_at) = we2.created_at; +JOIN umami.website_event_join j + ON we.session_id = j.session_id + and date_trunc('hour', we.created_at) = j.created_at +WHERE we.created_at > '2023-03-31'; RENAME TABLE umami.website_event TO umami.website_event_old; -RENAME TABLE umami.website_event_new TO umami.website_event; \ No newline at end of file +RENAME TABLE umami.website_event_new TO umami.website_event; + +/* + + DROP TABLE umami.website_event_old + DROP TABLE umami.website_event_join + + */ \ No newline at end of file