How to avoid Table Spool and Hash Match.
Is there any way I can optimize this query?
- Update your statistics, especially on dbo.PropVal
- Consider a nonclustered index on dbo.PropVal (Item, Value, Symbol)
- Review whether the Value column really needs to be a LOB type (e.g. nvarchar(max))
- Stop sprinkling NOLOCK hints everywhere and be sure you understand the risks
- Consider a computed column on dbo.Crons SUBSTRING([CronID],1,CHARINDEX('.',[CronID])-1)
- Create a nonclustered index on that computed column, [Date] and [Value].
- Correct the mismatched data types in the query, for example on expressions comparing [Value], [Symbol], and [Date]. Pay particular attention to Unicode strings and date/time types.
It is difficult to solve all the issues in that query in one step based on a single execution plan, but hopefully those ideas will lead you in the right direction. You may also find it helpful to break this query into smaller parts while you are tuning it – even if you subsequently recombine the blocks into a single monolithic query. Finding problems and addressing them is always easier when the problem is smaller 🙂
- Statistics are up-to-date. They are updated daily.
- There is a nonclustered index on dbo.ProbVal (Item, Symbol). The Value column is nvarchar(max).
- Yes the Value column is nvarchar(max).
- I will make sure I remove them before I deploy it to production. This was only for test.
- Created a computed column on dbo.Crons SUBSTRING([CronID],1,CHARINDEX('.',[CronID])-1)
- Created nonclustered index on the computed column and Date, in this order. The dbo.Crons table does not have Value column, which is on the dbo.PropVal table. After all these corrections, the performance went down from 3.5 minutes to 3 seconds.
- I am trying to correct the mismatch data types..
After all these steps, the performance went down from 3.5 minutes to 3 seconds. I know the impact of the "WITH (NOLOCK)", which gets uncommitted transaction from cache (dirty pages)… I was testing the script and posted the script without removing the table hints.
Thank you so much once again Paul.