我的查询具有以下结构:
SELECT DISTINCT (CO.CateringOrderId),
CO.CateringOrderNumber,
MC.FirstName + ' ' + MC.LastName AS "CustomerName",
CO.EventDate AS EventDate,
CO.IsCompleted,
CO.IsVerified,
MC.EmailId,
CAT.OfficePhone,
CAT.Mobile,
CAT.Fax,
CO.TotalInvoiceAmount,
CO.BarterCharityId,
(SELECT Sum (Amount)
FROM Catering_Order_Payment_Trans
WHERE CateringOrderId = CO.CateringOrderId) AS AmountReceived
FROM Catering_Orders CO,
Master_Customer MC,
Customer_Address_Trans CAT,
Catering_Order_Employee_Trans COET
WHERE MC.CompanyId = @p_CompanyId
AND (MC.CustomerId = @p_CustomerId OR @p_CustomerId = -1)
AND (CO.CateringOrderNumber LIKE '%' + @p_CateringOrderNumber + '%')
AND (CO.EventDate >= CONVERT (DATETIME, @p_FromDate) OR @p_FromDate = '')
AND (CO.EventDate <= CONVERT (DATETIME, @p_ToDate) OR @p_ToDate = '')
AND (CO.IsCompleted = @p_IsCompleted OR @p_IsCompleted = -1)
AND (COET.EmployeeId = @p_CatererId OR @p_CatererId = -1)
AND MC.CustomerId = CO.CustomerId
AND MC.PersonalAddressId = CAT.CustomerAddressId
AND (COET.CateringOrderId = CO.CateringOrderId
OR CO.CateringOrderId NOT IN
(SELECT CateringOrderId FROM Catering_Order_Employee_Trans))
AND (CAT.Mobile like '%' + @p_ContactNumber + '%' )
AND (CO.IsActive is null or CO.IsActive=1)
ORDER BY CO.CateringOrderId DESC
我认为SUM
子查询正在减慢它。请建议我如何加快速度。
目前它的执行时间大约是7-10秒。
答案 0 :(得分:1)
尝试这样的事情 -
SELECT DISTINCT
CO.CateringOrderId,
CO.CateringOrderNumber,
MC.FirstName + ' ' + MC.LastName AS CustomerName,
CO.EventDate AS EventDate,
CO.IsCompleted,
CO.IsVerified,
MC.EmailId,
CAT.OfficePhone,
CAT.Mobile,
CAT.Fax,
CO.TotalInvoiceAmount,
CO.BarterCharityId,
AmountReceived = (
SELECT SUM(t.Amount)
FROM dbo.Catering_Order_Payment_Trans t
WHERE t.CateringOrderId = CO.CateringOrderId
)
FROM (
SELECT *
FROM dbo.Catering_Orders
WHERE ISNULL(IsActive, 1) = 1
AND (IsCompleted = @p_IsCompleted OR @p_IsCompleted = -1)
AND CateringOrderNumber LIKE '%' + @p_CateringOrderNumber + '%'
AND EventDate BETWEEN
CONVERT(DATETIME, ISNULL(NULLIF(@p_FromDate, ''), '18000101'))
AND
CONVERT(DATETIME, ISNULL(NULLIF(@p_ToDate, ''), '30000101'))
) CO
JOIN dbo.Master_Customer MC ON MC.CustomerId = CO.CustomerId
JOIN dbo.Customer_Address_Trans CAT ON MC.PersonalAddressId = CAT.CustomerAddressId
LEFT JOIN (
SELECT *
FROM dbo.Catering_Order_Employee_Trans
WHERE EmployeeId = @p_CatererId
OR @p_CatererId = -1
) COET ON COET.CateringOrderId = CO.CateringOrderId
WHERE MC.CompanyId = @p_CompanyId
AND (MC.CustomerId = @p_CustomerId OR @p_CustomerId = -1)
AND CAT.Mobile LIKE '%' + @p_ContactNumber + '%'
中的主要问题
AND (COET.CateringOrderId = CO.CateringOrderId
OR CO.CateringOrderId NOT IN
(SELECT CateringOrderId FROM Catering_Order_Employee_Trans))
和
(SELECT Sum (Amount)
FROM Catering_Order_Payment_Trans
WHERE CateringOrderId = CO.CateringOrderId) AS AmountReceived
答案 1 :(得分:1)
尝试:
SELECT CO.CateringOrderId,
CO.CateringOrderNumber,
MC.FirstName + ' ' + MC.LastName AS "CustomerName",
CO.EventDate AS EventDate,
CO.IsCompleted,
CO.IsVerified,
MC.EmailId,
CAT.OfficePhone,
CAT.Mobile,
CAT.Fax,
CO.TotalInvoiceAmount,
CO.BarterCharityId,
COPT.AmountReceived
FROM Catering_Orders CO
JOIN Master_Customer MC ON MC.CustomerId = CO.CustomerId
JOIN Customer_Address_Trans CAT ON MC.PersonalAddressId = CAT.CustomerAddressId
LEFT JOIN (SELECT CateringOrderId, Sum(Amount) AS AmountReceived
FROM Catering_Order_Payment_Trans
GROUP BY CateringOrderId) COPT
ON COPT.CateringOrderId = CO.CateringOrderId
WHERE MC.CompanyId = @p_CompanyId
AND (MC.CustomerId = @p_CustomerId OR @p_CustomerId = -1)
AND (CO.CateringOrderNumber LIKE '%' + @p_CateringOrderNumber + '%')
AND (CO.EventDate >= CONVERT (DATETIME, @p_FromDate) OR @p_FromDate = '')
AND (CO.EventDate <= CONVERT (DATETIME, @p_ToDate) OR @p_ToDate = '')
AND (CO.IsCompleted = @p_IsCompleted OR @p_IsCompleted = -1)
AND EXISTS
(SELECT NULL
FROM Catering_Order_Employee_Trans COET
WHERE COET.CateringOrderId = CO.CateringOrderId AND
(COET.EmployeeId = @p_CatererId OR @p_CatererId = -1) )
AND (CAT.Mobile like '%' + @p_ContactNumber + '%' )
AND (CO.IsActive is null or CO.IsActive=1)
ORDER BY CO.CateringOrderId DESC
答案 2 :(得分:0)
要优化查询,您应该编辑插入内部的OR条件和条件,这会严重降低查询速度
AND ( CO.EventDate <= CONVERT (DATETIME, @p_ToDate)
OR @p_ToDate = '')
应该变得像
AND ( CO.EventDate <= CASE WHEN @p_ToDate = '' THEN
GETDATE()
ELSE
CONVERT (DATETIME,@p_ToDate)
END)
还尝试逐个删除innested select in select和where where to see which which which which which which which which which which which which