连接的MySQL查询占用时间太长

时间:2014-04-14 19:04:34

标签: mysql performance

我有一个足够简单的查询,需要花费近4分钟才能运行。

关于表格:

表A有25列和18354行 表B有24列和97889行

查询:

select (sum(a.a)/sum(a.ab)) as ab_avg,
sum(a.h) as h_sum,
((sum(a.2B) + sum(a.3B) + sum(a.4B))/sum(a.ab)) as b_avg,
(sum(a.sb) / (sum(a.sb) + sum(a.cs))) as sb_pct,
(sum(a.ab) / (sum(a.hr)) as ab_hr_ratio,
(sum(a.ab) / sum(a.so)) as ab_so_ratio,
b.lastname,
b.firstname
from a, b
where b.id = a.id
group by b.year

MySQL完全返回它应该返回的内容,但我想这个查询不应该花费4分钟来运行。我做错了会导致性能问题吗?

1 个答案:

答案 0 :(得分:2)

我添加了年份和id的索引,它在.45秒内运行。谢谢!