在我的查询中,我使用了一个子查询,该子查询获得给定产品的第二个最后一个报价日期。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