任务描述
1.问题描述:
    count() 查询 查询了大量不必要的字段和left join 查询,导致查询慢

2 .问题分析
  select count(1)  去掉不必要的字段查询和left 查询,sql优化后为:

SELECT Count(1)
FROM   (SELECT --P.ListingProductID,
               --P.CustomerID
               --P.OrderSourceID,
               --P.SiteCode,
               --P.ListingBasicID,
               --P.SKU,
               --P.OrderSourceSKU,
               --P.BuyItNowPrice,
               --P.EnableBestOffer,
               --B.Duration,
               --B.EnableTimerPosting,
               --B.TimerPosting,
               --P.MinBestOfferPrice,
               --P.AutoAcceptPrice,
               --P.Quantity,
               --P.EAN,
               --P.UPC,
               --P.EPID,
               --P.ParentListingProductID,
               --P.IsVariation,
               --P.ProductType,
               --B.TagName,
               B.ItemID
               --B.ListingType,
               --B.ProductTitle,
               --B.ProductSubTitle,
               --B.AuctionStartPrice,
               --B.AuctionReservePrice,
               --B.AuctionBuyItNowPrice,
               --B.Brand,
               --B.BrandMPN,
               --B.ClassID1,
               --B.ClassID2,
               --B.StoreClass1,
               --B.StoreClass2,
               --B.StoreURL,
               --B.StartTime,
               --B.FbaOrderSourceSKU,
               --B.EndTime,
               --Isnull(B.UpdateStatus, 0)           UpdateStatus,
               --B.ViewItemURL,
               --B.AddTime,
               --B.AdminID,
               --B.AdminName,
               --B.UpdateTime,
               --B.UpdateAdminID,
               --B.UpdateAdminName,
               --B.AddItemAdminID,
               --B.AddItemAdminName,
               --B.SellerAdminID,
               --B.SellerAdminName,
               --B.AddItemFee,
               --B.OnlineState,
               --B.Visits,
               --B.RowVersion,
               --B.SyncTime,
               --B.IsSync,
               --Isnull(B.QuantitySold, 0)           QuantitySold,
               --OS.OrderSourceName,
               --''                                  ClientSKU,
               --''                                  ProductNameCN,
               --NULL                                ProductState,
               --Isnull(ASP.EnableAutoSupplement, 0) EnableAutoSupplement,
               --Isnull(ASP.IsInventoryCheck, 0)     IsInventoryCheck,
               --ASP.ListingQty,
               --ASP.SupplementaryQty,
               --B.Warehouse,
               --0                                   SkuCount,
               --B.BidCount,
               --B.BidCurrentPrice,
               --P.OriginalRetailPrice
        FROM   RB_Ebay_Listing_Basic_V2 AS B WITH (NOLOCK)
               
               LEFT JOIN RB_Ebay_Listing_Product_V2 AS P WITH (NOLOCK)
                      ON B.CustomerID = P.CustomerID
                         AND B.ListingBasicID = P.ListingBasicID
                         AND B.OrderSourceID = P.OrderSourceID
                         AND B.SiteCode = P.SiteCode
               --LEFT OUTER JOIN RB_OrderSource AS OS WITH (NOLOCK)
               --             ON B.CustomerID = OS.CustomerID
               --                AND B.OrderSourceID = OS.ID
               --LEFT JOIN RB_Ebay_Listing_AutoSupplement_V2 ASP WITH (NOLOCK)
               --       ON ASP.CustomerID = P.CustomerID
               --          AND ASP.ListingBasicID = P.ListingBasicID
               --          AND ASP.ListingProductID = P.ListingProductID
               --          AND ASP.OrderSourceID = P.OrderSourceID
               --          AND ASP.SiteCode = P.SiteCode
               --LEFT JOIN RB_Ebay_SKU_Sale_Attach_V2 ESSA WITH (NOLOCK)
               --       ON ESSA.SKU = P.SKU
               --          AND ESSA.CustomerID = P.CustomerID
               --LEFT JOIN RB_Ebay_Listing_Markdown_Promotion MK WITH(NOLOCK)
               --       ON MK.CustomerID = P.CustomerID
               --          AND MK.ID = B.MarkdownPromotionID
        WHERE  P.CustomerID = 1
               AND P.IsVariation = 0
               AND B.OnlineState = 1
               AND ( EXISTS(SELECT 1
                            FROM   RB_Ebay_Listing_Product_V2 AS LPS (NOLOCK)
                                   LEFT JOIN RB_Product AS RP WITH (NOLOCK)
                                          ON LPS.CustomerID = RP.CustomerID
                                             AND LPS.SKU = RP.SKU
                                   
                            WHERE  LPS.CustomerID = P.CustomerID
                                   AND LPS.SiteCode = P.SiteCode
                                   AND LPS.OrderSourceID = P.OrderSourceID
                                   AND LPS.ListingBasicID = P.ListingBasicID) )
               AND ( EXISTS(SELECT 1
                            FROM   RB_Ebay_Listing_Product_V2 AS LPS (NOLOCK)
                                   INNER JOIN (SELECT RP.CustomerID,
                                                      RP.SKU
                                               FROM   RB_Product AS RP WITH (NOLOCK)
                                                      ) TRP
                                           ON LPS.CustomerID = TRP.CustomerID
                                              AND TRP.SKU = LPS.SKU
                            WHERE  LPS.CustomerID = P.CustomerID
                                   AND LPS.SiteCode = P.SiteCode
                                   AND LPS.OrderSourceID = P.OrderSourceID
                                   AND LPS.ListingBasicID = P.ListingBasicID) )
               AND ( EXISTS(SELECT 1
                            FROM   RB_Ebay_Listing_Product_V2 AS LPS (NOLOCK)
                                   INNER JOIN (SELECT RP.CustomerID,
                                                      RP.SKU
                                               FROM   RB_Product AS RP WITH (NOLOCK)
                                                      ) TRP
                                           ON LPS.CustomerID = TRP.CustomerID
                                              AND TRP.SKU = LPS.SKU
                            WHERE  LPS.CustomerID = P.CustomerID
                                   AND LPS.SiteCode = P.SiteCode
                                   AND LPS.OrderSourceID = P.OrderSourceID
                                   AND LPS.ListingBasicID = P.ListingBasicID) )) AS PagerTable
