编写此查询的最佳方式



我正在做一个子查询连接到另一个表,因为我希望能够对我得到的结果进行排序,我只需要第一行,但我需要它们以某种方式排序,所以我会得到最低的id。

我尝试将LIMIT 1添加到此,但随后完整查询返回0结果;所以现在它没有限制,在EXPLAIN中,我有两行显示他们正在使用auction_media表的完整10k+行。

我这样写是为了避免必须为每一行单独查询auction_media表,但现在我认为这种方式不是那么好,如果它必须使用整个auction_media表?

哪条路更好?我有它的方式或单独查询auction_media表?…还是有更好的办法?

代码如下:

SELECT 
    a.auction_id,
    a.name,
    media.media_url
FROM 
    auctions AS a 
    LEFT JOIN users AS u ON u.user_id=a.owner_id 
    INNER JOIN ( SELECT media_id,media_url,auction_id 
                         FROM auction_media 
                         WHERE media_type=1 
                          AND upload_in_progress=0 
                         ORDER BY media_id ASC
            ) AS media 
            ON a.auction_id=media.auction_id 
WHERE a.hpfeat=1 
  AND a.active=1 
  AND a.approved=1 
  AND a.closed=0 
  AND a.creation_in_progress=0 
  AND a.deleted=0 
  AND (a.list_in='auction' OR u.shop_active='1') 
GROUP BY a.auction_id;

Edit:通过我的测试,使用上面的查询似乎是更快的方法;然而,我担心,如果这将仍然是情况下,当auction_media表增长到像1M行或什么的。

edit:正如评论中所述- DISTINCT不是必需的,因为拍卖表只能与(最多)一个用户表行和内部查询中的一行相关联。

你可能想试试这个。外部查询的GROUP BY被替换为DISTINCT,因为你没有任何聚合函数。内部查询被替换为查找每个auction_id中最小的media_id的查询,然后join返回以获取media_url。(因为我不知道media_id和auction_id是否是复合唯一键,所以我使用了相同的WHERE子句来帮助消除潜在的重复。)

SELECT
    a.auction_id,
    a.name,
    auction_media.media_url
FROM auctions AS a 
LEFT JOIN users AS u 
  ON u.user_id=a.owner_id 
INNER JOIN (SELECT auction_id, MIN(media_id) AS media_id
            FROM auction_media 
            WHERE media_type=1 
               AND upload_in_progress=0 
            GROUP BY auction_id) AS media 
  ON a.auction_id=media.auction_id 
INNER JOIN auction_media
  ON auction_media.media_id = media.media_id
    AND auction_media.auction_id = media.auction_id
    AND auction_media.media_type=1 
    AND auction_media.upload_in_progress=0 
WHERE a.hpfeat=1 
  AND a.active=1 
  AND a.approved=1 
  AND a.closed=0 
  AND a.creation_in_progress=0 
  AND a.deleted=0 
  AND (a.list_in='auction' OR u.shop_active='1');

相关内容

  • 没有找到相关文章

最新更新