Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Optimise query

Blue Mouse3b42a4c46c5b
diff --git a/backend/migrations/clickhouse/2024_10_28_sort_of_working.js b/backend/migrations/clickhouse/2024_10_28_sort_of_working.js
index 0d412d720..6d8c8033e 100644
--- a/backend/migrations/clickhouse/2024_10_28_sort_of_working.js
+++ b/backend/migrations/clickhouse/2024_10_28_sort_of_working.js
@@ -1,10 +1,10 @@
const { queriesRunner, dbName } = require('./setup')
-// Generates insert queries from 2022-01-01 to 2023-10-17 AND where psid is NULL.
+// Generates insert queries from 2022-01-01 where psid is NULL.
const generateInsertQueries = () => {
const queries = []
const currentDate = new Date('2021-01-01')
- const endDate = new Date('2023-10-18')
+ const endDate = new Date()
while (currentDate <= endDate) {
// Calculate the start of next month
@@ -14,41 +14,32 @@ const generateInsertQueries = () => {
// Ensure we don't go past the end date
const queryEndDate = nextMonth > endDate ? endDate : nextMonth
- queries.push(`INSERT INTO ${dbName}.analytics_temp
- WITH
- SessionStarts AS (
+ queries.push(`
+ INSERT INTO ${dbName}.analytics_temp
+ WITH SessionStarts AS (
SELECT
- *,
- cityHash64(
- toString(toDate(created)),
- toString(br),
- toString(os),
- toString(lc),
- toString(cc),
- toString(rg),
- toString(ct)
- ) AS params_hash,
- 1000000 + dense_rank() OVER (
- PARTITION BY toDate(created)
- ORDER BY br, os, lc, cc, rg, ct, created
- ) AS session_id
+ *,
+ 1000000 + dense_rank() OVER (
+ PARTITION BY toDate(created)
+ ORDER BY br, os, lc, cc, rg, ct, created
+ ) AS session_id
FROM ${dbName}.analytics
WHERE isNull(psid) AND created >= parseDateTimeBestEffort('${currentDate.toISOString()}') AND created < parseDateTimeBestEffort('${queryEndDate.toISOString()}')
- ),
- SessionsAssigned AS (
+ ),
+ SessionsAssigned AS (
SELECT
- a.*,
- LAST_VALUE(a.session_id) OVER (
- PARTITION BY toDate(a.created), a.br, a.os, a.lc, a.cc, a.rg, a.ct
- ORDER BY a.created
- RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
- ) AS new_psid
+ a.*,
+ LAST_VALUE(a.session_id) OVER (
+ PARTITION BY toDate(a.created), a.br, a.os, a.lc, a.cc, a.rg, a.ct
+ ORDER BY a.created
+ RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
+ ) AS new_psid
FROM SessionStarts a
- )
- SELECT
+ )
+ SELECT
CASE
- WHEN isNotNull(psid) OR created >= '2023-10-18' THEN psid
- ELSE new_psid
+ WHEN isNotNull(psid) THEN psid
+ ELSE new_psid
END AS psid,
pid,
pg,
@@ -71,8 +62,8 @@ const generateInsertQueries = () => {
meta.value,
unique,
created
- FROM SessionsAssigned
- WHERE new_psid IS NOT NULL OR created >= '2023-10-18';`)
+ FROM SessionsAssigned
+ `)
// Move to the next month
currentDate.setMonth(currentDate.getMonth() + 1)
@@ -90,12 +81,12 @@ const queries = [
PARTITION BY toYYYYMM(created)
ORDER BY (pid, created);`,
+ // Step 2: Backfill session identifiers
...generateInsertQueries(),
- `INSERT INTO analytics_temp SELECT * FROM analytics WHERE created >= '2023-10-18';`,
+ `INSERT INTO analytics_temp SELECT * FROM analytics WHERE isNotNull(psid);`,
// Step 3: Swap Tables
- `RENAME TABLE ${dbName}.analytics TO ${dbName}.analytics_backup,
- ${dbName}.analytics_temp TO ${dbName}.analytics;`,
+ `RENAME TABLE ${dbName}.analytics TO ${dbName}.analytics_backup, ${dbName}.analytics_temp TO ${dbName}.analytics;`,
// Step 4: Drop Backup Table (Optional)
// `DROP TABLE ${dbName}.analytics_backup;`,