SQL query much slower running through app

I’ve run into an old problem – I have a stored proc that takes around 82 seconds to run through my web app.  However when I run it through SSMS it returns in about 3 seconds.  After poking around for a while it appears to be caused by SQL server parameter sniffing.  That’s where SQL server caches optimized execution plans based on parameter values – sometimes these plans can be bad…  So to fix it you specify “WITH RECOMPILE”:

https://www.simple-talk.com/sql/t-sql-programming/parameter-sniffing/?utm_source=simpletalk&utm_medium=email-main&utm_content=Sniffing-201010116&utm_campaign=SQL

From MS:
http://technet.microsoft.com/en-us/library/ms190439.aspx

Advertisements