在子查询中缓慢GROUP_CONCAT

时间:2013-06-11 09:44:40

标签: mysql

有一个子查询,它使用GROUP_CONCAT将结果分组到一个字段中。 但查询需要3秒才能运行,而没有使用0.05秒。 我索引了accessory.actionid和action.actionid是PK。 有没有提高绩效的解决方案?

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 

2 个答案:

答案 0 :(得分:1)

你可能最好使用它:

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

答案 1 :(得分:1)

因为你使group_concat的别名与导致mysql思考的表acc相同。

更改其中一个别名。

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

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