0

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

2 回答 2

1
select 
    CustomerPK, 
    LocalID, 
    ExternalID, 
    EarnedDate, 
    QtyEarned, 
    QtyUsed, 
    Value, 
    ServerSerial, 
    LastLocationID,
    SVS.Description as Status, 
    SVS.PhraseID as StatusPhraseID,
    IsNull(SUM(QtyEarned) OVER(PARTITION BY CustomerPK,LocalID,ServerSerial),0) -
      IsNull(SUM(QtyUsed) OVER(PARTITION BY CustomerPK,LocalID,ServerSerial),0) 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;
于 2013-07-01T19:06:49.557 回答
0
select 
    CustomerPK, 
    LocalID, 
    ExternalID, 
    EarnedDate, 
    QtyEarned, 
    QtyUsed, 
    Value, 
    ServerSerial, 
    LastLocationID,
    SVS.Description as Status, 
    SVS.PhraseID as StatusPhraseID,
    netT.net,
    AdminUserID, 
    ExpireDate, 
    PresentedCustomerID, 
    PresentedCardTypeID, 
    ResolvedCustomerID, 
    HHID, 
    Replayed, 
    ReplayedDate
from 
        SVHistory SVH with (NoLock) 
inner join 
        StoredValueStatus SVS with (NoLock) on SVS.StatusID=SVH.StatusFlag
inner merge join  
   ( select LocalID, ServerSerial, SUM(IsNull(QtyEarned,0)) - SUM(IsNull(QtyUsed,0)) as net
     from SVHistory 
     where CustomerPK=18653237 
     group by LocalID, ServerSerial
   ) netT
  on netT.LocalID = SVH.LocalID 
 and netT.ServerSerial = SVH.ServerSerial
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
于 2013-07-01T21:37:35.537 回答