OPTION(maxdop 2) 
--12705522  5.45





2.慢查询sql

 SELECT COUNT(1) FROM ( 
                    SELECT P.ListingProductID,
                P.CustomerID,
                P.OrderSourceID,
                P.SiteCode,
                P.ListingBasicID,
                P.SKU,
                P.OrderSourceSKU,
                P.BuyItNowPrice,
                P.EnableBestOffer,
                B.Duration,
                B.EnableTimerPosting,
                B.TimerPosting,
                P.MinBestOfferPrice,
                P.AutoAcceptPrice,
                P.Quantity,
                P.EAN,
                P.UPC,
                P.EPID,
                P.ParentListingProductID,
                P.IsVariation,
                P.ProductType,
                B.TagName,
                B.ItemID,
                B.ListingType,
                B.ProductTitle,
                B.ProductSubTitle,
                B.AuctionStartPrice,
                B.AuctionReservePrice,
                B.AuctionBuyItNowPrice,
                B.Brand,
                B.BrandMPN,
                B.ClassID1,
                B.ClassID2,
                B.StoreClass1,
                B.StoreClass2,
                B.StoreURL,
                B.StartTime,
                B.FbaOrderSourceSKU,
                B.EndTime,
                Isnull(B.UpdateStatus, 0) UpdateStatus,
                B.ViewItemURL,
                B.AddTime,
                B.AdminID,
                B.AdminName,
                B.UpdateTime,
                B.UpdateAdminID,
                B.UpdateAdminName,
                B.AddItemAdminID,
                B.AddItemAdminName,
                B.SellerAdminID,
                B.SellerAdminName,
                B.AddItemFee,
                B.OnlineState,
                B.Visits,
                B.RowVersion,
                B.SyncTime,
                B.IsSync,
                ISNULL(B.QuantitySold,0) QuantitySold,
                OS.OrderSourceName,
                '' ClientSKU,
                '' ProductNameCN,
                NULL ProductState,
                Isnull(ASP.EnableAutoSupplement, 0)   EnableAutoSupplement,
                Isnull(ASP.IsInventoryCheck, 0)       IsInventoryCheck,
                ASP.ListingQty,
                ASP.SupplementaryQty,
                        
                B.Warehouse,
             
                0 SkuCount,
        B.BidCount,
        B.BidCurrentPrice,
        P.OriginalRetailPrice 
        FROM   RB_Ebay_Listing_Basic_V2 AS B WITH (NOLOCK) 
                 
            
                 INNER JOIN #tbOrderSourceID OrderSourceIDs ON B.OrderSourceID=OrderSourceIDs.Id 
                
         left JOIN RB_Ebay_Listing_Product_V2 AS P WITH (NOLOCK)
        ON B.CustomerID = P.CustomerID
    AND B.ListingBasicID = P.ListingBasicID
    AND B.OrderSourceID = P.OrderSourceID
    AND B.SiteCode = P.SiteCode 
              LEFT OUTER JOIN RB_OrderSource AS OS WITH (NOLOCK)
ON B.CustomerID = OS.CustomerID
AND B.OrderSourceID = OS.ID     
LEFT JOIN RB_Ebay_Listing_AutoSupplement_V2 ASP WITH (NOLOCK)
ON ASP.CustomerID = P.CustomerID
AND ASP.ListingBasicID = P.ListingBasicID
AND ASP.ListingProductID = P.ListingProductID
AND ASP.OrderSourceID = P.OrderSourceID
AND ASP.SiteCode = P.SiteCode
        LEFT JOIN RB_Ebay_SKU_Sale_Attach_V2 ESSA WITH (NOLOCK)
         ON ESSA.SKU = P.SKU
        AND ESSA.CustomerID = P.CustomerID
            
                 LEFT JOIN RB_Ebay_Listing_Markdown_Promotion MK  WITH(NOLOCK)
                     ON MK.CustomerID=P.CustomerID
                        AND MK.ID=B.MarkdownPromotionID    
             WHERE  P.CustomerID = 1 AND P.IsVariation = 0  AND B.OnlineState=1  AND (  EXISTS(SELECT NULL
                                            FROM   RB_Ebay_Listing_Product_V2 AS LPS (NOLOCK) LEFT JOIN RB_Product  AS RP WITH (NOLOCK) ON 
                                                   LPS.CustomerID=RP.CustomerID AND LPS.SKU=RP.SKU  
                                            INNER JOIN #tbProductStates ProductStatesIDs ON RP.ProductState=ProductStatesIDs.Id 
                                            WHERE  LPS.CustomerID = P.CustomerID 
                                                    AND LPS.SiteCode = P.SiteCode 
                                                    AND LPS.OrderSourceID = P.OrderSourceID 
                                                    AND LPS.ListingBasicID = P.ListingBasicID))  AND (  EXISTS(SELECT NULL
                                                FROM   RB_Ebay_Listing_Product_V2 AS LPS (NOLOCK) 
                                                INNER JOIN (
                                                    SELECT RP.CustomerID,RP.SKU FROM RB_Product AS RP WITH (NOLOCK)
                                                    INNER JOIN #tbProductViolations ProductPropertys 
                                                    ON RP.ProductProperty=ProductPropertys.Id  ) TRP 
                                                        ON LPS.CustomerID=TRP.CustomerID AND TRP.SKU=LPS.SKU 
                                                WHERE   LPS.CustomerID = P.CustomerID
                                                        AND LPS.SiteCode = P.SiteCode
                                                        AND LPS.OrderSourceID = P.OrderSourceID
                                                        AND LPS.ListingBasicID = P.ListingBasicID) )  AND (  EXISTS(SELECT NULL
                                                FROM   RB_Ebay_Listing_Product_V2 AS LPS (NOLOCK) 
                                                INNER JOIN (
                                                    SELECT RP.CustomerID,RP.SKU FROM RB_Product AS RP WITH (NOLOCK)
                                                    INNER JOIN #tbWithBattery WithBatterys
                                                    ON RP.WithBattery=WithBatterys.Id ) TRP
                                                        ON LPS.CustomerID=TRP.CustomerID AND TRP.SKU=LPS.SKU
                                                WHERE   LPS.CustomerID = P.CustomerID
                                                        AND LPS.SiteCode = P.SiteCode
                                                        AND LPS.OrderSourceID = P.OrderSourceID
                                                        AND LPS.ListingBasicID = P.ListingBasicID) )  ) AS PagerTable option(maxdop 2)
