我有一个查询,它适用于1000条记录或更少,但现在我需要优化它的50,000条记录,当我运行它时,它只是停顿...更好的优化SELECT SQL查询50,000条记录
这里是我的代码:
SELECT
b1.account_num,b1.effective_date as ed1,b1.amount as am1,
b2.effective_date as ed2,b2.amount as am2
FROM bill b1
left join bill b2 on (b1.account_num=b2.account_num)
where b1.effective_date = (select max(effective_date) from bill where account_num = b1.account_num)
and (b2.effective_date = (select max(effective_date) from bill where account_num = b1.account_num and effective_date < (select max(effective_date) from bill where account_num = b1.account_num)) or b2.effective_date is null)
ORDER BY b1.effective_date DESC
我的目标是获得从一个表最新的两个有效日期和金额与多条记录。
附加'EXPLAIN'查询规划输出 – Antoniossss
我通常发现,MySQL的连接优化比相关子好,虽然我听说它在最近的版本更好。 – Barmar
“or b2.effective_date is null”的删除对结果有什么影响!?!? – Strawberry