shavit-rankings.sp - first attempt at optimizing queries using GROUP BY style instead of running a query for each style ID

This commit is contained in:
mourningsickness 2025-08-11 09:58:09 -07:00 committed by GitHub
parent 5391c32d3d
commit 5ca5446354
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1339,15 +1339,12 @@ void UpdatePointsForSinglePlayer(int client)
"UPDATE %susers SET points = (SELECT SUM(points) FROM %splayertimes WHERE auth = %d) WHERE auth = %d;", "UPDATE %susers SET points = (SELECT SUM(points) FROM %splayertimes WHERE auth = %d) WHERE auth = %d;",
gS_MySQLPrefix, gS_MySQLPrefix, auth, auth); gS_MySQLPrefix, gS_MySQLPrefix, auth, auth);
for (int i = 0; i < gI_Styles; i++)
{
FormatEx(sStyleQuery, sizeof(sStyleQuery), FormatEx(sStyleQuery, sizeof(sStyleQuery),
"UPDATE IGNORE %sstylepoints SET points = (SELECT SUM(points) FROM %splayertimes WHERE auth = %d AND style = %d) WHERE auth = %d AND style = %d;", "UPDATE IGNORE %sstylepoints AS S SET points = P.total (SELECT SUM(points) as total FROM %splayertimes WHERE auth = %d GROUP BY style) P WHERE auth = %d AND S.style = P.style;",
gS_MySQLPrefix, gS_MySQLPrefix, auth, i, auth, i); gS_MySQLPrefix, gS_MySQLPrefix, auth, auth);
QueryLog(gH_SQL, SQL_UpdateAllStylePoints_Callback, sStyleQuery); QueryLog(gH_SQL, SQL_UpdateAllStylePoints_Callback, sStyleQuery);
} }
}
else if (gB_SQLWindowFunctions) else if (gB_SQLWindowFunctions)
{ {
char sLimit[30]; char sLimit[30];
@ -1371,23 +1368,21 @@ void UpdatePointsForSinglePlayer(int client)
auth auth
); );
for (int i = 0; i < gI_Styles; i++)
{
FormatEx(sStyleQuery, sizeof(sStyleQuery), FormatEx(sStyleQuery, sizeof(sStyleQuery),
"UPDATE IGNORE %sstylepoints SET points = (\n" "UPDATE IGNORE %sstylepoints AS S SET points = (\n"
... " SELECT SUM(points2) FROM (\n" ... " SELECT SUM(points2) FROM (\n"
... " SELECT (points * POW(%f, ROW_NUMBER() OVER (ORDER BY points DESC) - 1)) as points2\n" ... " SELECT (points * POW(%f, ROW_NUMBER() OVER (ORDER BY points DESC) - 1)) as points2\n"
... " FROM %splayertimes\n" ... " FROM %splayertimes\n"
... " WHERE auth = %d AND points > 0 AND style = %d\n" ... " WHERE auth = %d AND points > 0 GROUP BY style\n"
... " ORDER BY points DESC %s\n" ... " ORDER BY points DESC %s\n"
... " ) as t\n" ... " ) as t\n"
... ") WHERE auth = %d AND style = %d;", ... ") P WHERE auth = %d AND S.style = P.style;",
gS_MySQLPrefix, gS_MySQLPrefix,
gCV_WeightingMultiplier.FloatValue, gCV_WeightingMultiplier.FloatValue,
gS_MySQLPrefix, gS_MySQLPrefix,
auth, i, auth,
sLimit, sLimit,
auth, i auth
); );
QueryLog(gH_SQL, SQL_UpdateAllStylePoints_Callback, sStyleQuery); QueryLog(gH_SQL, SQL_UpdateAllStylePoints_Callback, sStyleQuery);
@ -1443,16 +1438,13 @@ void UpdateAllPoints(bool recalcall=false, char[] map="", int track=-1)
gS_MySQLPrefix, gS_MySQLPrefix, gS_MySQLPrefix, gS_MySQLPrefix,
(sLastLogin[0] != 0) ? "AND " : "", sLastLogin); (sLastLogin[0] != 0) ? "AND " : "", sLastLogin);
for (int i = 0; i < gI_Styles; i++)
{
FormatEx(sStyleQuery, sizeof(sStyleQuery), FormatEx(sStyleQuery, sizeof(sStyleQuery),
"UPDATE IGNORE %sstylepoints AS S SET points = P.total FROM (SELECT auth, SUM(points) AS total FROM %splayertimes WHERE style = %d GROUP BY auth) P WHERE S.auth = P.auth AND style = %d %s %s;", "UPDATE IGNORE %sstylepoints AS S SET points = P.total FROM (SELECT auth, SUM(points) AS total FROM %splayertimes GROUP BY auth, style) P WHERE S.auth = P.auth AND S.style = P.style %s %s;",
gS_MySQLPrefix, gS_MySQLPrefix, i, i, gS_MySQLPrefix, gS_MySQLPrefix,
(sLastLogin[0] != 0) ? "AND " : "", sLastLogin); (sLastLogin[0] != 0) ? "AND " : "", sLastLogin);
QueryLog(gH_SQL, SQL_UpdateAllStylePoints_Callback, sStyleQuery); QueryLog(gH_SQL, SQL_UpdateAllStylePoints_Callback, sStyleQuery);
} }
}
else else
{ {
FormatEx(sQuery, sizeof(sQuery), FormatEx(sQuery, sizeof(sQuery),
@ -1460,17 +1452,14 @@ void UpdateAllPoints(bool recalcall=false, char[] map="", int track=-1)
gS_MySQLPrefix, gS_MySQLPrefix, gS_MySQLPrefix, gS_MySQLPrefix,
(sLastLogin[0] != 0) ? "WHERE" : "", sLastLogin); (sLastLogin[0] != 0) ? "WHERE" : "", sLastLogin);
for (int i = 0; i < gI_Styles; i++)
{
FormatEx(sStyleQuery, sizeof(sStyleQuery), FormatEx(sStyleQuery, sizeof(sStyleQuery),
"UPDATE IGNORE %sstylepoints AS S INNER JOIN (SELECT auth, SUM(points) AS total FROM %splayertimes WHERE style = %d GROUP BY auth) P ON S.auth = P.auth SET S.points = P.total WHERE style = %d %s %s;", "UPDATE IGNORE %sstylepoints AS S INNER JOIN (SELECT auth, SUM(points) AS total FROM %splayertimes GROUP BY auth, style) P ON S.auth = P.auth SET S.points = P.total WHERE S.style = P.style %s %s;",
gS_MySQLPrefix, gS_MySQLPrefix, i, i, gS_MySQLPrefix, gS_MySQLPrefix,
(sLastLogin[0] != 0) ? "WHERE" : "", sLastLogin); (sLastLogin[0] != 0) ? "WHERE" : "", sLastLogin);
QueryLog(gH_SQL, SQL_UpdateAllStylePoints_Callback, sStyleQuery); QueryLog(gH_SQL, SQL_UpdateAllStylePoints_Callback, sStyleQuery);
} }
} }
}
else if (gB_SQLWindowFunctions && gI_Driver == Driver_mysql) else if (gB_SQLWindowFunctions && gI_Driver == Driver_mysql)
{ {
if (sLastLogin[0]) if (sLastLogin[0])
@ -1503,8 +1492,6 @@ void UpdateAllPoints(bool recalcall=false, char[] map="", int track=-1)
sTrackWhere, sTrackWhere,
sLimit); // TODO: Remove/move sLimit? sLimit); // TODO: Remove/move sLimit?
for (int i = 0; i < gI_Styles; i++)
{
FormatEx(sStyleQuery, sizeof(sStyleQuery), FormatEx(sStyleQuery, sizeof(sStyleQuery),
"UPDATE IGNORE %sstylepoints AS s, (\n" "UPDATE IGNORE %sstylepoints AS s, (\n"
... " SELECT auth, SUM(t.points2) as pp FROM (\n" ... " SELECT auth, SUM(t.points2) as pp FROM (\n"
@ -1512,29 +1499,27 @@ void UpdateAllPoints(bool recalcall=false, char[] map="", int track=-1)
... " FROM %splayertimes AS p\n" ... " FROM %splayertimes AS p\n"
... " JOIN %sstylepoints AS s2\n" ... " JOIN %sstylepoints AS s2\n"
... " ON s2.auth = p.auth %s %s\n" ... " ON s2.auth = p.auth %s %s\n"
... " WHERE p.points > 0 AND p.style = %d AND p.auth IN (SELECT DISTINCT auth FROM %splayertimes WHERE style = %d %s %s %s %s)\n" ... " WHERE p.points > 0 AND p.auth IN (SELECT DISTINCT auth FROM %splayertimes %s %s %s %s)\n"
... " ORDER BY p.points DESC %s\n" ... " ORDER BY p.points DESC GROUP BY style %s\n"
... " ) AS t\n" ... " ) AS t\n"
... " GROUP by auth\n" ... " GROUP by auth, style\n"
... ") AS a\n" ... ") AS a\n"
... "SET s.points = a.pp\n" ... "SET s.points = a.pp\n"
... "WHERE s.auth = a.auth and s.style = %d;", ... "WHERE s.auth = a.auth and s.style = a.style;",
gS_MySQLPrefix, gS_MySQLPrefix,
gCV_WeightingMultiplier.FloatValue, gCV_WeightingMultiplier.FloatValue,
gS_MySQLPrefix, gS_MySQLPrefix,
gS_MySQLPrefix, gS_MySQLPrefix,
sLastLogin[0] ? "AND" : "", sLastLogin, sLastLogin[0] ? "AND" : "", sLastLogin,
i, gS_MySQLPrefix, i, gS_MySQLPrefix,
sMapWhere[0] ? "AND" : "", sMapWhere[0] ? "AND" : "",
sMapWhere, sMapWhere,
sTrackWhere[0] ? "AND" : "", sTrackWhere[0] ? "AND" : "",
sTrackWhere, sTrackWhere,
sLimit, // TODO: Remove/move sLimit? sLimit); // TODO: Remove/move sLimit?
i);
QueryLog(gH_SQL, SQL_UpdateAllStylePoints_Callback, sStyleQuery); QueryLog(gH_SQL, SQL_UpdateAllStylePoints_Callback, sStyleQuery);
} }
}
else if (gB_SQLWindowFunctions) else if (gB_SQLWindowFunctions)
{ {
FormatEx(sQuery, sizeof(sQuery), FormatEx(sQuery, sizeof(sQuery),
@ -1559,26 +1544,23 @@ void UpdateAllPoints(bool recalcall=false, char[] map="", int track=-1)
(sMapWhere[0] && sTrackWhere[0]) ? "AND" : "", (sMapWhere[0] && sTrackWhere[0]) ? "AND" : "",
sTrackWhere); sTrackWhere);
for (int i = 0; i < gI_Styles; i++)
{
FormatEx(sStyleQuery, sizeof(sStyleQuery), FormatEx(sStyleQuery, sizeof(sStyleQuery),
"UPDATE IGNORE %sstylepoints AS s\n" "UPDATE IGNORE %sstylepoints AS s\n"
... "SET points = (\n" ... "SET points = (\n"
... " SELECT SUM(points2) FROM (\n" ... " SELECT SUM(points2) FROM (\n"
... " SELECT (points * POW(%f, ROW_NUMBER() OVER (ORDER BY points DESC) - 1)) AS points2\n" ... " SELECT (points * POW(%f, ROW_NUMBER() OVER (ORDER BY points DESC) - 1)) AS points2\n"
... " FROM %splayertimes\n" ... " FROM %splayertimes\n"
... " WHERE auth = s.auth AND points > 0 AND style = %d\n" ... " WHERE auth = s.auth AND points > 0\n"
... " ORDER BY points DESC %s\n" ... " ORDER BY points DESC GROUP BY style %s\n"
... " ) AS t\n" ... " ) AS t\n"
... ") WHERE %s %s auth IN\n" ... ") p WHERE %s %s auth IN\n"
... " (SELECT DISTINCT auth FROM %splayertimes WHERE style = %d %s %s %s %s);", ... " (SELECT DISTINCT auth FROM %splayertimes WHERE style = t.style %s %s %s %s);",
gS_MySQLPrefix, gS_MySQLPrefix,
gCV_WeightingMultiplier.FloatValue, gCV_WeightingMultiplier.FloatValue,
gS_MySQLPrefix, gS_MySQLPrefix,
i,
sLimit, // TODO: Remove/move sLimit? sLimit, // TODO: Remove/move sLimit?
sLastLogin, sLastLogin[0] ? "AND" : "", sLastLogin, sLastLogin[0] ? "AND" : "",
gS_MySQLPrefix, i, gS_MySQLPrefix,
sMapWhere[0] ? "AND" : "", sMapWhere[0] ? "AND" : "",
sMapWhere, sMapWhere,
sTrackWhere[0] ? "AND" : "", sTrackWhere[0] ? "AND" : "",
@ -1586,7 +1568,6 @@ void UpdateAllPoints(bool recalcall=false, char[] map="", int track=-1)
QueryLog(gH_SQL, SQL_UpdateAllStylePoints_Callback, sStyleQuery); QueryLog(gH_SQL, SQL_UpdateAllStylePoints_Callback, sStyleQuery);
} }
}
else // !gB_SQLWindowFunctions && gI_Driver == Driver_mysql else // !gB_SQLWindowFunctions && gI_Driver == Driver_mysql
{ {
FormatEx(sQuery, sizeof(sQuery), FormatEx(sQuery, sizeof(sQuery),