简体   繁体   中英

slow GROUP_CONCAT in sub query

have a sub query which uses GROUP_CONCAT to group results in one field. but the query takes 3 secs to run with this and 0.05secs without. I have indexed accessory.actionid and action.actionid is PK. Is there a solution to improve performance?

SELECT
  action.actiondate, acc.acc, acc.acccount
FROM
  `action`
LEFT JOIN 
  (SELECT 
    actionid,
    GROUP_CONCAT(accessory) AS acc,
    COUNT(actionid) AS acccount
  FROM
  accessorieslink 
  GROUP BY actionid) AS acc 
  ON acc.actionid = action.actionid 

You might be better off using this:

SELECT
    a.actiondate, 
    GROUP_CONCAT(IFNULL(al.accessory,'') ) as acc, 
    SUM(IF(al.actionid IS NULL,0,1)) as acccount
FROM
  `action` a
LEFT JOIN accessorieslink al
    ON al.actionid = a.actionid
GROUP BY
    a.actionid
ORDER BY 
    NULL

because you are making alias of group_concat same as the table acc which cause mysql to think.

change one of the aliace names.

   GROUP_CONCAT(accessory) AS acc,
                               ^^^--//------ this

and

  GROUP BY actionid) AS acc 
                        ^^--// and this

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