运行此查询时:
SELECT
a.id,
pub.name AS publisher_name,
pc.name AS placement_name,
b.name AS banner_name,
a.lead_id,
a.partner_id,
a.type,
l.status,
s.correctness,
a.landing_page,
t.name AS tracker_name,
a.date_view,
a.date_action
FROM actions AS a
LEFT JOIN publishers AS pub ON a.publisher_id = pub.id
LEFT JOIN placements AS pc ON pc.publisher_id = pub.id
LEFT JOIN banners AS b ON b.campaign_id = a.campaign_id
LEFT JOIN leads l ON
l.lead_id = a.lead_id
AND l.created = (
SELECT MAX(created) from leads l2 where l2.lead_id = l.lead_id
)
LEFT JOIN statuses AS s ON l.status = s.status
LEFT JOIN trackers AS t ON t.id = a.tracker_id
LIMIT 10
我可以按actions
表中的每一列进行排序。但是,当我尝试ORDER BY b.name
(来自banners
表,加入actions.banner_id
)或ORDER BY l.lead_id
(加入leads
处于更复杂的情况时),如上所示)MySQL运行查询的时间很长(大多数表都有数万条记录)。在性能方面,是否可以按连接列进行排序?
答案 0 :(得分:1)
您应该在要排序的列的表上使用内部联接重写查询。
例如,如果您对actions.banner_id
SELECT ...
FROM actions AS a
JOIN banners AS b ON b.campaign_id = a.campaign_id
LEFT JOIN *rest of the query*
除非没有足够的横幅可以加入行动以产生总共10行,否则您将获得相同的结果。
我猜不是这样,否则你不会在banner_id上排序。
答案 1 :(得分:0)
您可以先在子查询中过滤(按顺序,在哪里等)记录,然后将结果与其余表格连接起来。