1

2 つのクエリがあります。

クエリ # 1: 一部のデータを一時テーブルに挿入します

クエリ # 2: 一時テーブル (いくつかの結合を含む) からのデータを新しいテーブルに挿入します。

クエリ #1 だけを実行すると、3 秒かかります。クエリ #2 だけを実行すると、12 秒かかります。

両方を 1 つの実行として一緒に実行すると、永久に (4 分以上) 実行されます。

この理由として何が考えられるでしょうか?

(私は自分のコードを投稿しますが、非常に長く、部外者にはあまり理解できません。)

これが私のSQLです(自己責任で):単独で実行した場合は正常に実行されることに注意してください。

-- START QUERY #1
SELECT * INTO #TempProdForSiteGoingTrim
FROM 
    (
        SELECT
            p.idProduct
            , p.active
            , p.sku
            , p.description
            , p.listprice
            , p.price
            , p.imageurl
            , p.smallimageurl
            , p.idManufacturer
            , sortOrder 
            , CASE WHEN p.pgroup = '' OR p.pgroup IS NULL THEN CAST(p.idProduct AS VARCHAR) ELSE pgroup END [pgroup]
            , CASE WHEN pa2.attr IS NULL  THEN CAST(p.idProduct AS VARCHAR) ELSE CASE WHEN p.pgroup = '' OR p.pgroup IS NULL THEN CAST(p.idProduct AS VARCHAR) ELSE pgroup END END [RugGroup] 
            , pa1.attr [Color]
            , pa3.attr [Collection]
            , pa2.attr [RugSize]
            , pa4.attr[RugShape]
        FROM
            (SELECT DISTINCT idProduct  FROM ProdSite WHERE idSite = 39 ) s 
            INNER JOIN (SELECT * FROM products p  WHERE active = -1 ) p ON s.idproduct = p.idproduct 
            LEFT OUTER JOIN (SELECT t.idproduct, attr FROM  (SELECT max(idprodattr) [idprodattr], idproduct, b.idattrset  FROM productattr a JOIN product_attr b on a.idattr = b.idattr WHERE idattrset = 1 GROUP BY a.idproduct, b.idattrset )t JOIN productattr a ON t.idprodattr = a.idprodattr JOIN product_attr b ON a.idattr = b.idattr) pa1 ON p.idproduct = pa1.idproduct  
            LEFT OUTER JOIN  (SELECT t.idproduct, attr FROM  (SELECT max(idprodattr)[idprodattr], idproduct, b.idattrset  FROM productattr a JOIN product_attr b on a.idattr = b.idattr WHERE idattrset = 160 GROUP BY a.idproduct, b.idattrset )t JOIN productattr a ON t.idprodattr = a.idprodattr JOIN product_attr b ON a.idattr = b.idattr) pa2 ON p.idproduct = pa2.idproduct  
            LEFT OUTER JOIN (SELECT t.idproduct, attr FROM  (SELECT max(idprodattr)[idprodattr], idproduct, b.idattrset  FROM productattr a JOIN product_attr b on a.idattr = b.idattr WHERE idattrset = 6  GROUP BY a.idproduct, b.idattrset )t JOIN productattr a ON t.idprodattr = a.idprodattr JOIN product_attr b ON a.idattr = b.idattr) pa3 ON p.idproduct = pa3.idproduct 
            LEFT OUTER JOIN (SELECT t.idproduct, attr FROM  (SELECT max(idprodattr)[idprodattr], idproduct, b.idattrset  FROM productattr a JOIN product_attr b on a.idattr = b.idattr WHERE idattrset = 62  GROUP BY a.idproduct, b.idattrset )t JOIN productattr a ON t.idprodattr = a.idprodattr JOIN product_attr b ON a.idattr = b.idattr) pa4 ON p.idproduct = pa4.idproduct 
    )t 
 -- END QUERY #1

