I have a view that returns 2 ints from a table using a CTE. If I query the view like this it runs in less than a second
SELECT * FROM view1 WHERE ID = 1
In my case in DB table column type was defined as VarChar and in parameterized query parameter type was defined as NVarChar, this introduced CONVERT_IMPLICIT
in the actual execution plan to match data type before comparing and that was culprit for sow performance, 2 sec vs 11 sec. Just correcting parameter type made parameterized query as fast as non parameterized version.
Hope this may help someone with similar issue.
DECLARE @id INT = 1
SELECT * FROM View1 WHERE ID = @id
Do this
DECLARE @sql varchar(max)
SET @sql = 'SELECT * FROM View1 WHERE ID =' + CAST(@id as varchar)
EXEC (@sql)
Solves your problem