0

我有一个性能很差的 Sql Ad-hoc 查询。请帮助我或给我一些优化它的建议。这是下面的查询:

SELECT TOP 20 CustomerPrimaryExtID,
              Max(POSTimeStamp)     AS TransactionDate,
              ExtLocationCode,
              0                     AS RedemptionAmount,
              0                     AS RedemptionCount,
              TerminalNum,
              LogixTransNum,
              POSTransNum           AS TransNum,
              0                     AS DetailRecords,
              CustomerTypeID,
              PresentedCustomerID,
              PresentedCardTypeID,
              HHID,
              Replayed,
              0                     AS TransContext,
              isnull(TransTotal, 0) AS TransTotal
FROM   TransHist AS TH WITH(nolock)
WHERE  ( ( ( CustomerPrimaryExtID IN ( '' )
             AND HHID IS NULL )
            OR HHID = '0000000250000013408'
               AND CustomerTypeID <> 1 )
          OR ( CustomerPrimaryExtID = '0000000250000013408'
               AND CustomerTypeID = 1 ) )
       AND NOT EXISTS (SELECT LogixTransNum
                       FROM   TransRedemptionView AS TR2
                       WHERE  ( ( ( CustomerPrimaryExtID IN ( '' )
                                    AND HHID IS NULL )
                                   OR HHID = '0000000250000013408'
                                      AND CustomerTypeID <> 1 )
                                 OR ( CustomerPrimaryExtID = '0000000250000013408'
                                      AND CustomerTypeID = 1 ) )
                              AND TH.LogixTransNum = TR2.LogixTransNum)
GROUP  BY CustomerPrimaryExtID,
          HHID,
          CustomerTypeID,
          PresentedCustomerID,
          PresentedCardTypeID,
          LogixTransNum,
          POSTransNum,
          TerminalNum,
          ExtLocationCode,
          Replayed,
          TransTotal
ORDER  BY TransactionDate DESC 

在此处输入图像描述

4

1 回答 1

0

鉴于 TransRedemption 的 62% 成本,我认为 CustomerPrimaryExtID、HHID、CustomerTypeID 和 LogixTransNum 的索引会有所帮助。

你也可以看看做:

FROM 
    TransHist TH 
LEFT OUTER JOIN
    TransRedemption TR2
    ON TH.LogixTransNum = TR2.LogixTransNum

不要在子查询或连接中使用视图,除非在该视图中执行某些操作来限制从 TransRedemption 返回的行数。

然后删除:

   AND NOT EXISTS (SELECT LogixTransNum
                   FROM   TransRedemptionView AS TR2
                   WHERE  ( ( ( CustomerPrimaryExtID IN ( '' )
                                AND HHID IS NULL )
                               OR HHID = '0000000250000013408'
                                  AND CustomerTypeID <> 1 )
                             OR ( CustomerPrimaryExtID = '0000000250000013408'
                                  AND CustomerTypeID = 1 ) )
                          AND TH.LogixTransNum = TR2.LogixTransNum)

从 where 子句替换为

AND TR2.LogixTransNum IS NULL

如果 TransRedemption 有很多列,那么您可能希望像这样限制连接中的结果:

FROM 
    TransHist TH 
LEFT OUTER JOIN
    (
        SELECT 
            LogixTransNum
        FROM
            TransRedemption
    ) TR2
    ON TH.LogixTransNum = TR2.LogixTransNum
于 2013-08-30T04:43:29.190 回答