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)
任务描述
附件

