我有一个性能很差的 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