We have this query to work out total amount withdrawn in the last 24 hours by a customer.
SELECT
SUM(Amount) SumWithdrawalAmount
FROM dbo.PaymentRequest pr WITH (READUNCOMMITTED)
WHERE
pr.PaymentRequestTypeId = 0 --Withdrawal
AND pr.CreateDate >= GETDATE()-1 --Last 24h
AND pr.PaymentAccountId = 'xxxxxxx-FD54-ED11-80F6-xxxxxxxxxxx'
SELECT
SUM(Amount) SumWithdrawalAmount
FROM dbo.PaymentRequest pr WITH (READUNCOMMITTED)
WHERE
pr.PaymentRequestTypeId = 0 --Withdrawal
AND pr.CreateDate >= GETDATE()-1 --Last 24h
AND pr.PaymentAccountId = 'xcxcxcx-FD54-ED11-80F6-xxxxxxxx'
AND pr.PaymentMethodExternalSystemId IN (1, 2, 3)
Suggestions on improving it ?
Query plan is attached.
SELECT
SUM(Amount) SumWithdrawalAmount
FROM
dbo.PaymentRequest pr WITH (READUNCOMMITTED)
WHERE
pr.PaymentRequestTypeId = 0 --Withdrawal
AND
pr.CreateDate >= GETDATE()-1 --Last 24h
AND
pr.PaymentAccountId = 'xcxcxcx-FD54-ED11-80F6-xxxxxxxx'
AND
pr.PaymentMethodExternalSystemId IN (1, 2, 3)
Thanks for suggestions i should have mentioned that building a index on the table will be not possible.
The table is 439 million rows and over 2TB in size and causes too much contention as its highly available DB and table so cannot afford the downtime.
I was hoping that we could improve in some other way.
You need to adjust an index one way or another to get better performance. You need a "covering index" for this query.
You could also create a new version of the existing index with an INCLUDE of the column(s) it's looking up, and once the new index is complete drop the old one.