From ba53bd7bf9b7d7742ca177c874f6ef99c2be924d Mon Sep 17 00:00:00 2001 From: Jaanus Sellin Date: Thu, 14 Mar 2024 12:11:15 +0200 Subject: [PATCH] refactor: optimize applications overview (#6548) There was no need to join the entire metrics table, as it is a huge table. We only needed all combinations of app_name, environment, and feature_name. The new query retrieves all this data, which will then be joined into the main query. --- src/lib/db/client-applications-store.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/lib/db/client-applications-store.ts b/src/lib/db/client-applications-store.ts index bef9e1f39d7..50b7657df71 100644 --- a/src/lib/db/client-applications-store.ts +++ b/src/lib/db/client-applications-store.ts @@ -292,6 +292,13 @@ export default class ClientApplicationsStore appName: string, ): Promise { const query = this.db + .with('metrics', (qb) => { + qb.distinct( + 'cme.app_name', + 'cme.environment', + 'cme.feature_name', + ).from('client_metrics_env as cme'); + }) .select([ 'f.project', 'cme.environment', @@ -302,7 +309,7 @@ export default class ClientApplicationsStore 'a.strategies', ]) .from({ a: 'client_applications' }) - .leftJoin('client_metrics_env as cme', 'cme.app_name', 'a.app_name') + .leftJoin('metrics as cme', 'cme.app_name', 'a.app_name') .leftJoin('features as f', 'cme.feature_name', 'f.name') .leftJoin('client_instances as ci', function () { this.on('ci.app_name', '=', 'cme.app_name').andOn(