Skip to content

Commit

Permalink
fix: increase performance of outdated SDK query (#7226)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sjaanus authored May 31, 2024
1 parent de74faa commit 3c73ce9
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/lib/db/client-instance-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 3c73ce9

Please sign in to comment.