refactor: records SQL query 50 times faster

This commit is contained in:
Valentin Kaelin 2022-02-02 22:25:15 +01:00
parent 618b7b3992
commit b17a80b2af

View file

@ -309,29 +309,43 @@ class MatchRepository {
'match_players.penta_kills', 'match_players.penta_kills',
] ]
const query = fields const query =
.map((field) => { `
return ` WITH base as (
SELECT
${fields.join()},
match_players.win as result,
matches.id,
matches.date,
matches.gamemode,
match_players.champion_id
FROM
match_players
${this.JOIN_MATCHES}
WHERE
${this.globalFilters(filters)}
)
` +
fields
.map(
(field) => `
(SELECT (SELECT
'${field}' AS what, '${field}' AS what,
${field} AS amount, ${field.split('.').pop()} AS amount,
match_players.win as result, result,
matches.id, id,
matches.date, date,
matches.gamemode, gamemode,
match_players.champion_id champion_id
FROM FROM
match_players base
${this.JOIN_MATCHES}
WHERE
${this.globalFilters(filters)}
ORDER BY ORDER BY
${field} DESC, matches.id 2 DESC, id
LIMIT LIMIT
1) 1)
` `
}) )
.join('UNION ALL ') .join(' UNION ALL ')
const { rows } = await Database.rawQuery(query, filters as any) const { rows } = await Database.rawQuery(query, filters as any)
return rows return rows