Are there any obvious reasons why calling a stored procedure via my entity model would result in far slower performance than calling it direcly?
Firstly, I don't expect the SP to run at exactly the same speed and I know that there are various things that EF has to do that wouldn't be called when accessing the SP directly.
That aside, I have a query that returns three columns of strings. It runs pretty much instantly when I execute it via Enterprise Manager. If I run it via EF then it takes about six seconds. Granted, the results are being mapped into a complex type but when I've ran the query through SQL Server Profiler it's clear to see that the delay happens on the SQL server:
On the diagram, 1 indicates the SQL being called from Enterprise Manager, 2 indicates it being called via my app using EF.
Is there anything obvious I'm doing wrong here? I'd expect a delay of maybe a second or two, but the difference seems too great.
EDIT:
It seems that the stored procedure also runs slowly when called via ADO.Net. My colleague seems to think it's something to do with a bad execution plan that .Net is caching. By editing the stored procedure and saving it again it seemed to clear whatever was in the cache and both the ADO.Net and EF call to the stored procedure work well.
Has anyone else encountered anything like this before?
Take a look at this thread on SQL Server forum. It's a bit similar and might give some clues. In short, you may have different SQL Server execution environment options in SSMS and ADO.NET leading to different execution plans. Clearing the SQL Server plan cache should help.