-- START QUERY #2
DECLARE @listRugSizes TABLE (idmanufacturer int, RugGroup VARCHAR(500), RugSizes VARCHAR(1000))  
INSERT INTO @listRugSizes 
SELECT
    t1.idmanufacturer
    , t1.RugGroup 
    , STUFF(( SELECT ', ' + RugSize FROM  #TempProdForSiteGoingTrim t2 WHERE t2.RugGroup = t1.RugGroup and t2.idmanufacturer = t1.idmanufacturer FOR XML PATH(''), TYPE ).value('.', 'varchar(max)'), 1, 1, '') [Values] 
FROM 
    #TempProdForSiteGoingTrim t1
GROUP BY
    t1.RugGroup 
    , t1.idmanufacturer


INSERT INTO [NewTableForSiteGoingTrim]
SELECT
    p.idProduct
    , p.sku
    , p.description
    , p.listPrice
    , p.price
    , p.imageUrl
    , p.smallImageUrl
    , p.sortOrder
    , p.idManufacturer
    , p.pgroup 
    , p.ruggroup  
    , c.idCategory
    , c.idCategory [fidcategory]
    , c.idParentCategory
    , c.idParentCategory [gidcategory]
    , pc.idParentCategory [hidCategory]  
    , ppc.idParentCategory [iidCategory]
    , m.Name 
    , rp.rewrite_key [rewrite_index]
    , rm.rewrite_key [rewrite_key] 
    , color [Color]
    , collection [Collection]
    , rugsize [RugSize] 
    , ruggroup.rugcount
    , ruggroup.maxprice
    , ruggroup.minprice
    , rs.RugSizes 
    , p.rugshape  
FROM
    #TempProdForSiteGoingTrim p 
    LEFT OUTER JOIN  ( 
                        SELECT
                            MIN(c.idCategory) [idCategory]
                            , c.idProduct
                        FROM
                            (
                                SELECT
                                    cp.idProduct
                                    , cp.idCategory
                                FROM
                                    dbo.categories_products cp 
                                    JOIN categories c ON cp.idcategory = c.idCategory
                                WHERE
                                    c.idSite = 24  
                            ) c
                        GROUP BY
                            c.idProduct 
                    ) cp ON p.idProduct = cp.idProduct 
    LEFT OUTER JOIN categories c ON cp.idCategory = c.idCategory  
    LEFT OUTER JOIN categories pc ON c.idParentCategory = pc.idCategory 
    LEFT OUTER JOIN categories ppc ON pc.idParentCategory = ppc.idCategory 
    LEFT OUTER JOIN manufacturer m ON p.idManufacturer = m.idManufacturer  
    LEFT OUTER JOIN (SELECT * FROM rewrite WHERE type = 3) rm ON p.idManufacturer = rm.id   
    LEFT OUTER JOIN (SELECT * FROM rewrite WHERE type = 1) rp ON p.idProduct = rp.id  
    LEFT OUTER JOIN @listRugSizes rs ON p.RugGroup = rs.RugGroup and p.idmanufacturer = rs.idmanufacturer
    LEFT OUTER  JOIN
        ( 
                SELECT
                    p.ruggroup 
                    , p.idmanufacturer
                    , min(price) [minPrice] 
                    , count(*) [RugCount] 
                    , m.maxprice
                FROM 
                    #TempProdForSiteGoingTrim p  
                    LEFT OUTER JOIN
                        (
                            SELECT
                                r.ruggroup
                                , r.idmanufacturer
                                , max(price) [maxprice]
                            FROM
                                #TempProdForSiteGoingTrim r
                            WHERE
                                r.idproduct = (SELECT MAX(idproduct) FROM #TempProdForSiteGoingTrim WHERE ruggroup = r.ruggroup AND price = r.price and idmanufacturer = r.idmanufacturer) 
                            GROUP BY
                                ruggroup 
                                , idmanufacturer
                        ) m ON p.ruggroup = m.ruggroup and p.idmanufacturer = m.idmanufacturer
                GROUP BY
                    p.ruggroup 
                    , m.maxprice 
                    , p.idmanufacturer

        ) ruggroup ON p.ruggroup = ruggroup.ruggroup  and p.idmanufacturer = ruggroup.idmanufacturer
-- END QUERY #2

編集

最後の結合「ruggroup」をグループのみに変更し、「ruggroup」と「idmanufacturer」ではなく列「ruggroup」で結合すると、正常に動作します。

4

0 に答える 0