我有一個查詢,它適用於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