From 3c73ce9dd9d4c616cfbea5ebf37af84e158e4125 Mon Sep 17 00:00:00 2001 From: Jaanus Sellin Date: Fri, 31 May 2024 12:39:52 +0300 Subject: [PATCH] fix: increase performance of outdated SDK query (#7226) Joining might not always be the best solution. If a table contains too much data, and you later run sorting on top of it, it will be slow. In this case, we will first reduce the instances table to a minimal version because instances usually share the same SDK versions. Only after that, we join. Based on some customer data, we reduced query time from 3000ms to 60ms. However, this will vary based on the number of instances the customer has. --- src/lib/db/client-instance-store.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/lib/db/client-instance-store.ts b/src/lib/db/client-instance-store.ts index 0b8533f395..8f1eb99074 100644 --- a/src/lib/db/client-instance-store.ts +++ b/src/lib/db/client-instance-store.ts @@ -222,15 +222,22 @@ export default class ClientInstanceStore implements IClientInstanceStore { projectId: string, ): Promise<{ sdkVersion: string; applications: string[] }[]> { const rows = await this.db + .with( + 'instances', + this.db + .select('app_name', 'sdk_version') + .distinct() + .from('client_instances'), + ) .select([ - 'ci.sdk_version as sdkVersion', + 'i.sdk_version as sdkVersion', this.db.raw('ARRAY_AGG(DISTINCT cme.app_name) as applications'), ]) .from('client_metrics_env as cme') .leftJoin('features as f', 'f.name', 'cme.feature_name') - .leftJoin('client_instances as ci', 'ci.app_name', 'cme.app_name') + .leftJoin('instances as i', 'i.app_name', 'cme.app_name') .where('f.project', projectId) - .groupBy('ci.sdk_version'); + .groupBy('i.sdk_version'); return rows; }