0

I have a SQL query which is performing badly. It is taking about 2 minutes to return the result set.

Is there a better way to rewrite the query? I know about CTE, but never used it before.

Please help.

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
  • 3
    We need to know that your tables and indexes look like. Commented Jul 1, 2013 at 18:59
  • 2
    You should look at the execution plan (and possibly post it here) - it'll tell you where you're losing your time.... Commented Jul 1, 2013 at 19:20
  • The is just strange. You are reporting individual QtyEarned QtyUsed and AND for each line a sum of aggregate difference. Commented Jul 1, 2013 at 19:33
  • It's not strange; it's just denormalized. Commented Jul 1, 2013 at 21:04

2 Answers 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;
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Spelling: "inner merger join"
@RBarryYoung I got inner right. That syntax was clearly by hand and not tested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.