我有下面的查询,我正在寻找优化它的方法(等使它更有效和更快)。有没有人有任何想法?
SELECT s.oem_id, s.name, SUM(u.minimum_gfs) AS minimum_gfs, sum(u.minimum_ofs) AS
minimum_ofs, sum(u.minimum_wq) AS minimum_wq
FROM nmp_building_reqs AS s
LEFT OUTER JOIN nmp_building_util AS u
ON u.oem_id = s.oem_id
GROUP BY s.oem_id, s.name
答案 0 :(得分:5)
唯一明显的做法是确保将oem_id编入索引以使连接更快。
但是,我怀疑你可以在加入之前进行聚合,然后引入名称。这假设oem_id只有一个与之关联的名称。查询看起来像:
SELECT s.oem_id, s.name, u.minimum_gfs, u.minimum_ofs, u.minimum_wq
FROM nmp_building_reqs s left outer join
(select u.oem_id, SUM(u.minimum_gfs) AS minimum_gfs,
sum(u.minimum_ofs) AS minimum_ofs, sum(u.minimum_wq) AS minimum_wq
from nmp_building_util u
group by u.oem_id
) u
ON u.oem_id = s.oem_id