这是我当前正在运行的查询(28小时过去了!):慢MySQL查询占满了我的磁盘空间
drop table if exists temp_codes;
create temporary table temp_codes
select distinct CODE from Table1;
alter table temp_codes
add primary key (CODE);
drop table if exists temp_ids;
create temporary table temp_ids
select distinct ID from Table1;
alter table temp_ids
add primary key (ID);
drop table if exists temp_ids_codes;
create temporary table temp_ids_codes
select ID, CODE
from temp_ids, temp_codes;
alter table temp_ids_codes
add index idx_id(ID),
add index idx_code(CODE);
insert into Table2(ID,CODE,cnt)
select
a.ID, a.CODE, coalesce(count(t1.ID), 0)
from
temp_ids_codes as a
left join Table1 as t1 on (a.ID = t1.ID and a.CODE=t1.CODE)
group by
a.ID, a.CODE;
我的表是这样的(表1):
ID CODE
-----------------
0001 345
0001 345
0001 120
0002 567
0002 034
0002 567
0003 567
0004 533
0004 008
......
(millions of rows)
而且我运行上面的查询,以获得本(表2):
ID CODE CNT
1 008 0
1 034 0
1 120 1
1 345 2
1 533 0
1 567 0
2 008 0
2 034 1
...
CNT是每个代码的计数的每个ID .. 如何以最佳方式实现此目的以提高性能并且不使用磁盘空间? 谢谢
您确定只有6个编码?我怀疑交叉连接产生的数据比你想象的要多得多。 –
不,我有成千上万的代码...这只是一个样本 – user2578185
用LIMIT 1000开始查询并查看结果有什么问题 – jaczes