附件
历史记录  
  1. 2022-04-28 15:15:16, 由 陈少军 创建。
  2. 2022-04-28 15:15:47, 由 陈少军 指派给 罗友军。
    修改了 预计剩余,旧值为 "0",新值为 "2"。
    参考解决方案修改
  3. 2022-04-28 17:09:18, 由 罗友军 指派给 魏鹏军。
    修改了 指派给,旧值为 "luoyoujun",新值为 "weipengjun"。
  4. 2022-05-21 18:00:51, 由 罗友军 指派给 温林生。
    修改了 指派给 ,旧值为 "weipengjun",新值为 "wenlinsheng"。
    修改了 预计剩余,旧值为 "2",新值为 "8"。
  5. 2022-05-27 15:13:41, 由 温林生 完成。
    修改了 总计消耗,旧值为 "0",新值为 "7.5"。
    修改了 指派给 ,旧值为 "wenlinsheng",新值为 "liletong"。
    修改了 实际完成,旧值为 "",新值为 "2022-05-27 15:06:12"。
    修改了 任务状态,旧值为 "wait",新值为 "done"。
    修改了 实际开始,旧值为 "0000-00-00 00:00:00",新值为 "2022-05-27 15:13:41"。
    修改了 预计剩余,旧值为 "8",新值为 "0"。
    修改了 由谁完成,旧值为 "",新值为 "wenlinsheng"。
    上传了附件 #26451.sql

    该页面查询Count 时已将不必需要的字段去除掉,但是 关联其他表时候这步目前暂时无法处理,因为改动量过大,且之前的代码耦合度太高了不太好改动,容易引发大问题等因素,因为放弃 减少关联其他表。


  6. 2022-05-27 15:17:18, 由 温林生 添加备注。

    附加SQL截图:

    附加页面截图:

  7. 2022-07-16 17:09:17, 由 zhengqiao 关闭。
    修改了 任务状态,旧值为 "done",新值为 "closed"。
    修改了 指派给 ,旧值为 "liletong",新值为 "closed"。
    修改了 由谁关闭,旧值为 "",新值为 "zhengqiao"。
    修改了 关闭时间,旧值为 "0000-00-00 00:00:00",新值为 "2022-07-16 17:09:17"。
    修改了 关闭原因,旧值为 "",新值为 "done"。
所属迭代 系统管理部 - 系统服务优化
所属模块 /
相关需求 暂无
指派给 Closed 于 2022-07-16 17:09:17
任务类型 后端
任务标签 初始
开发组
任务状态 已关闭
进度 100%
优先级 P2
关键词
抄送给 刘灿 黄金龙 胡晓龙
由谁创建 陈少军 于 2022-04-28 15:15:16
由谁完成 温林生 于 2022-05-27 15:06:12
由谁取消 暂无
由谁关闭 zhengqiao 于 2022-07-16 17:09:17
关闭原因 已完成
最后编辑 zhengqiao 于 2022-07-16 17:09:17
团队 最初预计 总计消耗 预计剩余
工时信息
最初预计 0工时
总计消耗 7.5工时
预计剩余 0工时
预计开始 2022-04-28
实际开始 2022-05-27
截止日期 2022-05-05