我有一个执行不良的 sql 查询,它导致表扫描/ IO 峰值。请检查以下脚本和执行计划,如果它需要任何索引或查询本身的重构,请告诉我。谢谢
- 询问
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 = '0000000250000007320' AND CustomerTypeID <> 1
) OR ( CustomerPrimaryExtID = '0000000250000007320' AND CustomerTypeID = 1 )
)
AND NOT EXISTS
(
SELECT
LogixTransNum
FROM TransRedemptionView AS TR2
WHERE
(
( ( CustomerPrimaryExtID IN ( '' ) AND HHID IS NULL ) OR HHID = '0000000250000007320' AND CustomerTypeID <> 1 )
OR ( CustomerPrimaryExtID = '0000000250000007320' 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