Skip to content

Commit d2ad5cd

Browse files
committed
perf: improve leaderboard mysql query
1 parent f0c6345 commit d2ad5cd

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/graphql/operations/leaderboards.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,20 @@ export default async function (parent, args) {
2929
let orderDirection = (args.orderDirection || 'desc').toUpperCase();
3030
if (!['ASC', 'DESC'].includes(orderDirection)) orderDirection = 'DESC';
3131

32-
const orderQuery = uniq([orderBy, ...ORDER_FIELDS])
32+
// Use a single extra field for deterministic ordering
33+
// depending on the realm (space or user)
34+
// This covers 100% of our use cases
35+
// Missing realm is coming from queries not used by our UI
36+
const orderByFields = [orderBy];
37+
if (where['space']) {
38+
orderByFields.push('user');
39+
} else if (where['user']) {
40+
orderByFields.push('space');
41+
} else {
42+
orderByFields.push('last_vote');
43+
}
44+
45+
const orderQuery = uniq(orderByFields)
3346
.map((field: string) => `l.${field} ${orderDirection}`)
3447
.join(', ');
3548

0 commit comments

Comments
 (0)