0

my_table 包含 290M 行,我希望优化以下查询

select
  col1,
  col2,
  group_concat(distinct case when col3=1 then col4 end) c1,
  group_concat(distinct case when col3=2 then col4 end) c2,
  ...
  group_concat(distinct case when col3=70 then col4 end) c70
from my_table
group by col1,col2
order by null

我已经尝试过像这样运行较小的查询,但整个事情更糟

select
  col1,
  col2,
  group_concat(distinct case when col3=1 then col4 end) c1
from my_table
group by col1,col2
order by null

有没有办法做到这一点?

4

2 回答 2

0

(请使用真实的列名;那里通常有有用的线索。)

也许这会更快......

首先,让我们看看GROUP_CONCATs一次完成所有操作的速度有多快:

SELECT col3,
       GROUP_CONCAT(DISTINCT col4) AS list
    FROM my_table
    GROUP BY col3;

这将需要一次全表扫描(2.9 亿行),但可以加快速度

INDEX(col3, col4)  -- in this order

这是“覆盖”。

但是,既然你已经col1col2作品弄混了,让我们改为

SELECT col1, col2, col3,
       GROUP_CONCAT(DISTINCT col4) AS list
    FROM my_table
    GROUP BY col1, col3, col3;

INDEX(col1, col2, col3, col4)   -- in this order

那时,您拥有所有数据,但需要“透视”它。(见[pivot]标签。)

于 2019-04-17T03:17:51.307 回答
0

这是一个艰难的过程,因为您只针对一个表进行查询。我可以建议以下索引:

CREATE INDEX idx ON my_table (col1, col2, col3, col4);

MySQL可能会选择使用这个索引,因为(col1, col2)它可以对每个组进行索引扫描以找到 的每个值col3,然后将 的不同值连接在一起col4

于 2019-03-25T09:44:09.023 回答