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,12 +309,11 @@ class MatchRepository {
'match_players.penta_kills', 'match_players.penta_kills',
] ]
const query = fields const query =
.map((field) => { `
return ` WITH base as (
(SELECT SELECT
'${field}' AS what, ${fields.join()},
${field} AS amount,
match_players.win as result, match_players.win as result,
matches.id, matches.id,
matches.date, matches.date,
@ -325,12 +324,27 @@ class MatchRepository {
${this.JOIN_MATCHES} ${this.JOIN_MATCHES}
WHERE WHERE
${this.globalFilters(filters)} ${this.globalFilters(filters)}
)
` +
fields
.map(
(field) => `
(SELECT
'${field}' AS what,
${field.split('.').pop()} AS amount,
result,
id,
date,
gamemode,
champion_id
FROM
base
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)