I faced this problem twice with SQL Azure and two different queries so far.
Something like this:
select top(100) * from SomeView WHERE AccountId='actual-guid-typed-in-here'
runs super fast but equivalent code (generated from Linq-to-SQL)
declare @accountId uniqueidentifier; set @accountId = 'actual-guid-typed-in-here' select top(100) * from SomeView WHERE AccountId=@accountId
runs super slow and upon careful analysis I see that the engine uses the wrong index in the latter case. Note that the latter case would also arise if I moved my code into a stored procedure.
The two queries are identical and so I expect them to be executed identically but the database engine thinks otherwise.
What's the typical solution from this case?