0

我有两张表:附件和订单。

[accessories.category] ​​包含一个类别列表,即 Red、Green、Blue ... orders 表包含过去订单的列表。

我要选择:

10 records from [accessories.category] WHERE category = 'Red' AND
10 records from [accessories.category] WHERE category = 'Green' AND
10 records from [accessories.category] WHERE category = 'Blue'

我还想引用连接 [accessories.catno] 的订单表,它等于 [orders.catno],仅显示 [accessories.stocklevel] >0 的产品,并按 COUNT(orders.sold) DESC 订购记录集(显示畅销商品第一的)。

因为我想从每个类别中获取一组记录,所以我认为 UNION ALL 查询是合适的,而且确实有效。问题是投入生产的速度不够快,因为在我的开发盒上执行需要 0.3 秒。如果我删除“ORDER BY sold DESC”,查询只需 0.0236 秒,这是可以接受的。问题是我不能丢失 ORDER BY。

顺便说一句:我已经索引了我需要的所有字段。这是查询:

(SELECT 
accessories.catno, 
accessories.category, 
accessories.header, 
accessories.description, 
accessories.specialoffer, 
accessories.picture, 
accessories.unit, 
accessories.addinfo, 
accessories.post, 
accessories.price, 
accessories.vat, 
accessories.soundclip, 
COUNT(orders.catno) AS sold 
FROM 
accessories, orders 
WHERE 
orders.catno = accessories.catno 
AND 
accessories.category = 'Red' 
AND accessories.stocklevel > 0 AND 
accessories.category = 'Red' 
GROUP BY catno 
ORDER BY sold DESC 
LIMIT 10) 

UNION ALL 

(SELECT 
accessories.catno, 
accessories.category, 
accessories.header, 
accessories.description, 
accessories.specialoffer, 
accessories.picture, 
accessories.unit, 
accessories.addinfo, 
accessories.post, 
accessories.price, 
accessories.vat, 
accessories.soundclip, 
COUNT(orders.catno) AS sold 
FROM 
accessories, orders 
WHERE 
orders.catno = accessories.catno 
AND 
accessories.category = 'Green' 
AND accessories.stocklevel > 0 AND 
accessories.category = 'Green' 
GROUP BY catno 
ORDER BY sold DESC 
LIMIT 10) 

UNION ALL

(SELECT 
accessories.catno, 
accessories.category, 
accessories.header, 
accessories.description, 
accessories.specialoffer, 
accessories.picture, 
accessories.unit, 
accessories.addinfo, 
accessories.post, 
accessories.price, 
accessories.vat, 
accessories.soundclip, 
COUNT(orders.catno) AS sold 
FROM 
accessories, orders 
WHERE 
orders.catno = accessories.catno 
AND 
accessories.category = 'Blue' 
AND accessories.stocklevel > 0 AND 
accessories.category = 'Blue' 
GROUP BY catno 
ORDER BY sold DESC 
LIMIT 10) 

不确定我是否在此查询中要求太多,或者是否需要重新考虑。有任何想法吗?

添加了 EXPLAIN 的输出。

解释输出

4

3 回答 3

2

正如亚历克斯建议的那样,您可以制作一个专栏并使用触发器使其保持最新状态。或者您可以定期运行查询(每小时?每 5 分钟?每 1 分钟?)并将结果保存在另一个表中(或者更好的是,缓存我猜你正在从中生成的 html 并完全避免查询)。

于 2013-01-24T21:07:34.317 回答
0

试试这个查询,虽然它未经测试,但试一试

SET @level = 0;
SET @group = '';

SELECT 
    *
FROM (
    SELECT
      accessories.catno,
      accessories.category,
      accessories.header,
      accessories.description,
      accessories.specialoffer,
      accessories.picture,
      accessories.unit,
      accessories.addinfo,
      accessories.post,
      accessories.price,
      accessories.vat,
      accessories.soundclip,
      COUNT(orders.catno)      AS sold,
          @level := IF(@group = accessories.category, @level+1, 1) AS level, 
          @group := accessories.category as E_Group 
    FROM accessories,
      orders
      left join orders
        on orders.catno = accessories.catno
    WHERE accessories.stocklevel > 0
    GROUP BY orders.catno, accessories.category
    ORDER BY orders.sold DESC
) rs
WHERE level < 11 
于 2013-01-24T20:49:03.667 回答
0

试试这个查询

 (  SELECT * FROM ( SELECT
accessories.catno, 
accessories.category, 
accessories.header, 
accessories.description, 
accessories.specialoffer, 
accessories.picture, 
accessories.unit, 
accessories.addinfo, 
accessories.post, 
accessories.price, 
accessories.vat, 
accessories.soundclip, 
COUNT(orders.catno) AS sold 
FROM 
accessories, orders 
WHERE 
orders.catno = accessories.catno 
AND 
accessories.category = 'Red' 
AND accessories.stocklevel > 0 AND 
accessories.category = 'Red' 
GROUP BY catno )
ORDER BY sold DESC 
LIMIT 10 )

UNION ALL 

( SELECT * FROM (SELECT
accessories.catno, 
accessories.category, 
accessories.header, 
accessories.description, 
accessories.specialoffer, 
accessories.picture, 
accessories.unit, 
accessories.addinfo, 
accessories.post, 
accessories.price, 
accessories.vat, 
accessories.soundclip, 
COUNT(orders.catno) AS sold 
FROM 
accessories, orders 
WHERE 
orders.catno = accessories.catno 
AND 
accessories.category = 'Green' 
AND accessories.stocklevel > 0 AND 
accessories.category = 'Green' 
GROUP BY catno )
ORDER BY sold DESC 
LIMIT 10)

UNION ALL

( SELECT * FROM ( SELECT 
accessories.catno, 
accessories.category, 
accessories.header, 
accessories.description, 
accessories.specialoffer, 
accessories.picture, 
accessories.unit, 
accessories.addinfo, 
accessories.post, 
accessories.price, 
accessories.vat, 
accessories.soundclip, 
COUNT(orders.catno) AS sold 
FROM 
accessories, orders 
WHERE 
orders.catno = accessories.catno 
AND 
accessories.category = 'Blue' 
AND accessories.stocklevel > 0 AND 
accessories.category = 'Blue' 
GROUP BY catno )
ORDER BY sold DESC 
LIMIT 10)
于 2013-01-24T20:54:54.920 回答