Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion hasura/views/v_match_lineup_buy_types.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
-- Every match page filters on match_id. A filter only passes below a window
-- function, a DISTINCT ON or a GROUP BY on one of its keys, so match_id is in
-- each of them even though match_map_id already implies it; without it every
-- page read computed the view for every match on the panel.
CREATE OR REPLACE VIEW public.v_match_lineup_buy_types AS
WITH round_lineup AS (
SELECT
Expand Down Expand Up @@ -46,7 +50,7 @@ round_start AS (
(rl.match_type IS DISTINCT FROM 'Rush' AND rl.round > rl.mr * 2
AND (rl.round - rl.mr * 2 - 1) % 3 = 0) AS ot_half_start
FROM round_lineup rl
WINDOW w AS (PARTITION BY rl.match_map_id, rl.match_lineup_id ORDER BY rl.round)
WINDOW w AS (PARTITION BY rl.match_id, rl.match_map_id, rl.match_lineup_id ORDER BY rl.round)
),
typed AS (
SELECT
Expand Down
17 changes: 12 additions & 5 deletions hasura/views/v_match_lineup_map_stats.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
-- Every match page filters on match_id. A filter only passes below a window
-- function, a DISTINCT ON or a GROUP BY on one of its keys, so match_id is in
-- each of them even though match_map_id already implies it; without it every
-- page read computed the view for every match on the panel.
CREATE OR REPLACE VIEW public.v_match_lineup_map_stats AS
WITH round_lineup AS (
SELECT
Expand Down Expand Up @@ -46,10 +50,11 @@ round_start AS (
(rl.match_type IS DISTINCT FROM 'Rush' AND rl.round > rl.mr * 2
AND (rl.round - rl.mr * 2 - 1) % 3 = 0) AS ot_half_start
FROM round_lineup rl
WINDOW w AS (PARTITION BY rl.match_map_id, rl.match_lineup_id ORDER BY rl.round)
WINDOW w AS (PARTITION BY rl.match_id, rl.match_map_id, rl.match_lineup_id ORDER BY rl.round)
),
first_kill AS (
SELECT DISTINCT ON (pk.match_map_id, pk.round)
SELECT DISTINCT ON (pk.match_id, pk.match_map_id, pk.round)
pk.match_id,
pk.match_map_id,
pk.round,
pk.attacker_steam_id,
Expand All @@ -59,16 +64,17 @@ first_kill AS (
AND pk.attacker_team <> pk.attacked_team
AND pk.attacker_steam_id IS NOT NULL
AND pk.attacker_steam_id <> pk.attacked_steam_id
ORDER BY pk.match_map_id, pk.round, pk."time" ASC
ORDER BY pk.match_id, pk.match_map_id, pk.round, pk."time" ASC
),
fk_lineups AS (
SELECT
fk.match_id,
fk.match_map_id,
fk.round,
klp.match_lineup_id AS killer_lineup,
vlp.match_lineup_id AS victim_lineup
FROM first_kill fk
JOIN public.match_maps mm ON mm.id = fk.match_map_id
JOIN public.match_maps mm ON mm.id = fk.match_map_id AND mm.match_id = fk.match_id
JOIN public.matches m ON m.id = mm.match_id
LEFT JOIN public.match_lineup_players klp
ON klp.steam_id = fk.attacker_steam_id
Expand Down Expand Up @@ -97,7 +103,8 @@ per_round AS (
(fk.victim_lineup = rl.match_lineup_id) AS lost_first
FROM round_start rl
LEFT JOIN fk_lineups fk
ON fk.match_map_id = rl.match_map_id
ON fk.match_id = rl.match_id
AND fk.match_map_id = rl.match_map_id
AND fk.round = rl.round
WHERE rl.side IS NOT NULL
)
Expand Down
18 changes: 11 additions & 7 deletions hasura/views/v_match_player_buy_types.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
-- Every match page filters on match_id. A filter only passes below a window
-- function, a DISTINCT ON or a GROUP BY on one of its keys, so match_id is in
-- each of them even though match_map_id already implies it; without it every
-- page read computed the view for every match on the panel.
CREATE OR REPLACE VIEW public.v_match_player_buy_types AS
WITH round_lineup AS (
SELECT
Expand Down Expand Up @@ -42,7 +46,7 @@ round_start AS (
(rl.match_type IS DISTINCT FROM 'Rush' AND rl.round > rl.mr * 2
AND (rl.round - rl.mr * 2 - 1) % 3 = 0) AS ot_half_start
FROM round_lineup rl
WINDOW w AS (PARTITION BY rl.match_map_id, rl.match_lineup_id ORDER BY rl.round)
WINDOW w AS (PARTITION BY rl.match_id, rl.match_map_id, rl.match_lineup_id ORDER BY rl.round)
),
typed AS (
SELECT
Expand Down Expand Up @@ -86,19 +90,19 @@ player_rounds AS (
WHERE mr.matchup IS NOT NULL AND mlp.steam_id IS NOT NULL
),
kills_per_round AS (
SELECT match_map_id, round, attacker_steam_id AS steam_id, COUNT(*)::int AS kills
SELECT match_id, match_map_id, round, attacker_steam_id AS steam_id, COUNT(*)::int AS kills
FROM public.player_kills
WHERE attacker_steam_id IS NOT NULL
AND attacker_team <> attacked_team
AND attacker_steam_id <> attacked_steam_id
GROUP BY match_map_id, round, attacker_steam_id
GROUP BY match_id, match_map_id, round, attacker_steam_id
),
deaths_per_round AS (
SELECT match_map_id, round, attacked_steam_id AS steam_id, COUNT(*)::int AS deaths
SELECT match_id, match_map_id, round, attacked_steam_id AS steam_id, COUNT(*)::int AS deaths
FROM public.player_kills
WHERE attacker_team <> attacked_team
AND attacker_steam_id <> attacked_steam_id
GROUP BY match_map_id, round, attacked_steam_id
GROUP BY match_id, match_map_id, round, attacked_steam_id
)
SELECT
pr.match_id,
Expand All @@ -112,7 +116,7 @@ SELECT
COALESCE(SUM(d.deaths), 0)::int AS deaths
FROM player_rounds pr
LEFT JOIN kills_per_round k
ON k.match_map_id = pr.match_map_id AND k.round = pr.round AND k.steam_id = pr.steam_id
ON k.match_id = pr.match_id AND k.match_map_id = pr.match_map_id AND k.round = pr.round AND k.steam_id = pr.steam_id
LEFT JOIN deaths_per_round d
ON d.match_map_id = pr.match_map_id AND d.round = pr.round AND d.steam_id = pr.steam_id
ON d.match_id = pr.match_id AND d.match_map_id = pr.match_map_id AND d.round = pr.round AND d.steam_id = pr.steam_id
GROUP BY pr.match_id, pr.match_map_id, pr.match_lineup_id, pr.steam_id, pr.side, pr.matchup;
23 changes: 15 additions & 8 deletions hasura/views/v_match_player_opening_duels.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
-- Every match page filters on match_id. A filter only passes below a window
-- function, a DISTINCT ON or a GROUP BY on one of its keys, and never into a
-- CTE used twice unless it is NOT MATERIALIZED, so match_id is a key of each
-- even though match_map_id already implies it; without it every page read
-- computed the view for every match on the panel.
CREATE OR REPLACE VIEW public.v_match_player_opening_duels AS
WITH round_sides AS (
WITH round_sides AS NOT MATERIALIZED (
SELECT
mm.match_id, mmr.match_map_id, mmr.round,
m.lineup_1_id AS l1, public.normalize_side(mmr.lineup_1_side) AS l1_side,
Expand All @@ -10,19 +15,19 @@ WITH round_sides AS (
WHERE mmr.round > 0 AND mmr.deleted_at IS NULL
),
first_kill AS (
SELECT DISTINCT ON (pk.match_map_id, pk.round)
pk.match_map_id, pk.round, pk."time",
SELECT DISTINCT ON (pk.match_id, pk.match_map_id, pk.round)
pk.match_id, pk.match_map_id, pk.round, pk."time",
pk.attacker_steam_id AS killer, pk.attacked_steam_id AS victim
FROM public.player_kills pk
WHERE pk.attacker_team IS NOT NULL
AND pk.attacker_team <> pk.attacked_team
AND pk.attacker_steam_id IS NOT NULL
AND pk.attacker_steam_id <> pk.attacked_steam_id
ORDER BY pk.match_map_id, pk.round, pk."time" ASC
ORDER BY pk.match_id, pk.match_map_id, pk.round, pk."time" ASC
),
fk AS (
fk AS NOT MATERIALIZED (
SELECT
fk.match_map_id, fk.round, fk.killer, fk.victim,
fk.match_id, fk.match_map_id, fk.round, fk.killer, fk.victim,
EXISTS (
SELECT 1 FROM public.player_kills t
WHERE t.match_map_id = fk.match_map_id
Expand All @@ -38,15 +43,17 @@ duel_rows AS (
CASE WHEN klp.match_lineup_id = rs.l1 THEN rs.l1_side ELSE rs.l2_side END AS side,
fk.killer AS steam_id, true AS is_win, false AS is_death, false AS traded
FROM fk
JOIN round_sides rs ON rs.match_map_id = fk.match_map_id AND rs.round = fk.round
JOIN round_sides rs
ON rs.match_id = fk.match_id AND rs.match_map_id = fk.match_map_id AND rs.round = fk.round
JOIN public.match_lineup_players klp
ON klp.steam_id = fk.killer AND klp.match_lineup_id IN (rs.l1, rs.l2)
UNION ALL
SELECT rs.match_id, fk.match_map_id, fk.round, vlp.match_lineup_id,
CASE WHEN vlp.match_lineup_id = rs.l1 THEN rs.l1_side ELSE rs.l2_side END,
fk.victim, false, true, fk.victim_traded
FROM fk
JOIN round_sides rs ON rs.match_map_id = fk.match_map_id AND rs.round = fk.round
JOIN round_sides rs
ON rs.match_id = fk.match_id AND rs.match_map_id = fk.match_map_id AND rs.round = fk.round
JOIN public.match_lineup_players vlp
ON vlp.steam_id = fk.victim AND vlp.match_lineup_id IN (rs.l1, rs.l2)
),
Expand Down
49 changes: 30 additions & 19 deletions hasura/views/v_player_match_map_roles.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
-- Every match page filters on match_id. A filter only passes below a window
-- function, a DISTINCT ON or a GROUP BY on one of its keys, so match_id is a
-- key of each even though match_map_id already implies it; without it every
-- page read computed the view for every match on the panel.
DROP VIEW IF EXISTS public.v_player_match_map_roles;
CREATE VIEW public.v_player_match_map_roles AS
WITH kills_agg AS (
SELECT
pk.match_id,
pk.match_map_id,
pk.attacker_steam_id AS steam_id,
COUNT(*)::int AS total_kills,
Expand All @@ -11,31 +16,35 @@ WITH kills_agg AS (
FROM player_kills pk
WHERE pk.attacker_steam_id IS NOT NULL
AND pk.attacker_steam_id <> pk.attacked_steam_id
GROUP BY pk.match_map_id, pk.attacker_steam_id
GROUP BY pk.match_id, pk.match_map_id, pk.attacker_steam_id
),
first_kills AS (
SELECT DISTINCT ON (pk.match_map_id, pk.round)
SELECT DISTINCT ON (pk.match_id, pk.match_map_id, pk.round)
pk.match_id,
pk.match_map_id,
pk.attacker_steam_id,
pk.attacked_steam_id
FROM player_kills pk
WHERE pk.attacker_steam_id <> pk.attacked_steam_id
ORDER BY pk.match_map_id, pk.round, pk."time" ASC
ORDER BY pk.match_id, pk.match_map_id, pk.round, pk."time" ASC
),
-- One pass over first_kills: a CTE read twice is materialized past the match
-- filter, and forcing it inline makes reads without that filter (the profile,
-- role-filtered leaderboards) scan every kill twice.
opening AS (
SELECT
match_map_id,
steam_id,
fk.match_id,
fk.match_map_id,
v.steam_id,
COUNT(*)::int AS opening_attempts,
COUNT(*) FILTER (WHERE is_kill)::int AS open_kills,
COUNT(*) FILTER (WHERE NOT is_kill)::int AS open_deaths
FROM (
SELECT match_map_id, attacker_steam_id AS steam_id, true AS is_kill FROM first_kills
UNION ALL
SELECT match_map_id, attacked_steam_id AS steam_id, false AS is_kill FROM first_kills
) o
WHERE steam_id IS NOT NULL
GROUP BY match_map_id, steam_id
COUNT(*) FILTER (WHERE v.is_kill)::int AS open_kills,
COUNT(*) FILTER (WHERE NOT v.is_kill)::int AS open_deaths
FROM first_kills fk
CROSS JOIN LATERAL (
VALUES (fk.attacker_steam_id, true), (fk.attacked_steam_id, false)
) AS v(steam_id, is_kill)
WHERE v.steam_id IS NOT NULL
GROUP BY fk.match_id, fk.match_map_id, v.steam_id
),
base AS (
SELECT
Expand Down Expand Up @@ -67,8 +76,10 @@ base AS (
(s.flash_assists + (s.he_damage + s.molotov_damage) / 100.0 + s.flashes_thrown * 0.3)
/ NULLIF(s.rounds_played, 0) AS support_idx
FROM player_match_map_stats s
LEFT JOIN kills_agg k ON k.match_map_id = s.match_map_id AND k.steam_id = s.steam_id
LEFT JOIN opening o ON o.match_map_id = s.match_map_id AND o.steam_id = s.steam_id
LEFT JOIN kills_agg k
ON k.match_id = s.match_id AND k.match_map_id = s.match_map_id AND k.steam_id = s.steam_id
LEFT JOIN opening o
ON o.match_id = s.match_id AND o.match_map_id = s.match_map_id AND o.steam_id = s.steam_id
LEFT JOIN v_player_match_map_hltv h ON h.match_map_id = s.match_map_id AND h.steam_id = s.steam_id
LEFT JOIN LATERAL (
SELECT ml.id AS lineup_id
Expand All @@ -83,7 +94,7 @@ ranked AS (
SELECT
b.*,
ROW_NUMBER() OVER (
PARTITION BY b.match_map_id, b.lineup_id
PARTITION BY b.match_id, b.match_map_id, b.lineup_id
ORDER BY b.awp_kills DESC, b.awp_share DESC, b.steam_id
) AS awp_rk
FROM base b
Expand All @@ -95,7 +106,7 @@ flag_awp AS (
rank_entry AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY match_map_id, lineup_id
PARTITION BY match_id, match_map_id, lineup_id
ORDER BY (CASE WHEN is_awper THEN -1 ELSE entry_rate END) DESC, steam_id
) AS entry_rk
FROM flag_awp
Expand All @@ -107,7 +118,7 @@ flag_entry AS (
rank_support AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY match_map_id, lineup_id
PARTITION BY match_id, match_map_id, lineup_id
ORDER BY (CASE WHEN is_awper OR is_entry THEN -1 ELSE support_idx END) DESC, steam_id
) AS support_rk
FROM flag_entry
Expand Down
Loading
Loading