We have the following code:
select * from View1 where (Timestamp >= @x) and (SomeCode like 'ABC%')
It runs terribly slow. But the code
select * from View1 where (Timestamp >= @x) (*)
runs pretty fast. Also SomeCode like... filter runs pretty fast on the previous (*) code. So, it’s fast when two-phased. (View1 is a CLR computed view.)
Question: how to advice SQL Server 2008 R2 to make the query (more precisely, the two filters) in two phases, that is, first the Timestamp filter and then the SomeCode filter.
Note: Nesting the queries doesn’t work for us, it’s slow as well.
There are probably better ways, but this one works for me:
This forces the fast query to be run first.
Responding to the comments:
Maybe “force” isn’t the right word, but I’ve definitely observed it making a difference. I’ll revise this if someone has a better suggestion.
Responding to your comment:
“Doesn’t work” = still slow?
In which case: is the Timestamp column indexed? Is SQL Server running short on resources (memory, disk space) with the larger query? Have you looked at the execution plan?
What happens if you really force the issue: