在我的查詢中,我使用了一個子查詢,該子查詢獲得給定產品的第二個最後一個報價日期。MySQL優化使用WHERE子句的子查詢?
這裏是我的子查詢:
LEFT JOIN (SELECT product_id, MAX(offer_date) AS sec_last_date
FROM t_offers AS s1
WHERE offer_date < (SELECT MAX(offer_date)
FROM t_offers
WHERE product_id=s1.product_id)
GROUP BY product_id) AS t2 ON t2.product_id=p.id
LEFT JOIN t_offers AS o2 ON o2.product_id=t2.product_id AND
o2.offer_date=t2.sec_last_date
它工作正常,但是目前是隻在t_offers表幾行。
由於WHERE
子句迫使MySQL爲每個product_id
迭代t_offers表,所以它可能不會在數千行或數百萬行中發揮作用。
我該如何優化這個子查詢?
不知這個作品真的很好,你說的。例如,這沒有意義'product_id = s1.product_id',因爲它與'product_id = product_id'相同。 – 2014-11-21 10:03:04
是的,我可以刪除product_id = product_id並得到相同的結果。 但是我仍然在我的子查詢中有一個WHERE,所以pb保持不變。 – Duddy67 2014-11-21 11:37:07