I'm experiencing some strange behaviour in SQL Server 2008 R2.
I have the following query:
UPDATE TableToUpdate
SET ColumnToUpdate = @ColumnValue
WHERE ColumnA IN
(
SELECT ColumnA
FROM SubQ1Table
WHERE ColumnToUpdateReference = @ColumnValue
)
and ColumnB in
(
SELECT ColumnB
FROM SubQ2Table
WHERE ColumnToUpdateReference = @ColumnValue
)
When I run the query manually and declare the variable @ColumnValue as follows:
DECLARE @ColumnValue INT = 123;
It runs in seconds and does an Index Seek on the index covering columns ColumnA, ColumnB and ColumnToUpdate on the TableToUpdate table.
When I create a stored procedure using the exact query except the parameter is passed in.. eg:
EXEC sp_Query 123
An Index Scan is used on the same index and takes around 30 seconds to complete.
I've looked at both query plans which are different. Reading from left to right the first difference is the stored procedure seems to do a Nested Loops (Inner Join) Whereas the direct query does a Stream Aggregate (Aggregate).
Why would calling this SQL through a stored-procedure make a difference? If you need me to provide any more information let me know.
Thanks in advance,
Tom.