Skip to content

Commit 2e1e62e

Browse files
committed
perf(db): replace SELECT * with lean column projections and remove redundant sorting
1 parent fb1ce36 commit 2e1e62e

1 file changed

Lines changed: 49 additions & 34 deletions

File tree

‎src/lib/db/db-store.ts‎

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ export type GitHubUserRow<TRaw = GitHubUserData, TScores = CalculateUserScoreRes
2323
updated_at: Date;
2424
};
2525

26+
export type LeaderboardUserRow = Pick<
27+
GitHubUserRow,
28+
| "username"
29+
| "name"
30+
| "avatar_url"
31+
| "repo_score"
32+
| "pr_score"
33+
| "contribution_score"
34+
| "final_score"
35+
>;
36+
37+
export type TopStaleUserRow = Pick<GitHubUserRow, "username" | "stale_after" | "final_score">;
38+
2639
export type UpsertUserParams<TRaw = GitHubUserData, TScores = CalculateUserScoreResult> = {
2740
username: string;
2841
name: string | null;
@@ -244,7 +257,12 @@ export class DatabaseStore {
244257
async getUser(username: string): Promise<GitHubUserRow | null> {
245258
const client = getPool();
246259
const result = await client.query(
247-
"SELECT * FROM github_users WHERE LOWER(username) = LOWER($1)",
260+
`SELECT
261+
username, name, avatar_url, location, country,
262+
raw_data, scores, repo_score, pr_score, contribution_score, final_score,
263+
fetched_at, stale_after, created_at, updated_at
264+
FROM github_users
265+
WHERE LOWER(username) = LOWER($1)`,
248266
[username],
249267
);
250268
return result.rows[0] ?? null;
@@ -261,46 +279,47 @@ export class DatabaseStore {
261279

262280
// ── Leaderboard operations ──────────────────────────────────────────
263281

264-
async getLeaderboard(country: string, limit: number = 500): Promise<GitHubUserRow[]> {
282+
async getLeaderboard(country: string, limit: number = 500): Promise<LeaderboardUserRow[]> {
265283
const client = getPool();
266-
const result = await client.query(
267-
`SELECT *
268-
FROM (
269-
SELECT DISTINCT ON (LOWER(username)) *
270-
FROM github_users
271-
WHERE country = $1
272-
ORDER BY LOWER(username), final_score DESC, updated_at DESC
273-
) deduped_users
274-
ORDER BY final_score DESC
275-
LIMIT $2`,
276-
[country, limit],
277-
);
284+
const isWorldwide = country.trim().toLowerCase() === "worldwide";
285+
const result = isWorldwide
286+
? await client.query(
287+
`SELECT username, name, avatar_url, repo_score, pr_score, contribution_score, final_score
288+
FROM github_users
289+
ORDER BY final_score DESC
290+
LIMIT $1`,
291+
[limit],
292+
)
293+
: await client.query(
294+
`SELECT username, name, avatar_url, repo_score, pr_score, contribution_score, final_score
295+
FROM github_users
296+
WHERE country = $1
297+
ORDER BY final_score DESC
298+
LIMIT $2`,
299+
[country, limit],
300+
);
278301
return result.rows;
279302
}
280303

281304
async getLeaderboardCount(country: string): Promise<number> {
282305
const client = getPool();
283-
const result = await client.query(
284-
"SELECT COUNT(DISTINCT LOWER(username)) FROM github_users WHERE country = $1",
285-
[country],
286-
);
306+
const isWorldwide = country.trim().toLowerCase() === "worldwide";
307+
const result = isWorldwide
308+
? await client.query("SELECT COUNT(*) FROM github_users")
309+
: await client.query("SELECT COUNT(*) FROM github_users WHERE country = $1", [country]);
287310
return Number(result.rows[0].count);
288311
}
289312

290313
/**
291314
* Returns stale users in a country, ordered by score descending.
292315
* These are users whose data needs to be refreshed from GitHub.
293316
*/
294-
async getTopStaleUsers(country: string, limit: number = 500): Promise<GitHubUserRow[]> {
317+
async getTopStaleUsers(country: string, limit: number = 500): Promise<TopStaleUserRow[]> {
295318
const client = getPool();
296319
const result = await client.query(
297-
`SELECT *
298-
FROM (
299-
SELECT DISTINCT ON (LOWER(username)) *
300-
FROM github_users
301-
WHERE country = $1 AND stale_after < NOW()
302-
ORDER BY LOWER(username), final_score DESC, updated_at DESC
303-
) deduped_users
320+
`SELECT username, stale_after, final_score
321+
FROM github_users
322+
WHERE country = $1 AND stale_after < NOW()
304323
ORDER BY final_score DESC
305324
LIMIT $2`,
306325
[country, limit],
@@ -312,16 +331,12 @@ export class DatabaseStore {
312331
* Returns the top-scoring users in a country regardless of staleness.
313332
* Used to determine which users to check for refresh.
314333
*/
315-
async getTopUsers(country: string, limit: number = 500): Promise<GitHubUserRow[]> {
334+
async getTopUsers(country: string, limit: number = 500): Promise<TopStaleUserRow[]> {
316335
const client = getPool();
317336
const result = await client.query(
318-
`SELECT *
319-
FROM (
320-
SELECT DISTINCT ON (LOWER(username)) *
321-
FROM github_users
322-
WHERE country = $1
323-
ORDER BY LOWER(username), final_score DESC, updated_at DESC
324-
) deduped_users
337+
`SELECT username, stale_after, final_score
338+
FROM github_users
339+
WHERE country = $1
325340
ORDER BY final_score DESC
326341
LIMIT $2`,
327342
[country, limit],

0 commit comments

Comments
 (0)