求助,mysql语句优化,有explain的内容
三个表关联
select
sum(XXX)
from a
left join b on a.id1=b.id1 and a.id2=b.id2
left join c on c.id1=b.id1
where a.date between '' and '' (时间范围为1个月) and a.org in (1,2,3,4,5,6,7,8,9,10) and b.log=0
group by
b.id3,b.id4,b.id5,b.id6,b.id7,b.id8 (分组字段为6个)
查询时间为:5.658
其中b表中数据量最大,此时间范围内为392571条数据,整张表数据量为2989801条数据。
explain 后
id select_type tabel type rows extra
1 SIMPLE a rang 45113 Using where; Using index; Using temporary; Using filesort
1 SIMPLE b ref 8 Using where
1 SIMPLE c eq_ref 4 Using index
show profile block io,cpu for query 449;
status duration cpu_user cpu_system Block_ops_in Block_ops_out
Copying to tmp table 4.831741 4.890625 0 null null
分组是必须的,需要根据这个分组去确定返回结果集内的很多数据。而且这个6个字段是分组的依据,也没有其他方式去处理。
即便去掉分组直接查询剩余的部分,时间也要3秒以上。
mysql 版本:5.5
temp_table_size为35M,max_heap_table_size这个在配置文件里没有找到,就没有对这个做任何变动。
我在数据量比这个小的库上把temp_table_size的值调到了50M和100M,并没有明显的提升。
希望大神们帮着提一些优化的建议。
PS:
where后面的条件是根据参数来决定的,日期和org 肯定会有,但是时间范围不定,org in的内容也是不确定的。 已经按照org做了表分区了,单查一个org,一个月的数据0.704s就能出结果。