任务描述

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'

历史记录  
  1. 2022-05-04 11:50:51, 由 陈少军 创建。
  2. 2022-05-04 11:51:46, 由 陈少军 指派给 罗友军。
    修改了 预计剩余,旧值为 "0",新值为 "2"。


    参考解决方案 修改

  3. 2022-05-04 11:54:22, 由 罗友军 指派给 huangtiekun。
    修改了 指派给,旧值为 "luoyoujun",新值为 "huangtiekun"。
  4. 2022-05-09 20:32:19, 由 huangtiekun 完成。
    修改了 总计消耗,旧值为 "0",新值为 "4"。
    修改了 实际完成,旧值为 "",新值为 "2022-05-09 20:26:12"。
    修改了 任务状态,旧值为 "wait",新值为 "done"。
    修改了 实际开始,旧值为 "0000-00-00 00:00:00",新值为 "2022-05-09 20:32:19"。
    修改了 预计剩余,旧值为 "2",新值为 "0"。
    修改了 由谁完成,旧值为 "",新值为 "huangtiekun"。

    堡垒机测试修改后的SQL摘出来,效果如下:


    将分页与总条数分开查:

  5. 2022-07-16 17:09:17, 由 zhengqiao 关闭。
    修改了 任务状态,旧值为 "done",新值为 "closed"。
    修改了 指派给 ,旧值为 "huangtiekun",新值为 "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-05-04 11:50:51
由谁完成 huangtiekun 于 2022-05-09 20:26:12
由谁取消 暂无
由谁关闭 zhengqiao 于 2022-07-16 17:09:17
关闭原因 已完成
最后编辑 zhengqiao 于 2022-07-16 17:09:17
团队 最初预计 总计消耗 预计剩余
工时信息
最初预计 0工时
总计消耗 4工时
预计剩余 0工时
预计开始 2022-05-04
实际开始 2022-05-09
截止日期 2022-05-11