I have sql queries with some IN clauses. To improve query plan caching, I decided to use table parameters. Here is an example of WHERE ID IN (SELECT ID FROM @P1) . @ P1 is a variable of the following type:
CREATE TYPE [dbo].[Ids] AS TABLE( [ID] [int] NOT NULL, PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (IGNORE_DUP_KEY = OFF) )
But I notice that some queries are getting slower. Request example:
select * from SomeTable s where ((s.SomeForeignId in (select id from @p1)) or s.SomeForeignId is null)
which runs in 2.1 seconds on my db.
And the old request:
select * from SomeTable s where ((s.SomeForeignId in (1,2,3.....)) or s.SomeForeignId is null)
performed in 1.8 seconds.
I notice a difference in terms of requests. The plan for the first request consists of two parts (one for zero verification, one for the proposal), and then concatenation. Although the background is just an index search.
Is there a way to improve my parameterized query to execute faster?
PS This is just a sample of a distilled request, I want to know that something is wrong with this part in (select id from @p1) .
source share