是否有更好(更高性能)的方式加入MAX(id)(不使用subselect)而不是我的实际方式?我们正在处理庞大的数据库,并且正在尝试尽可能地优化
SELECT e.*, r.report_name FROM report r
LEFT JOIN engineer e ON e.id = r.id
WHERE e.id = (SELECT MAX(id) FROM engineer WHERE process = r.process)
提前致谢!
答案 0 :(得分:0)
也许这会对你有所帮助
WHERE e.id = (SELECT top 1 id
FROM engineer
WHERE process =r.process
order by id desc)
答案 1 :(得分:0)
我认为那更好
SELECT TOP 1 e.*, r.report_name FROM report r
LEFT JOIN engineer e ON e.id = r.id
ORDER BY e.id desc
答案 2 :(得分:0)
另一个选项:预先聚合整个集合,然后JOIN
预先聚合整个集合,以过滤其他表格。这将把它视为一个表,而不是为你遇到的每一行执行相同的SELECT;基于集合的逻辑与逐行激动的行。
SELECT
e.*,
r.report_name
FROM
report r
INNER JOIN -- the WHERE clause effectively makes this an INNER JOIN, so changed for readability
engineer e ON
e.id = r.id
INNER JOIN
(
SELECT Process, MAX(ID) MaxID
FROM Engineer
GROUP BY Process
) eMax ON
e.Process = eMax.Process AND
e.ID = eMax.MaxID