Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

perf(properties): list event property keys via GROUP BY so the projection applies (#457)

Ritik Jainde0720a33fc4
diff --git a/packages/db/src/services/event.service.ts b/packages/db/src/services/event.service.ts
index 68527f501..a20dfc05c 100644
--- a/packages/db/src/services/event.service.ts
+++ b/packages/db/src/services/event.service.ts
@@ -1251,14 +1251,23 @@ export async function listEventPropertiesCore(input: {
columns: readonly string[];
properties: Array<{ property_key: string; event_name: string }>;
}> {
+ // GROUP BY rather than DISTINCT: both return the same set of
+ // (property_key, name) pairs, but a multi-column DISTINCT cannot be matched
+ // against the epv_keys aggregating projection, so it degrades to a full scan
+ // of the project's MV slice. `name` is a tie-breaker for the ORDER BY —
+ // without it the LIMIT slices an arbitrary subset of the rows sharing a
+ // property_key, which is also what made the two spellings return different
+ // windows.
const builder = clix(ch)
.select<{ property_key: string; event_name: string }>([
- 'distinct property_key',
+ 'property_key',
'name as event_name',
])
.from(TABLE_NAMES.event_property_values_mv)
.where('project_id', '=', input.projectId)
+ .groupBy(['property_key', 'name'])
.orderBy('property_key', 'ASC')
+ .orderBy('name', 'ASC')
.limit(500);
if (input.eventName) {
diff --git a/packages/mcp/src/tools/analytics/property-values.ts b/packages/mcp/src/tools/analytics/property-values.ts
index 291dff5d0..c0f2ce868 100644
--- a/packages/mcp/src/tools/analytics/property-values.ts
+++ b/packages/mcp/src/tools/analytics/property-values.ts
@@ -26,14 +26,20 @@ export function registerPropertyValueTools(
async ({ projectId: inputProjectId, eventName }) =>
withErrorHandling(async () => {
const projectId = await resolveProjectId(context, inputProjectId);
+ // GROUP BY rather than DISTINCT so the epv_keys projection can serve
+ // this (a multi-column DISTINCT cannot be matched against an
+ // aggregating projection); `name` tie-breaks the ORDER BY so the
+ // LIMIT window is deterministic. See listEventPropertiesCore.
const builder = clix(ch)
.select<{ property_key: string; event_name: string }>([
- 'distinct property_key',
+ 'property_key',
'name as event_name',
])
.from(TABLE_NAMES.event_property_values_mv)
.where('project_id', '=', projectId)
+ .groupBy(['property_key', 'name'])
.orderBy('property_key', 'ASC')
+ .orderBy('name', 'ASC')
.limit(500);
if (eventName) {