From 1b7350757abf5765ca6b4565b30d97c73d025b29 Mon Sep 17 00:00:00 2001 From: acanas Date: Wed, 7 Apr 2021 00:26:31 +0200 Subject: [PATCH] Version 20.60: Apr 07, 2021 Optimizations in database selects. --- swad_API.c | 6 +-- swad_changelog.h | 3 +- swad_exam.c | 72 +++++++---------------------- swad_exam_set.c | 85 ++++++++++------------------------ swad_game.c | 116 ++++++++++++----------------------------------- swad_match.c | 30 ++++-------- 6 files changed, 84 insertions(+), 228 deletions(-) diff --git a/swad_API.c b/swad_API.c index 52c746e4f..af243f290 100644 --- a/swad_API.c +++ b/swad_API.c @@ -686,14 +686,14 @@ static bool API_GetSomeUsrDataFromUsrCod (struct UsrData *UsrDat,long CrsCod) UsrDat->Roles.InCurrentCrs.Valid = true; else { - UsrDat->Roles.InCurrentCrs.Role = Rol_UNK; + UsrDat->Roles.InCurrentCrs.Role = Rol_UNK; UsrDat->Roles.InCurrentCrs.Valid = false; } } else // MAX(Role) == NULL if user does not belong to any course { - UsrDat->Roles.InCurrentCrs.Role = Rol_UNK; + UsrDat->Roles.InCurrentCrs.Role = Rol_UNK; UsrDat->Roles.InCurrentCrs.Valid = true; } } @@ -3672,7 +3672,7 @@ int swad__sendMessage (struct soap *soap, { /***** Check if the original message was really received by me *****/ if (DB_QuerySELECTUnsigned ("can not check original message", - "SELECT SUM(N)" // row[0] + "SELECT SUM(N)" " FROM (SELECT COUNT(*) AS N" " FROM msg_rcv" " WHERE UsrCod=%ld" diff --git a/swad_changelog.h b/swad_changelog.h index e22859abd..6a8543b95 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -600,13 +600,14 @@ TODO: Salvador Romero Cort TODO: FIX BUG, URGENT! En las fechas como parámetro Dat_WriteParamsIniEndDates(), por ejemplo al cambiar el color de la gráfica de accesos por día y hora, no se respeta la zona horaria. */ -#define Log_PLATFORM_VERSION "SWAD 20.59 (2021-04-06)" +#define Log_PLATFORM_VERSION "SWAD 20.60 (2021-04-07)" #define CSS_FILE "swad20.45.css" #define JS_FILE "swad20.6.2.js" /* TODO: Rename CENTRE to CENTER in help wiki. TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams + Version 20.60: Apr 07, 2021 Optimizations in database selects. (308790 lines) Version 20.59: Apr 06, 2021 Optimizations in database selects. (308903 lines) Version 20.58: Apr 06, 2021 Optimizations in database selects. (308989 lines) Version 20.57: Apr 05, 2021 Optimizations in database selects. (309041 lines) diff --git a/swad_exam.c b/swad_exam.c index 4bfd8bfc6..2337ebe34 100644 --- a/swad_exam.c +++ b/swad_exam.c @@ -1769,8 +1769,6 @@ unsigned Exa_GetParamQstInd (void) long Exa_GetQstCodFromQstInd (long ExaCod,unsigned QstInd) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; long QstCod; /***** Get question code of the question to be moved up *****/ @@ -1784,14 +1782,6 @@ long Exa_GetQstCodFromQstInd (long ExaCod,unsigned QstInd) if (QstCod <= 0) Lay_ShowErrorAndExit ("Error: wrong question index."); - /***** Get question code (row[0]) *****/ - row = mysql_fetch_row (mysql_res); - if ((QstCod = Str_ConvertStrCodToLongCod (row[0])) <= 0) - Lay_ShowErrorAndExit ("Error: wrong question code."); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - return QstCod; } @@ -1803,67 +1793,39 @@ long Exa_GetQstCodFromQstInd (long ExaCod,unsigned QstInd) unsigned Exa_GetPrevQuestionIndexInExam (long ExaCod,unsigned QstInd) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - unsigned PrevQstInd = 0; - /***** Get previous question index in an exam from database *****/ // Although indexes are always continuous... // ...this implementation works even with non continuous indexes - if (!DB_QuerySELECT (&mysql_res,"can not get previous question index", - "SELECT MAX(QstInd)" // row[0] - " FROM exa_set_questions" - " WHERE ExaCod=%ld" - " AND QstInd<%u", - ExaCod, - QstInd)) - Lay_ShowErrorAndExit ("Error: previous question index not found."); - - /***** Get previous question index (row[0]) *****/ - row = mysql_fetch_row (mysql_res); - if (row) - if (row[0]) - if (sscanf (row[0],"%u",&PrevQstInd) != 1) - Lay_ShowErrorAndExit ("Error when getting previous question index."); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - - return PrevQstInd; + return DB_QuerySELECTUnsigned ("can not get previous question index", + "SELECT MAX(QstInd)" // row[0] + " FROM exa_set_questions" + " WHERE ExaCod=%ld" + " AND QstInd<%u", + ExaCod, + QstInd); } /*****************************************************************************/ /************* Get next question index to a given index in an exam ************/ /*****************************************************************************/ // Input question index can be 0, 1, 2, 3... n-1 -// Return question index will be 1, 2, 3... n if next question exists, or 0 if no next question +// Return question index will be 1, 2, 3... n if next question exists, or big number if no next question unsigned Exa_GetNextQuestionIndexInExam (long ExaCod,unsigned QstInd) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - unsigned NextQstInd = ExaSes_AFTER_LAST_QUESTION; // End of questions has been reached + unsigned NextQstInd; /***** Get next question index in an exam from database *****/ // Although indexes are always continuous... // ...this implementation works even with non continuous indexes - if (!DB_QuerySELECT (&mysql_res,"can not get next question index", - "SELECT MIN(QstInd)" // row[0] - " FROM exa_set_questions" - " WHERE ExaCod=%ld" - " AND QstInd>%u", - ExaCod,QstInd)) - Lay_ShowErrorAndExit ("Error: next question index not found."); - - /***** Get next question index (row[0]) *****/ - row = mysql_fetch_row (mysql_res); - if (row) - if (row[0]) - if (sscanf (row[0],"%u",&NextQstInd) != 1) - Lay_ShowErrorAndExit ("Error when getting next question index."); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); + NextQstInd = DB_QuerySELECTUnsigned ("can not get next question index", + "SELECT MIN(QstInd)" + " FROM exa_set_questions" + " WHERE ExaCod=%ld" + " AND QstInd>%u", + ExaCod,QstInd); + if (NextQstInd == 0) + NextQstInd = ExaSes_AFTER_LAST_QUESTION; // End of questions has been reached return NextQstInd; } diff --git a/swad_exam_set.c b/swad_exam_set.c index f0a7e13ce..44f2cfd78 100644 --- a/swad_exam_set.c +++ b/swad_exam_set.c @@ -822,25 +822,12 @@ static long ExaSet_GetSetCodFromSetInd (long ExaCod,unsigned SetInd) static unsigned ExaSet_GetMaxSetIndexInExam (long ExaCod) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - unsigned SetInd = 0; - /***** Get maximum set index in an exam from database *****/ - DB_QuerySELECT (&mysql_res,"can not get max set index", - "SELECT MAX(SetInd)" // row[0] - " FROM exa_sets" - " WHERE ExaCod=%ld", - ExaCod); - row = mysql_fetch_row (mysql_res); - if (row[0]) // There are sets - if (sscanf (row[0],"%u",&SetInd) != 1) - Lay_ShowErrorAndExit ("Error when getting max set index."); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - - return SetInd; + return DB_QuerySELECTUnsigned ("can not get max set index", + "SELECT MAX(SetInd)" + " FROM exa_sets" + " WHERE ExaCod=%ld", + ExaCod); } /*****************************************************************************/ @@ -851,66 +838,40 @@ static unsigned ExaSet_GetMaxSetIndexInExam (long ExaCod) static unsigned ExaSet_GetPrevSetIndexInExam (long ExaCod,unsigned SetInd) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - unsigned PrevSetInd = 0; - /***** Get previous set index in an exam from database *****/ // Although indexes are always continuous... // ...this implementation works even with non continuous indexes - if (!DB_QuerySELECT (&mysql_res,"can not get previous set index", - "SELECT MAX(SetInd)" // row[0] - " FROM exa_sets" - " WHERE ExaCod=%ld" - " AND SetInd<%u", - ExaCod,SetInd)) - Lay_ShowErrorAndExit ("Error: previous set index not found."); - - /***** Get previous set index (row[0]) *****/ - row = mysql_fetch_row (mysql_res); - if (row) - if (row[0]) - if (sscanf (row[0],"%u",&PrevSetInd) != 1) - Lay_ShowErrorAndExit ("Error when getting previous set index."); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - - return PrevSetInd; + return DB_QuerySELECTUnsigned ("can not get previous set index", + "SELECT MAX(SetInd)" + " FROM exa_sets" + " WHERE ExaCod=%ld" + " AND SetInd<%u", + ExaCod, + SetInd); } /*****************************************************************************/ /*************** Get next set index to a given index in an exam **************/ /*****************************************************************************/ // Input set index can be 0, 1, 2, 3... n-1 -// Return set index will be 1, 2, 3... n if next set exists, or 0 if no next set +// Return set index will be 1, 2, 3... n if next set exists, or big number if no next set static unsigned ExaSet_GetNextSetIndexInExam (long ExaCod,unsigned SetInd) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - unsigned NextSetInd = ExaSes_AFTER_LAST_QUESTION; // End of sets has been reached + unsigned NextSetInd; /***** Get next set index in an exam from database *****/ // Although indexes are always continuous... // ...this implementation works even with non continuous indexes - if (!DB_QuerySELECT (&mysql_res,"can not get next set index", - "SELECT MIN(SetInd)" // row[0] - " FROM exa_sets" - " WHERE ExaCod=%ld" - " AND SetInd>%u", - ExaCod,SetInd)) - Lay_ShowErrorAndExit ("Error: next set index not found."); - - /***** Get next set index (row[0]) *****/ - row = mysql_fetch_row (mysql_res); - if (row) - if (row[0]) - if (sscanf (row[0],"%u",&NextSetInd) != 1) - Lay_ShowErrorAndExit ("Error when getting next set index."); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); + NextSetInd = DB_QuerySELECTUnsigned ("can not get next set index", + "SELECT MIN(SetInd)" + " FROM exa_sets" + " WHERE ExaCod=%ld" + " AND SetInd>%u", + ExaCod, + SetInd); + if (NextSetInd == 0) + NextSetInd = ExaSes_AFTER_LAST_QUESTION; // End of sets has been reached return NextSetInd; } diff --git a/swad_game.c b/swad_game.c index 8416cd67f..87b144ade 100644 --- a/swad_game.c +++ b/swad_game.c @@ -1843,33 +1843,18 @@ static void Gam_RemAnswersOfAQuestion (long GamCod,unsigned QstInd) /*****************************************************************************/ /************ Get question index given game and code of question *************/ /*****************************************************************************/ +// Return 0 is question is not present in game static unsigned Gam_GetQstIndFromQstCod (long GamCod,long QstCod) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - unsigned QstInd = 0; // Return 0 is question is not present in game - /***** Get question index in a game given the question code *****/ - if (DB_QuerySELECT (&mysql_res,"can not get question index", - "SELECT QstInd" // row[0] - " FROM gam_questions" - " WHERE GamCod=%ld" - " AND QstCod=%ld", - GamCod, - QstCod)) - { - /***** Get question code (row[0]) *****/ - row = mysql_fetch_row (mysql_res); - QstInd = Str_ConvertStrToUnsigned (row[0]); - if (QstInd == 0) - Lay_ShowErrorAndExit ("Error: wrong question index."); - } - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - - return QstInd; + return DB_QuerySELECTUnsigned ("can not get question index", + "SELECT QstInd" + " FROM gam_questions" + " WHERE GamCod=%ld" + " AND QstCod=%ld", + GamCod, + QstCod); } /*****************************************************************************/ @@ -1902,25 +1887,12 @@ long Gam_GetQstCodFromQstInd (long GamCod,unsigned QstInd) static unsigned Gam_GetMaxQuestionIndexInGame (long GamCod) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - unsigned QstInd = 0; - /***** Get maximum question index in a game from database *****/ - DB_QuerySELECT (&mysql_res,"can not get last question index", - "SELECT MAX(QstInd)" // row[0] - " FROM gam_questions" - " WHERE GamCod=%ld", - GamCod); - row = mysql_fetch_row (mysql_res); - if (row[0]) // There are questions - if (sscanf (row[0],"%u",&QstInd) != 1) - Lay_ShowErrorAndExit ("Error when getting last question index."); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - - return QstInd; + return DB_QuerySELECTUnsigned ("can not get last question index", + "SELECT MAX(QstInd)" + " FROM gam_questions" + " WHERE GamCod=%ld", + GamCod); } /*****************************************************************************/ @@ -1931,33 +1903,16 @@ static unsigned Gam_GetMaxQuestionIndexInGame (long GamCod) unsigned Gam_GetPrevQuestionIndexInGame (long GamCod,unsigned QstInd) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - unsigned PrevQstInd = 0; - /***** Get previous question index in a game from database *****/ // Although indexes are always continuous... // ...this implementation works even with non continuous indexes - if (!DB_QuerySELECT (&mysql_res,"can not get previous question index", - "SELECT MAX(QstInd)" // row[0] - " FROM gam_questions" - " WHERE GamCod=%ld" - " AND QstInd<%u", - GamCod, - QstInd)) - Lay_ShowErrorAndExit ("Error: previous question index not found."); - - /***** Get previous question index (row[0]) *****/ - row = mysql_fetch_row (mysql_res); - if (row) - if (row[0]) - if (sscanf (row[0],"%u",&PrevQstInd) != 1) - Lay_ShowErrorAndExit ("Error when getting previous question index."); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - - return PrevQstInd; + return DB_QuerySELECTUnsigned ("can not get previous question index", + "SELECT MAX(QstInd)" + " FROM gam_questions" + " WHERE GamCod=%ld" + " AND QstInd<%u", + GamCod, + QstInd); } /*****************************************************************************/ @@ -1968,31 +1923,20 @@ unsigned Gam_GetPrevQuestionIndexInGame (long GamCod,unsigned QstInd) unsigned Gam_GetNextQuestionIndexInGame (long GamCod,unsigned QstInd) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - unsigned NextQstInd = Mch_AFTER_LAST_QUESTION; // End of questions has been reached + unsigned NextQstInd; /***** Get next question index in a game from database *****/ // Although indexes are always continuous... // ...this implementation works even with non continuous indexes - if (!DB_QuerySELECT (&mysql_res,"can not get next question index", - "SELECT MIN(QstInd)" // row[0] - " FROM gam_questions" - " WHERE GamCod=%ld" - " AND QstInd>%u", - GamCod, - QstInd)) - Lay_ShowErrorAndExit ("Error: next question index not found."); - - /***** Get next question index (row[0]) *****/ - row = mysql_fetch_row (mysql_res); - if (row) - if (row[0]) - if (sscanf (row[0],"%u",&NextQstInd) != 1) - Lay_ShowErrorAndExit ("Error when getting next question index."); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); + NextQstInd = DB_QuerySELECTUnsigned ("can not get next question index", + "SELECT MIN(QstInd)" + " FROM gam_questions" + " WHERE GamCod=%ld" + " AND QstInd>%u", + GamCod, + QstInd); + if (NextQstInd == 0) + NextQstInd = Mch_AFTER_LAST_QUESTION; // End of questions has been reached return NextQstInd; } diff --git a/swad_match.c b/swad_match.c index e485f1613..b6834d2f4 100644 --- a/swad_match.c +++ b/swad_match.c @@ -3494,7 +3494,7 @@ static void Mch_ShowMatchScore (const struct Mch_Match *Match) double Range; double NumRowsPerScorePoint; double Score; - unsigned MaxUsrs = 0; + unsigned MaxUsrs; unsigned NumUsrs; unsigned NumRowForThisScore; unsigned NumRow; @@ -3507,26 +3507,14 @@ static void Mch_ShowMatchScore (const struct Mch_Match *Match) NumRowsPerScorePoint = (double) Mch_NUM_ROWS_SCORE / Range; /***** Get maximum number of users *****/ - if (DB_QuerySELECT (&mysql_res,"can not get max users", - "SELECT MAX(NumUsrs)" // row[0] - " FROM (SELECT COUNT(*) AS NumUsrs" - " FROM mch_results" - " WHERE MchCod=%ld" - " GROUP BY Score" - " ORDER BY Score) AS Scores", - Match->MchCod)) - { - row = mysql_fetch_row (mysql_res); - - /* Get maximum number of users (row[0]) *****/ - if (row) - if (row[0]) - if (sscanf (row[0],"%u",&MaxUsrs) != 1) - MaxUsrs = 0; - } - - /* Free structure that stores the query result */ - DB_FreeMySQLResult (&mysql_res); + MaxUsrs = DB_QuerySELECTUnsigned ("can not get max users", + "SELECT MAX(NumUsrs)" + " FROM (SELECT COUNT(*) AS NumUsrs" + " FROM mch_results" + " WHERE MchCod=%ld" + " GROUP BY Score" + " ORDER BY Score) AS Scores", + Match->MchCod); /***** Get scores from database *****/ NumScores = (unsigned)