我有一个性能很差的 SQL 查询。返回结果集大约需要 2 分钟。
有没有更好的方法来重写查询?我知道 CTE,但以前从未使用过。
请帮忙。
select
CustomerPK,
LocalID,
ExternalID,
EarnedDate,
QtyEarned,
QtyUsed,
Value,
ServerSerial,
LastLocationID,
SVS.Description as Status,
SVS.PhraseID as StatusPhraseID,
(select SUM(IsNull(QtyEarned,0)) - SUM(IsNull(QtyUsed,0))
from SVHistory SVH2 with (NoLock)
where CustomerPK=18653237 and SVH2.LocalID = SVH.LocalID and SVH2.ServerSerial=SVH.ServerSerial
) as QtyAvail,
AdminUserID,
ExpireDate,
PresentedCustomerID,
PresentedCardTypeID,
ResolvedCustomerID,
HHID,
Replayed,
ReplayedDate
from
SVHistory SVH with (NoLock)
inner join
StoredValueStatus SVS with (NoLock) on SVS.StatusID=SVH.StatusFlag
where
LastLocationID <> -8
and CustomerPK = 18653237
and SVProgramID = 112
and LastUpdate between '2013-05-27 00:00:00' and '2013-06-26 23:59:59'
and Deleted = 0
order by
EarnedDate DESC,
LocalID,
ExternalID,
Status;