简体   繁体   中英

Group_concat is very slow

Hello I have a problem with the performance of my query, from what I see, group_concat seems to performs very slowly. Infact, if I comment it, speed gains go up 100x . Is there anyway I can change the query so that it can be faster?Here is the query:

SELECT 
      g.id_gara, 
      ( SELECT 
              GROUP_CONCAT( TIG.sigla, (' - '), Cl.sigla, (' ;'), 
              ( SELECT color 
                   FROM bootstrap_colors 
                   where non_color = '0' 
                   ORDER BY RAND() 
                   LIMIT 1 ) ) AS class_sigla 
           FROM 
              associazione_gara_tipologiagara_classifica as A 
                 INNER JOIN tipologie_gare AS TIG 
                    ON A.id_tipologia_gara = TIG.ID 
                 INNER JOIN classifiche_gare AS Cl 
                    ON A.id_classifica = Cl.idclassifiche_gare 
           WHERE 
              A.id_gara = g.id_gara 
           GROUP BY 
              A.id_gara ) cat_class_list,
      tg.bootstrap_description color_gara, 
      ta.bootstrap_description color_aggiudicazione 
   FROM 
      gara g 
         LEFT OUTER JOIN ente AS en 
            ON g.id_ente = en.ID
         LEFT OUTER JOIN tipo_gara AS tg 
            ON g.tipo_gara = tg.idtipo_gara
         LEFT OUTER JOIN admin AS ad 
            ON g.opElab = ad.id
         LEFT OUTER JOIN ente_presso AS ep 
            ON g.indirizzo_ente = ep.idente_presso
         LEFT OUTER JOIN tipo_aggiudicazione_gara AS ta 
            ON g.criterio_aggiudicazione = ta.idtipo_aggiudicazione_gara
   WHERE 
      (en.e_prov LIKE '%bn%') 
   ORDER BY 
      g.id_gara DESC 
   LIMIT 10

I was thinking that alternatively, I could using synched ajax calls to fetch the information which I would get with the CSV given by group_concat, do you think that would be better?

Here is the result with EXPLAIN

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   PRIMARY     en  ALL     PRIMARY     NULL    NULL    NULL    12889   Using where; Using temporary; Using filesort
1   PRIMARY     g   ref     id_ente     id_ente     5   disasrl.en.ID   4   NULL
1   PRIMARY     tg  eq_ref  PRIMARY     PRIMARY     4   disasrl.g.tipo_gara     1   NULL
1   PRIMARY     ad  eq_ref  PRIMARY,id  PRIMARY     4   disasrl.g.opElab    1   Using index
1   PRIMARY     ep  eq_ref  PRIMARY     PRIMARY     4   disasrl.g.indirizzo_ente    1   Using index
1   PRIMARY     ta  eq_ref  PRIMARY     PRIMARY     4   disasrl.g.criterio_aggiudicazione   1   NULL
2   DEPENDENT SUBQUERY  A   ALL     NULL    NULL    NULL    NULL    152721  Using where; Using filesort
2   DEPENDENT SUBQUERY  Cl  eq_ref  PRIMARY     PRIMARY     4   disasrl.A.id_classifica     1   NULL
2   DEPENDENT SUBQUERY  TIG     eq_ref  PRIMARY     PRIMARY     4   disasrl.A.id_tipologia_gara     1   NULL
3   UNCACHEABLE SUBQUERY    bootstrap_colors    ALL     NULL    NULL    NULL    NULL    11  Using where; Using temporary; Using filesort

It so happens to be that the mysql optimizer didn't perform efficiently. It can't resolve easily the many joins present in the query. So the best to resolve this problen is to you use SELECT STRAIGHT_JOIN instead of just SELECT so that you force the optimizer to execute "as is" the query without trying to optimize it.

Saluti.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM