From 8d1a62fb7bc45f4fdbb08ae619ceb7ef5f8461d5 Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Fri, 17 Apr 2026 11:24:57 -0700 Subject: [PATCH] getValues performance fix for relational --- src/queries/sql/getValues.ts | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/queries/sql/getValues.ts b/src/queries/sql/getValues.ts index 8573335bf..908be4126 100644 --- a/src/queries/sql/getValues.ts +++ b/src/queries/sql/getValues.ts @@ -1,8 +1,13 @@ import clickhouse from '@/lib/clickhouse'; +import { FILTER_COLUMNS, SESSION_COLUMNS } from '@/lib/constants'; import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db'; import prisma from '@/lib/prisma'; import type { QueryFilters } from '@/lib/types'; +const SESSION_DB_COLUMNS = new Set( + SESSION_COLUMNS.map(col => FILTER_COLUMNS[col as keyof typeof FILTER_COLUMNS]).filter(Boolean), +); + const FUNCTION_NAME = 'getValues'; export async function getValues( @@ -45,13 +50,27 @@ async function relationalQuery(websiteId: string, column: string, filters: Query } } + if (SESSION_DB_COLUMNS.has(column)) { + return rawQuery( + ` + select ${column} as "value", count(*) as "count" + from session + where website_id = {{websiteId::uuid}} + and created_at between {{startDate}} and {{endDate}} + ${searchQuery} + group by 1 + order by 2 desc + limit 10 + `, + { websiteId, startDate, endDate, search: `%${search}%`, ...params }, + FUNCTION_NAME, + ); + } + return rawQuery( ` select ${column} as "value", count(*) as "count" from website_event - inner join session - on session.session_id = website_event.session_id - and session.website_id = website_event.website_id where website_event.website_id = {{websiteId::uuid}} and website_event.created_at between {{startDate}} and {{endDate}} ${searchQuery} @@ -60,13 +79,7 @@ async function relationalQuery(websiteId: string, column: string, filters: Query order by 2 desc limit 10 `, - { - websiteId, - startDate, - endDate, - search: `%${search}%`, - ...params, - }, + { websiteId, startDate, endDate, search: `%${search}%`, ...params }, FUNCTION_NAME, ); }