0

私はSQLの専門家ではないので、次のクエリについてサポートが必要です。最適化が不十分で、実行に時間がかかりすぎます。クエリを高速化するIN()ためにに置き換えることは可能ですか?JOIN()どうやってするの?

SELECT * 
FROM   shop_orders 
WHERE  id IN (SELECT orders_id 
              FROM   shop_orders_data 
              WHERE  closed = '1' /*AND backorder='0'*/) 
       AND id IN (SELECT orders_id 
                  FROM   shop_orders_products 
                  WHERE  products_id IN (SELECT id 
                                         FROM   shop_products 
                                         WHERE  artno = '120000' 
                                                 OR name LIKE '%120000%')) 
ORDER  BY created DESC 
4

4 に答える 4

3

LIKEがここで最大の遅延を引き起こしていると思います...artnoは十分ではないでしょうか?LIKEは通常、非常に費用のかかる手順です。

于 2012-06-05T15:08:23.600 に答える
2

私はこれがそれをするべきだと思います:

SELECT 
  s.* 
FROM 
  shop_orders s 
INNER JOIN
  shop_orders_data od 
ON 
  s.id=od.orders_id 
INNER JOIN
  shop_orders_products sop 
ON 
  s.id=sop.orders_id 
INNER JOIN 
  shop_products sp 
ON 
  sop.products_id=sp.id 
WHERE
  od.closed=1
AND 
  ( sp.artno='120000' or sp.name LIKE '%120000%' )
于 2012-06-05T15:08:19.733 に答える
1

はい、あなたは正しい道を進んでいます-これらのIN演算子ではなく、これらすべてのテーブル間で内部結合を行うことができます。

SELECT
    so.* 
FROM 
    shop_orders so
    INNER JOIN shop_orders_data sod
       ON so.id = sod.orders_id
    INNER JOIN shop_orders_products sop
       ON sod.id = sop.orders_id
    INNER JOIN shop_products sp
       ON sop.products_id = sp.id
WHERE sod.closed = '1'
    AND (sp.artno = '120000' OR sp.name LIKE '%120000%')
于 2012-06-05T15:07:56.287 に答える
1
Select so.*
from shop_orders as so
     join shop_orders_data as sod on sod.orders_id = so.id
     join (select orders_id 
           from   shop_orders_products as sop
                  join shop_products as sp on sp.id = sop.products_id
           where  sp.artno = '120000' 
                  OR sp.name like '%120000%') as sop on sop.orders_id = so.id
where 
  sod.closed = '1'
order by so.created desc
于 2012-06-05T15:11:30.193 に答える