Hello,
In my application, I have a query which takes a large number of items in a 'in' clause:
select [columns]
from
where
.[id] in (1,2,3,4,.........,1000).
These queries are run through a .net application which uses ado and a native sql connection. The number of items in the 'in' list change depending on the user that uses the application. It can be a 1000 items, it could only be 1 or 2...
I am in the progress of changing such large 'in' lists to table variables, this to use parametrized queries and to restrict the amount of query cache used by our database servers. The query now looks like this and works:
select [columns]
from
where
.[id] in (select id from [@table_var])
However, a sql server specialist gave me the tip that I should use 'option OPTIMIZE FOR UNKNOWN' to prevent bad query plan caching if the first occurrence of the query only has one items and all subsequent queries have 1000.
i have been trying to use the optimize hint only for the table variable with 'option (OPTIMIZE FOR (@table_var UNKNOWN))' , but I get an error "Must declare the scalar variable @table_var". I can use the optimize hint for other (non table) variables just as fine. I can use the general 'option (OPTIMIZE FOR UNKNOWN)' without errors, but I don't know if my table variable is also covered, since the previous syntax failed.
Can the optimizer handle different sizes of the table variable within a same query plan another way?
Thanks in advance for the responses,
Kind greetings
SqlMim
In my application, I have a query which takes a large number of items in a 'in' clause:
select [columns]
from
These queries are run through a .net application which uses ado and a native sql connection. The number of items in the 'in' list change depending on the user that uses the application. It can be a 1000 items, it could only be 1 or 2...
I am in the progress of changing such large 'in' lists to table variables, this to use parametrized queries and to restrict the amount of query cache used by our database servers. The query now looks like this and works:
select [columns]
from
However, a sql server specialist gave me the tip that I should use 'option OPTIMIZE FOR UNKNOWN' to prevent bad query plan caching if the first occurrence of the query only has one items and all subsequent queries have 1000.
i have been trying to use the optimize hint only for the table variable with 'option (OPTIMIZE FOR (@table_var UNKNOWN))' , but I get an error "Must declare the scalar variable @table_var". I can use the optimize hint for other (non table) variables just as fine. I can use the general 'option (OPTIMIZE FOR UNKNOWN)' without errors, but I don't know if my table variable is also covered, since the previous syntax failed.
Can the optimizer handle different sizes of the table variable within a same query plan another way?
Thanks in advance for the responses,
Kind greetings
SqlMim