Optimize seasons page speed

This commit is contained in:
Kalle
2024-05-20 19:11:55 +03:00
parent 3fea665d56
commit 7082bc72c3
4 changed files with 30 additions and 55 deletions

View File

@@ -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;
}

View File

@@ -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({

View File

@@ -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 }),

View File

@@ -0,0 +1,7 @@
export function up(db) {
db.transaction(() => {
db.prepare(
/* sql */ `create index skill_user_id_season on "Skill" ("userId", "season");`,
).run();
})();
}