Hi,
I've tested the query in your environment and it's not slow.
We use Moodle 3.7.1+ debian 9 apache. I've seen that you've 445 students in that query it's a lot but not unusual.
We use perconna on a
mysql database. it took some seconds.
When you want to speed up a query a good way it's to reduce the scan on table for instance here can you try to replace the second line
FROM mdl_user u
by
FROM (SELECT * FROM mdl_user WHERE id IN ('228722','228149','168189','229367','229805','227938','5560','219930','228045','229821','228560','227835','229792','227908','228132','227976','228654','230221','228626','228181','228104','229983','106161','220144','229750','227979','227971','228154','4323','228101','227862','229995','5685','228094','227804','228866','228241','228224','227850','228240','228141','228480','65793','227788','227770','71086','211997','33013','227919','229949','81779','227876','228073','227935','220079','229753','229946','229217','229762','228262','109127','229770','228552','211294','228477','229580','228629','218857','229961','60687','75253','229381','154188','228179','229811','227917','229473','227913','228013','227765','229664','229819','152487','227852','228219','227974','230102','8830','229985','229548','199116','228105','47495','122094','228628','228037','227992','220142','218611','220062','229356','227800','43005','228070','229578','228623','135190','229945','230418','228147','230276','227841','219980','219877','227827','230287','220226','228129','228191','229984','228030','220116','228049','227758','228510','228130','23636','209896','228716','202966','227901','24732','230160','227810','228491','229333','218788','227875','229767','229329','228064','227819','227984','106349','229871','228018','229137','228016','227829','59986','82700','93097','214381','230065','228437','228170','229954','229751','209827','227793','228284','175613','229565','220324','228620','220321','227898','227998','70540','228291','228015','228211','55547','188990','3541','101463','228023','5140','227803','227806','227774','230273','219944','229315','227867','38462','227920','227780','228378','228356','167235','45250','228038','227890','220298','228289','228271','227818','230244','227789','227905','230285','227811','229363','220193','228556','220194','228452','123138','228339','230320','227978','228183','229282','228186','229826','227844','230072','229160','171011','228052','228283','79734','227836','220190','229377','230019','70425','218693','210','229078','219894','229739','230411','227851','227792','227918','227999','228636','229795','229964','228651','229970','229755','48236','227883','227878','227968','230163','228278','228136','228270','219947','228251','228225','229667','75873','228358','227906','227896','228469','228618','228479','219971','228158','227895','228644','227760','230403','199199','227993','75929','202913','227768','227988','206854','229033','229812','230374','227797','229994','218569','228081','228142','219948','228193','229102','218919','105539','229982','22851','113922','172433','228647','35567','228113','229957','228022','229800','228006','228322','217340','229009','91124','230256','227945','126454','227785','229713','227975','229741','230121','228279','227904','227830','227926','227882','228084','227763','229103','227858','78036','228244','228011','229955','228562','228239','227960','228027','230414','227786','228176','113902','220333','133113','228206','227808','227828','211957','227942','617','228168','228053','229584','230367','230013','227807','228097','97809','229969','229405','209841','227772','104596','228650','95558','219968','228624','227842','228468','34032','229748','227766','227934','224248','228190','228392','230018','11027','223227','228080','220302','228231','228242','227839','227985','227864','228140','122443','219908','227779','229988','228109','228209','227799','228245','228210','228042','228351','212281','228273','199941','227775','228625','220309','228103','82187','228492','220105','228662','227801','227816','57438','28680','229075','155852','223685','227891','228001','228790','228653','227795','172552','229085','220172','227838','228302','5541','9028','219912','228048','229824','228139','227871','227813','227986','230083','220177','227915','227771','229786','220174','229680','174966','230286','214625','228487','229122','228614','229368','227790','218541','227840','229796','228570')) u
I hope it's help.
Dominique.