I have a query that is a little complex from another Department but let me simplify it as Q1:
select * from Table1 where UserID = 'x'
then I run Q2:
select * from Table1 where UserID = 'y'
Q1 runs slow on a clean cache and then fast on subsequent runs. Than I try Q2 without clearing the cache and it runs slow as well on the first run and fast on subsequent runs.
I first thought that the issue was bringing data into the cache but with SET STATISTICS TIME ON, it seems that it is compile time which is taking longer. Then I checked in dm_exec_query_stats and I see SQL server made a separate plan for Q1 and Q2.
Is there a way to force SQL Server to use the same query plan for Q1 and Q2? With a stored procedure it would since the UserIDs, 'x' or 'y' would be parameters. But this department said because of the tool they are using, they can't use Stored Procedures.