Brighthouse group_concat另类?

时间:2011-07-20 16:49:05

标签: mysql group-concat

我正在使用一个大型数据库,我们有一些关键查询依赖于group_concat(通过myisam)来分组时正确排序多个列。基本上,这样我们就可以确保在每一行中获得正确的数据。例如,如果我想知道每个邮政编码中特定状态下有多少记录,但每个地址只有一条记录:

(id是唯一的无符号数)

SELECT state     AS state,
       zip       AS zip,
       COUNT(id) AS total
FROM   (SELECT CAST(Substring_index(GROUP_CONCAT(id ORDER BY state, zip, city,
               address,
                      id),
               ',', 1)
               AS UNSIGNED)   AS id,
               CAST(Substring_index(GROUP_CONCAT(zip ORDER BY state, zip, city,
               address
                      , id),
               ',', 1
               ) AS UNSIGNED) AS zip,
               CAST(Substring_index(GROUP_CONCAT(state ORDER BY state, zip, city
               ,
                      address, id),
               ',',
               1) AS CHAR)    AS state
        FROM   DATABASE.table
        GROUP  BY st,
                  zip,
                  city,
                  address) AS a
GROUP  BY zip
ORDER  BY zip  

我有一个myisam和一个明亮的数据库,可以使用相同的数据。如果我只是做简单的选择,那么明亮的房子有巨大的性能优势,所以我希望找到一种方法来运行一个具有相同结果的查询,可以利用明亮的速度。

编辑:为了纠正自己,我想找到一个查询,用Brighthouse引擎完成同样的事情,而不是像我之前所述的innodb。

1 个答案:

答案 0 :(得分:1)

我最终发现我可以重新安排一些事情以获得我追求的目标。原始问题中的查询可以重写为:

SELECT state     AS state,
       zip       AS zip,
       COUNT(id) AS total
FROM   (SELECT state,
               zip,
               id
        FROM   (SELECT state,
                       zip,
                       id,
                       address
                FROM   DATABASE.table
                ORDER  BY id) AS a
        GROUP  BY address) AS a2
GROUP  BY zip
ORDER  BY zip  

...允许相同的结果集并利用Brighthouse引擎! : - )