1

运行此查询时:

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 正在运行查询很长时间(大多数表有数万条记录)。在性能方面是否可以按连接列排序?

4

2 回答 2

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上排序。

于 2013-07-26T13:02:22.867 回答
0

您可以首先在子查询中过滤(排序依据、位置等)您的记录,然后将结果与其余表连接起来。

于 2013-07-26T12:38:46.130 回答