query optimizations on session property filters

This commit is contained in:
Francis Cao
2026-05-04 12:42:25 -07:00
parent 5fc02b6aef
commit a113f5be79
4 changed files with 68 additions and 7 deletions
@@ -108,3 +108,19 @@ SELECT
groupArrayState(data_type)
FROM umami.session_data
GROUP BY website_id, session_id, distinct_id;
ALTER TABLE umami.session_data
ADD PROJECTION session_data_property_filter_projection (
SELECT *
ORDER BY (
website_id,
data_key,
data_type,
string_value,
number_value,
date_value,
session_id
)
);
ALTER TABLE umami.session_data MATERIALIZE PROJECTION session_data_property_filter_projection;
+16
View File
@@ -90,6 +90,22 @@ ENGINE = ReplacingMergeTree
ORDER BY (website_id, session_id, data_key)
SETTINGS index_granularity = 8192;
ALTER TABLE umami.session_data
ADD PROJECTION session_data_property_filter_projection (
SELECT *
ORDER BY (
website_id,
data_key,
data_type,
string_value,
number_value,
date_value,
session_id
)
);
ALTER TABLE umami.session_data MATERIALIZE PROJECTION session_data_property_filter_projection;
-- stats hourly
CREATE TABLE umami.website_event_stats_hourly
(
+18 -3
View File
@@ -253,6 +253,10 @@ function getPropertyFilterQuery(
const table = propertyType === 'event' ? 'event_data' : 'session_data final';
const column = propertyType === 'event' ? 'event_id' : 'session_id';
const outerColumn = propertyType === 'event' ? 'event_id' : 'website_event.session_id';
const dateFilter =
propertyType === 'event'
? `and created_at between {startDate:DateTime64} and {endDate:DateTime64}`
: '';
filters.forEach(({ propertyName, dataType, operator, value }, i) => {
const keyParam = `pf_key_${i}`;
@@ -333,15 +337,26 @@ function getPropertyFilterQuery(
}
}
parts.push(`and ${outerColumn} in (
select ${column}
if (propertyType === 'session') {
parts.push(`and tuple(website_event.website_id, website_event.session_id) in (
select website_id, session_id
from ${table}
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and data_key = {${keyParam}:String}
and data_type = ${dataType}
and ${condition}
)`);
} else {
parts.push(`and ${outerColumn} in (
select ${column}
from ${table}
where website_id = {websiteId:UUID}
${dateFilter}
and data_key = {${keyParam}:String}
and data_type = ${dataType}
and ${condition}
)`);
}
});
return { sql: parts.join('\n'), params };
+18 -4
View File
@@ -289,6 +289,8 @@ function getPropertyFilterQuery(
const column = propertyType === 'event' ? 'website_event_id' : 'session_id';
const outerColumn =
propertyType === 'event' ? 'website_event.event_id' : 'website_event.session_id';
const dateFilter =
propertyType === 'event' ? `and created_at between {{startDate}} and {{endDate}}` : '';
filters.forEach(({ propertyName, dataType, operator, value }, i) => {
const keyParam = `pf_key_${i}`;
@@ -372,15 +374,27 @@ function getPropertyFilterQuery(
}
}
parts.push(`and ${outerColumn} in (
select ${column}
if (propertyType === 'session') {
parts.push(`and exists (
select 1
from ${table}
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
where website_id = website_event.website_id
and session_id = website_event.session_id
and data_key = {{${keyParam}}}
and data_type = ${dataType}
and ${condition}
)`);
} else {
parts.push(`and ${outerColumn} in (
select ${column}
from ${table}
where website_id = {{websiteId::uuid}}
${dateFilter}
and data_key = {{${keyParam}}}
and data_type = ${dataType}
and ${condition}
)`);
}
});
return { sql: parts.join('\n'), params };