From dc5d316db1d1cb45bdb8477641b67126facdddb5 Mon Sep 17 00:00:00 2001 From: Scott Weaver Date: Mon, 17 Feb 2025 15:06:54 -0600 Subject: [PATCH] chunking adjustments to updateScheduledMatchup and getActiveMatchupsByLeague --- convex/matchups.ts | 6 +++--- convex/schedules.ts | 22 ++++++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/convex/matchups.ts b/convex/matchups.ts index 58e0fb8..4d2c410 100644 --- a/convex/matchups.ts +++ b/convex/matchups.ts @@ -200,10 +200,10 @@ export const getActiveMatchupsByLeague = query({ handler: async (ctx, { league }) => { const matchups = await ctx.db .query("matchups") - .filter((q) => - q.and(q.eq(q.field("league"), league), q.eq(q.field("active"), true)) + .withIndex("by_active_league", (q) => + q.eq("league", league).eq("active", true) ) - .take(50); + .take(250); // Get pick counts for each matchup const matchupsWithPicks = []; diff --git a/convex/schedules.ts b/convex/schedules.ts index a0ad4b7..8638b5d 100644 --- a/convex/schedules.ts +++ b/convex/schedules.ts @@ -148,19 +148,21 @@ export const updateScheduledMatchup = internalMutation({ status: v.string(), }, handler: async (ctx, { gameId, league, startTime, status }) => { + // Use index to efficiently query matchups const matchups = await ctx.db .query("matchups") - .filter((q) => - q.and( - q.eq(q.field("league"), league), - q.eq(q.field("active"), true), - q.eq(q.field("gameId"), gameId) - ) + .withIndex("by_active_league", (q) => + q.eq("league", league).eq("active", true) ) - .take(500); - for (const matchup of matchups) { - await ctx.db.patch(matchup._id, { startTime, status }); - } + .filter((q) => q.eq(q.field("gameId"), gameId)) + .take(50); + + // Batch update all matching matchups + await Promise.all( + matchups.map((matchup) => + ctx.db.patch(matchup._id, { startTime, status }) + ) + ); }, });