Optimize /top5points SQL query

This commit is contained in:
Zwelf 2020-05-30 18:42:40 +02:00
parent fa8972deb6
commit 7514e93a8a

View file

@ -1317,23 +1317,13 @@ bool CSqlScore::ShowTopPointsThread(CSqlServer* pSqlServer, const CSqlData<CSqlP
try try
{ {
char aBuf[512]; char aBuf[512];
pSqlServer->executeSql("SET @prev := NULL;");
pSqlServer->executeSql("SET @rank := 1;");
pSqlServer->executeSql("SET @pos := 0;");
str_format(aBuf, sizeof(aBuf), str_format(aBuf, sizeof(aBuf),
"SELECT Rank, Points, Name " "SELECT Rank, Points, Name "
"FROM (" "FROM ("
"SELECT Name, " "SELECT RANK() OVER w AS Rank, Points, Name "
"(@pos := @pos+1) pos, " "FROM %s_points "
"(@rank := IF(@prev = Points,@rank, @pos)) Rank, " "WINDOW w as (ORDER BY Points DESC)"
"(@prev := Points) Points " ") as a "
"FROM ("
"SELECT Name, Points "
"FROM %s_points "
"GROUP BY Name "
"ORDER BY Points DESC"
") as a"
") as b "
"ORDER BY Rank %s " "ORDER BY Rank %s "
"LIMIT %d, 5;", "LIMIT %d, 5;",
pSqlServer->GetPrefix(), pOrder, LimitStart pSqlServer->GetPrefix(), pOrder, LimitStart