0

我有下面的查询,我正在寻找优化它的方法(等使其更高效和更快)。有没有人有任何想法?

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
4

1 回答 1

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
于 2012-12-19T14:32:47.027 回答