I've heard of and encountered parameter sniffing in stored procedures before. But today I ran into the first instance of what I can only assume is parameter sniffing in a view.
I have a query that selects from a number of tables and a view. It's basically:
select *
from view v
inner join table t
on v.id = t.id
where v.date = @EndDate;
With the parameter in place, the query will attempt to execute literally for hours. I've let it sit for over 2 hours and nothing gets returned. If I replace the parameter with a specific date where v.date = '2017-11-20'
or even a calculated value where v.date = cast(getdate() as date)
it will return in 2 seconds. I can keep the parameter in by adding option (recompile)
to the end of the query and it will return results in a second or two.
So I know how to fix this, I am really just asking: Is this something that others have run into before? Is it possible it's not parameter sniffing but rather something else?
Thanks.
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `table` ADD INDEX `table_idx_id` (`id`);
ALTER TABLE `view` ADD INDEX `view_idx_date` (`date`);
SELECT
*
FROM
view v
INNER JOIN
table t
ON v.id = t.id
WHERE
v.date = @EndDate