问题与在两个表中保存列表数据和潜在客户数据的列表站点有关。在查询列表和每个列表的潜在客户总数时,添加 ORDER BY 后结果非常慢。如果没有 ORDER BY,检索结果会非常快。任何有关重组以下查询的建议或帮助都会很棒!仅供参考,有 20k 个列表和 100k 个潜在客户。
SELECT ls.*, IFNULL(ld.total_leads, 0) AS total_leads
FROM listing ls
LEFT JOIN (SELECT listing_id, COUNT(listing_id) AS total_leads
FROM lead GROUP BY listing_id) ld
ON (ls.listing_id = ld.listing_id)
ORDER BY ls.listing_id DESC LIMIT 0,20
解释:
id select_type table type pos key ke ref rows Extra
1 PRIMARY ls ALL NULL NULL NULL NULL 17215 Using temporary; Using filesort
1 PRIMARY 2> ALL NULL NULL NULL NULL 12865
2 DERIVED lead ALL NULL NULL NULL NULL 117830 Using temporary; Using filesort