diff --git a/app/features/mmr/queries/seasonAllMMRByUserId.server.ts b/app/features/mmr/queries/seasonAllMMRByUserId.server.ts index 884716851..ddcb9e283 100644 --- a/app/features/mmr/queries/seasonAllMMRByUserId.server.ts +++ b/app/features/mmr/queries/seasonAllMMRByUserId.server.ts @@ -23,22 +23,6 @@ const groupedSkillsStm = sql.prepare(/* sql */ ` order by "date" asc `); -const mostRecentStm = sql.prepare(/* sql */ ` - select - "Skill"."ordinal" - from - "Skill" - where - "Skill"."id" = ( - select max("id") - from "Skill" - where "userId" = @userId - and "season" = @season - and "matchesCount" >= ${MATCHES_COUNT_NEEDED_FOR_LEADERBOARD} - group by "userId" - ) -`); - export function seasonAllMMRByUserId({ userId, season, @@ -51,17 +35,3 @@ export function seasonAllMMRByUserId({ date: string; }>; } - -export function currentMMRByUserId({ - userId, - season, -}: { - userId: number; - season: number; -}) { - return ( - mostRecentStm.get({ userId, season }) as { - ordinal: number; - } | null - )?.ordinal; -} diff --git a/app/features/sendouq/queries/seasonMatchesByUserId.server.ts b/app/features/sendouq/queries/seasonMatchesByUserId.server.ts index 170c08d9f..8f2ed30d8 100644 --- a/app/features/sendouq/queries/seasonMatchesByUserId.server.ts +++ b/app/features/sendouq/queries/seasonMatchesByUserId.server.ts @@ -16,13 +16,16 @@ const stm = sql.prepare(/* sql */ ` "GroupMatch"."createdAt", "GroupMatch"."memento", (select exists (select 1 from "Skill" where "Skill"."groupMatchId" = "GroupMatch"."id")) as "isLocked" - from "GroupMatch" - left join "Group" on - "GroupMatch"."alphaGroupId" = "Group"."id" or - "GroupMatch"."bravoGroupId" = "Group"."id" - left join "GroupMember" on "Group"."id" = "GroupMember"."groupId" - where "GroupMember"."userId" = @userId + from "GroupMember" + inner join "Group" on "GroupMember"."groupId" = "Group"."id" + inner join "GroupMatch" on + ( + "GroupMatch"."alphaGroupId" = "Group"."id" or + "GroupMatch"."bravoGroupId" = "Group"."id" + ) and "GroupMatch"."createdAt" between @starts and @ends + where + "GroupMember"."userId" = @userId order by "GroupMatch"."id" desc limit ${MATCHES_PER_SEASONS_PAGE} offset ${MATCHES_PER_SEASONS_PAGE} * (@page - 1) @@ -166,21 +169,19 @@ export function seasonMatchesByUserId({ } const pagesStm = sql.prepare(/* sql */ ` - with "q1" as ( - select - "GroupMatch"."id" - from "GroupMatch" - left join "Group" on - "GroupMatch"."alphaGroupId" = "Group"."id" or - "GroupMatch"."bravoGroupId" = "Group"."id" - left join "GroupMember" on "Group"."id" = "GroupMember"."groupId" - where "GroupMember"."userId" = @userId - and "GroupMatch"."createdAt" between @starts and @ends - group by "GroupMatch"."id" - ) select count(*) as "count" - from "q1" + from + "GroupMember" + inner join "Group" on "GroupMember"."groupId" = "Group"."id" + inner join "GroupMatch" on + ( + "GroupMatch"."alphaGroupId" = "Group"."id" or + "GroupMatch"."bravoGroupId" = "Group"."id" + ) + and "GroupMatch"."createdAt" between @starts and @ends + where + "GroupMember"."userId" = @userId `); export function seasonMatchesByUserIdPagesCount({ diff --git a/app/features/user-page/routes/u.$identifier.seasons.tsx b/app/features/user-page/routes/u.$identifier.seasons.tsx index f295843fb..5469752a9 100644 --- a/app/features/user-page/routes/u.$identifier.seasons.tsx +++ b/app/features/user-page/routes/u.$identifier.seasons.tsx @@ -23,10 +23,7 @@ import { Tab, Tabs } from "~/components/Tabs"; import { AlertIcon } from "~/components/icons/Alert"; import { TopTenPlayer } from "~/features/leaderboards/components/TopTenPlayer"; import { playerTopTenPlacement } from "~/features/leaderboards/leaderboards-utils"; -import { - currentMMRByUserId, - seasonAllMMRByUserId, -} from "~/features/mmr/queries/seasonAllMMRByUserId.server"; +import { seasonAllMMRByUserId } from "~/features/mmr/queries/seasonAllMMRByUserId.server"; import { allSeasons, currentOrPreviousSeason, @@ -86,14 +83,14 @@ export const loader = async ({ params, request }: LoaderFunctionArgs) => { ); const { isAccurateTiers, userSkills } = _userSkills(season); - const { tier } = userSkills[user.id] ?? { + const { tier, ordinal } = userSkills[user.id] ?? { approximate: false, ordinal: 0, tier: { isPlus: false, name: "IRON" }, }; return { - currentOrdinal: currentMMRByUserId({ season, userId: user.id }), + currentOrdinal: ordinal, winrates: { maps: seasonMapWinrateByUserId({ season, userId: user.id }), sets: seasonSetWinrateByUserId({ season, userId: user.id }), diff --git a/migrations/056-skill-index.js b/migrations/056-skill-index.js new file mode 100644 index 000000000..bb212f114 --- /dev/null +++ b/migrations/056-skill-index.js @@ -0,0 +1,7 @@ +export function up(db) { + db.transaction(() => { + db.prepare( + /* sql */ `create index skill_user_id_season on "Skill" ("userId", "season");`, + ).run(); + })(); +}