我的查询具有以下结构:
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 秒。