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,
               (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 秒。

4

3 回答 3

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
于 2013-06-25T10:27:26.720 回答
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
于 2013-06-25T10:36:09.863 回答
0

要优化您的查询,您应该编辑插入在 AND 条件中的 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)

还尝试在 select 和 where 中一一删除嵌套选择,以查看哪个最减慢查询

于 2013-06-25T10:18:05.267 回答