1.问题分析
在ERP日志表里,慢sql top 2, 70秒--600秒,测试8分钟查不处理;
select count(0) from RB_OrderInfo nolock -- 46116303
select count(0) from RB_OrderList nolock -- 59512660
2.解决方案
将大量的运行在数据库运算,转移到应用程序计算
1.先从数据库查询出基础数据;
2.在应用程序过滤
优化后的sql;
SELECT *
FROM (SELECT Row_number()
OVER (
ORDER BY PayTime DESC) AS [PageIndex],
*
FROM (SELECT oi.ID,
oi.OrderCode,
oi.ClientOrderCode,
oi.MainOrderCode,
oi.MergeOrderCode,
oi.SalesRecordNumber,
oi.OrderSourceID,
oi.ClientUserAccount,
oi.OrderStatus,
oi.OrderState,
oi.IsFirstDeliver,
( CASE
WHEN oi.IsPriority > 0 THEN oi.IsPriority
ELSE Isnull(os.PriorityNum, 0)
END ) AS IsPriority,
oi.PackagedIDs,
oi.IsASingleInTheEnd,
oi.PayerId,
oi.Email,
oi.SellerOrderId,
oi.IsPay,
oi.CommentType,
oi.TransactionId,
oi.PaymentType,
Isnull(oi.RefundPrice, 0) AS RefundPrice,
Isnull(oi.ThrowingRate, 0) AS ThrowingRate,
oi.TotalPrice,
oi.ExchangeRMB,
oi.ExchangeUSD,
oi.TransportPay,
oi.OtherPayPrice,
oi.ProductTotalPrice,
oi.PromotionDiscountAmount,
oi.CouponCode,
oi.CouponMinusPrice,
oi.DiscountMinusPrice,
oi.AddPayPrice,
oi.AddOrderReasonType,
oi.ShareType,
oi.ShareCustomerID,
oi.IsAccount,
oi.AccountTime,
oi.ShareTotalPrice,
oi.SynchronousTime,
oi.IsSynchronous,
oi.IsTogeInvoice,
Isnull(oi.Encrypted, 0) AS Encrypted,
oi.FirstName,
oi.LastName,
oi.Address,
oi.District,
oi.CabinetNO,
oi.City,
oi.Province,
oi.Country,
oi.PostCode,
oi.Telephone,
oi.ShippingService,
oi.DownLoadUpdateTime,
oi.PromotionType,
oi.TrackNumberTemp,
oi.TrackNumbers,
oi.UpdatePlatformTime,
oi.MarkDeliveryState,
oi.EMPSDeliveryState,
oi.LogisticsOrderNo,
oi.LogisticsOrderNo1,
oi.ShipTime,
oi.OrderDescription,
oi.ISsynchOverseas,
oi.ProductNumber,
oi.ProductVolume,
oi.ProductWeight,
oi.PayTime,
oi.AddTime,
oi.UpdateTime,
oi.LatestShipDate,
oi.ServiceAdminName,
oi.SKUNumber,
oi.LatestDeliveryDate,
oi.VipDiscount,
Isnull(oi.IsTaxFree, 0) IsTaxFree,
oi.Currency,
os.ServiceAdminName AS ServiceOSAdminName,
Isnull(oi.IsFBATransit, 0) IsFBATransit,
oi.WareHouseID,
wh.WareHouseName,
os.OrderSourceName,
os.OrderSourceType,
os.OrderSourceCountry,
e.Country_cn,
t.TransportName,
t.PMSTransport,
tapi.Transport_API_Class,
wh.WareHouseType,
oi.Token,
os.SaleAdminName,
os.VATNumber,
os.VATRate,
oi.SaveTrackNumbersTime,
t.TrackingNumberValidDays,
Isnull(t.TrackNumbersIsNaturalDay, 1) AS TrackNumbersIsNaturalDay,
Datediff(day, Getdate(), ( Dateadd(day, TrackingNumberValidDays, SaveTrackNumbersTime) )) AS TrackNumbersDiffDay,
bt.BusinessTeamName,
t.ID AS TransportID,
e.ID AS CountryID,
oi.Oaid,
api.CertIDAccessKeyId,
Isnull(cexpand.BillPdfUrl, '') BillPdfUrl,
att1.IsAttachMail AS IsAttachMail
FROM RB_OrderInfo oi WITH(nolock)
LEFT JOIN RB_OrderSource os WITH(nolock)
ON oi.CustomerID = os.CustomerID
AND oi.OrderSourceID = os.ID
LEFT JOIN E_Country e WITH(nolock)
ON oi.Country = e.Shorthand
LEFT JOIN RB_Transport t WITH(nolock)
ON oi.CustomerID = t.CustomerID
AND oi.TransportID = t.ID
LEFT JOIN RB_Transport_API tapi WITH(nolock)
ON t.Transport_API_ID = tapi.ID
AND t.CustomerID = tapi.CustomerID
LEFT JOIN RB_WareHouse wh WITH(nolock)
ON oi.CustomerID = wh.CustomerID
AND oi.WareHouseID = wh.ID
LEFT JOIN RB_Admin rba WITH(nolock)
ON rba.CustomerID = os.CustomerID
AND rba.ID = os.SaleAdminID
LEFT JOIN RB_BusinessTeam bt WITH(nolock)
ON bt.CustomerID = os.CustomerID
AND bt.ID = rba.BusinessTeamID
LEFT JOIN RB_OrderSource_EbayAmazonAPI api WITH(nolock)
ON os.CustomerID = api.CustomerID
AND os.ID = api.OrderSourceID
LEFT JOIN RB_OrderInfo_ClientExpand (NOLOCK) cexpand
ON oi.CustomerID = cexpand.CustomerID
AND oi.OrderCode = cexpand.OrderCode
OUTER apply (SELECT Count(1) IsAttachMail
FROM RB_OrderInfo_Attach att(nolock)
WHERE att.CustomerID = oi.CustomerID
AND att.OrderCode = oi.OrderCode) att1
WHERE oi.CustomerID = 1
AND oi.IsStockOrder = 0
AND oi.IsPay = 1
AND oi.PayTime >= '2023/4/1 0:00:00'
AND oi.PayTime <= '2023/4/13 23:59:59'
--AND EXISTS (SELECT 1
-- FROM RB_OrderList ol WITH(nolock)
-- INNER JOIN RB_Product_Admin pa WITH(nolock)
-- ON ol.CustomerID = pa.CustomerID
-- AND ol.SKU = pa.SKU
-- WHERE ol.CustomerID = 1
-- AND ol.OrderCode = oi.OrderCode
-- AND pa.DevelopAdminID = 6323)
) AS TempTable) AS PagerTable
WHERE PagerTable.[PageIndex] BETWEEN 1 AND 500
3.sql
SELECT *
FROM (SELECT Row_number()
OVER (
ORDER BY PayTime DESC) AS [PageIndex],
*
FROM (SELECT oi.ID,
oi.OrderCode,
oi.ClientOrderCode,
oi.MainOrderCode,
oi.MergeOrderCode,
oi.SalesRecordNumber,
oi.OrderSourceID,
oi.ClientUserAccount,
oi.OrderStatus,
oi.OrderState,
oi.IsFirstDeliver,
( CASE
WHEN oi.IsPriority > 0 THEN oi.IsPriority
ELSE Isnull(os.PriorityNum, 0)
END ) AS IsPriority,
oi.PackagedIDs,
oi.IsASingleInTheEnd,
oi.PayerId,
oi.Email,
oi.SellerOrderId,
oi.IsPay,
oi.CommentType,
oi.TransactionId,
oi.PaymentType,
Isnull(oi.RefundPrice, 0) AS RefundPrice,
Isnull(oi.ThrowingRate, 0) AS ThrowingRate,
oi.TotalPrice,
oi.ExchangeRMB,
oi.ExchangeUSD,
oi.TransportPay,
oi.OtherPayPrice,
oi.ProductTotalPrice,
oi.PromotionDiscountAmount,
oi.CouponCode,
oi.CouponMinusPrice,
oi.DiscountMinusPrice,
oi.AddPayPrice,
oi.AddOrderReasonType,
oi.ShareType,
oi.ShareCustomerID,
oi.IsAccount,
oi.AccountTime,
oi.ShareTotalPrice,
oi.SynchronousTime,
oi.IsSynchronous,
oi.IsTogeInvoice,
Isnull(oi.Encrypted, 0) AS Encrypted,
oi.FirstName,
oi.LastName,
oi.Address,
oi.District,
oi.CabinetNO,
oi.City,
oi.Province,
oi.Country,
oi.PostCode,
oi.Telephone,
oi.ShippingService,
oi.DownLoadUpdateTime,
oi.PromotionType,
oi.TrackNumberTemp,
oi.TrackNumbers,
oi.UpdatePlatformTime,
oi.MarkDeliveryState,
oi.EMPSDeliveryState,
oi.LogisticsOrderNo,
oi.LogisticsOrderNo1,
oi.ShipTime,
oi.OrderDescription,
oi.ISsynchOverseas,
oi.ProductNumber,
oi.ProductVolume,
oi.ProductWeight,
oi.PayTime,
oi.AddTime,
oi.UpdateTime,
oi.LatestShipDate,
oi.ServiceAdminName,
oi.SKUNumber,
oi.LatestDeliveryDate,
oi.VipDiscount,
Isnull(oi.IsTaxFree, 0) IsTaxFree,
oi.Currency,
os.ServiceAdminName AS ServiceOSAdminName,
Isnull(oi.IsFBATransit, 0) IsFBATransit,
oi.WareHouseID,
wh.WareHouseName,
os.OrderSourceName,
os.OrderSourceType,
os.OrderSourceCountry,
e.Country_cn,
t.TransportName,
t.PMSTransport,
tapi.Transport_API_Class,
wh.WareHouseType,
oi.Token,
os.SaleAdminName,
os.VATNumber,
os.VATRate,
oi.SaveTrackNumbersTime,
t.TrackingNumberValidDays,
Isnull(t.TrackNumbersIsNaturalDay, 1) AS TrackNumbersIsNaturalDay,
Datediff(day, Getdate(), ( Dateadd(day, TrackingNumberValidDays, SaveTrackNumbersTime) )) AS TrackNumbersDiffDay,
bt.BusinessTeamName,
t.ID AS TransportID,
e.ID AS CountryID,
oi.Oaid,
api.CertIDAccessKeyId,
Isnull(cexpand.BillPdfUrl, '') BillPdfUrl,
att1.IsAttachMail AS IsAttachMail
FROM RB_OrderInfo oi WITH(nolock)
LEFT JOIN RB_OrderSource os WITH(nolock)
ON oi.CustomerID = os.CustomerID
AND oi.OrderSourceID = os.ID
LEFT JOIN E_Country e WITH(nolock)
ON oi.Country = e.Shorthand
LEFT JOIN RB_Transport t WITH(nolock)
ON oi.CustomerID = t.CustomerID
AND oi.TransportID = t.ID
LEFT JOIN RB_Transport_API tapi WITH(nolock)
ON t.Transport_API_ID = tapi.ID
AND t.CustomerID = tapi.CustomerID
LEFT JOIN RB_WareHouse wh WITH(nolock)
ON oi.CustomerID = wh.CustomerID
AND oi.WareHouseID = wh.ID
LEFT JOIN RB_Admin rba WITH(nolock)
ON rba.CustomerID = os.CustomerID
AND rba.ID = os.SaleAdminID
LEFT JOIN RB_BusinessTeam bt WITH(nolock)
ON bt.CustomerID = os.CustomerID
AND bt.ID = rba.BusinessTeamID
LEFT JOIN RB_OrderSource_EbayAmazonAPI api WITH(nolock)
ON os.CustomerID = api.CustomerID
AND os.ID = api.OrderSourceID
LEFT JOIN RB_OrderInfo_ClientExpand (NOLOCK) cexpand
ON oi.CustomerID = cexpand.CustomerID
AND oi.OrderCode = cexpand.OrderCode
OUTER apply (SELECT Count(1) IsAttachMail
FROM RB_OrderInfo_Attach att(nolock)
WHERE att.CustomerID = oi.CustomerID
AND att.OrderCode = oi.OrderCode) att1
WHERE oi.CustomerID = 1
AND oi.IsStockOrder = 0
AND oi.IsPay = 1
AND oi.PayTime >= '2023/4/1 0:00:00'
AND oi.PayTime <= '2023/4/13 23:59:59'
AND EXISTS (SELECT 1
FROM RB_OrderList ol WITH(nolock)
INNER JOIN RB_Product_Admin pa WITH(nolock)
ON ol.CustomerID = pa.CustomerID
AND ol.SKU = pa.SKU
WHERE ol.CustomerID = 1
AND ol.OrderCode = oi.OrderCode
AND pa.DevelopAdminID = 6323)) AS TempTable) AS PagerTable
WHERE PagerTable.[PageIndex] BETWEEN 1 AND 500
OPTION(recompile, maxdop 12)