I am a software developer and I was recently approached by DBA to optimize the query that an app of mine is using. DBA reported that query takes about 50% of CPU and high I/O operations when it runs. The query is pretty straight forward and I am unsure how to optimize it.
Question 1: How can I optimize this query?
Question 2: is it even my job to do so, shouldn't DBA be more knowledgeable in this? Mind you we have no DB developers, just DBA and Software Developers.
DB has approximately 30-50 million of records, it is constantly maintained/monitored by DBA, but I am unsure how. Server is on a dedicated machine and is Microsoft SQL Server 2005 - 9.00.5057.00 (X64)
PS: Please do not provide ways to improve DB by structural changes, I know it's a bad design to have currency stored as varchar, but it is what it is, we can't change DB structure, only queries accessing it.
Thank you for any insight.
Query:
SELECT
COALESCE(CAST([PH].[PAmount] AS decimal(15, 2)) + CAST([PH].[Fee] AS decimal(15, 2)), 0.0) AS [PayAmount],
[PH].[PDate] AS [PayDate]
FROM [History] AS [PH] WITH (NOLOCK)
WHERE [PH].[PMode] IN ('C', 'P')
AND [PH].[INNO] = 'XYZ'
AND [PH].[PStatus] IN ('CONSERVED', 'EXPECTING', 'REFRIGERATED', 'POSTPONED', 'FILED')
AND [PH].[Locked] = 1
AND [PH].[PDate] >= 'Jan 1, 2015'
ORDER BY [PH].[PDate] ASC
Fields:
PAmount
- non-clustered index, varchar(50)
Fee
- not indexed, decimal(6,2)
PDate
- clustered index, datetime
PMode
- non-clustered index, varchar(5)
INNO
- non-clustered index, varchar(50)
PStatus
- non-clustered index, varchar(50)
Locked
- not indexed, bit
Execution plan:
SELECT---Compute Scalar---Filter---NestedLoops-|--Index Seek
(Inner Join) |
cost 0% Cost 0% Cost 0% Cost 0% | cost 4%
|---Key Lookup
Cost 96%