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”:


From MS: