0

アクセサリーと注文の2つのテーブルがあります。

[accessories.category]には、赤、緑、青などのカテゴリのリストが含まれています。注文テーブルには、過去の注文のリストが含まれています。

選択したい:

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秒かかるため、本番環境に移行するのに十分な速度がないことです。「ORDERBYsoldDESC」を削除すると、クエリにかかる時間はわずか0.0236秒で、許容範囲内です。問題は、ORDERBYを失うことができないということです。

ところで:私は必要なすべてのフィールドにインデックスを付けました。クエリは次のとおりです。

(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

Alexが示唆しているように、列を販売して、トリガーで最新の状態に保つことができます。または、クエリを定期的に(1時間ごと?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 に答える