1. 问题描述,分析
count 查询,查询了大量字段,导致查询慢
2.解决方案
去掉count 查询里不必要的字段查询,测试查询从200秒到优化后的19秒查询出结果
优化后的sql
SELECT Count(1) FROM
( SELECT
--a.ListingProductID AS Id
a.CustomerID AS CustomerId
--b.SiteCode AS SiteCode,
--c.OrderSourceCountry AS Country,
--b.OrderSourceId
--c.OrderSourceName,
--a.OrderSourceSKU AS OrderSourceSku,
--a.SKU AS Sku
--b.ProductTitle AS Title,
--(SELECT Stuff((SELECT '',''+AttrValue FROM Rb_ebay_listing_attribute_v2 (nolock) d
-- WHERE (d.AttrValue <>'''' OR d.AttrValue IS NOT NULL) AND d.ListingBasicID= a.ListingBasicID
-- AND d.OrderSourceID=a.OrderSourceID AND d.CustomerID=a.CustomerID
-- FOR xml path('''')),1,1,'''')) AS Attributes,
--b.ProductDetails AS [Description],
--b.OnlineState AS Status,
--b.UpdateTime
FROM RB_Ebay_Listing_Product_V2 a WITH(nolock)
JOIN RB_Ebay_Listing_Basic_V2 b WITH(nolock) ON a.CustomerID=b.CustomerID AND a.OrderSourceID=b.OrderSourceID AND a.ListingBasicID=b.ListingBasicID
LEFT JOIN RB_OrderSource c WITH(nolock) ON a.OrderSourceID=c.Id
WHERE c.OrderSourceType=2 AND a.CustomerID=1 AND b.UpdateTime >= '2022-02-21 22:15:46.460' AND b.UpdateTime < '2022-02-22 22:15:46.460'
)
AS PagerTable
-- CustomerID int,@UpdateBeginTime datetime,@UpdateEndTime datetime',@CustomerID=1,@UpdateBeginTime='2022-02-21 22:15:46.460',@UpdateEndTime='2022-02-22 22:15:46.460
--select count(0) from RB_Ebay_Listing_Product_V2 nolock --68486907
--select count(0) from RB_Ebay_Listing_Basic_V2 nolock --59815688
--select count(0) from RB_OrderSource nolock --10206
3.原始sql:
exec sp_executesql N'SELECT COUNT(1) FROM ( select
a.ListingProductID as Id,
a.CustomerID as CustomerId,
b.SiteCode AS SiteCode,
c.OrderSourceCountry AS Country,
b.OrderSourceId,
c.OrderSourceName,
a.OrderSourceSKU as OrderSourceSku,
a.SKU as Sku,
b.ProductTitle as Title,
(select stuff((select '',''+AttrValue from RB_Ebay_Listing_Attribute_V2 (nolock) d
where (d.AttrValue <>'''' or d.AttrValue IS NOT NULL) AND d.ListingBasicID= a.ListingBasicID
AND d.OrderSourceID=a.OrderSourceID and d.CustomerID=a.CustomerID
for xml path('''')),1,1,'''')) as Attributes,
b.ProductDetails AS [Description],
b.OnlineState AS Status,
b.UpdateTime
from RB_Ebay_Listing_Product_V2 a with(nolock)
join RB_Ebay_Listing_Basic_V2 b with(nolock) on a.CustomerID=b.CustomerID and a.OrderSourceID=b.OrderSourceID and a.ListingBasicID=b.ListingBasicID
left join RB_OrderSource c with(nolock) on a.OrderSourceID=c.Id
where c.OrderSourceType=2
and a.CustomerID=@CustomerID and b.UpdateTime >= @UpdateBeginTime AND b.UpdateTime < @UpdateEndTime
) AS PagerTable',N'@CustomerID int,@UpdateBeginTime datetime,@UpdateEndTime datetime',@CustomerID=1,@UpdateBeginTime='2022-02-21 22:15:46.460',@UpdateEndTime='2022-02-22 22:15:46.460'





参考解决方案 修改