I've been battling a strange query performance issue. The query is sometimes running slowly from a website(2 - 4 seconds). I run a trace and it seems to be CPU overhead. The trace will show roughly the same amount of CPU time as duration. The thing I can't figure out is that when I copy/paste the query from the trace and run it, it immediately completes and the trace shows it taking roughly 300ms. I can do the same thing over-and-over. Slow on the website, fast in SSMS. I even converted the query into a view and the select is now just directly querying the view, but I'm seeing the same issue.
Another oddity I've noticed is that despite the fact that it's a very small resultset(70ish rows, 13 small columns), the time it takes to run the query correlates with the size of the resultset. So, if I shift a date parameter to return 20 rows rather than 70, my query time also reduces to about a third the time.
Here's another observation: If I take out the last column and add it back it starts working just fine. But, when I go away for the day and come back it runs slow again when I'm back in the office. This has happened multiple times.
I looked up a way to see the execution plan in a trace. It's definitely different when it's slow, but I can't tell why it's slow.
Any reason why it would consistently use a different execution plan for the same query?