Recently I’ve been experiencing serious performance problems with Entity Framework. A query that was previously taking under a second to run was now taking over a minute to run. Taking the generated SQL and manually running it (either exactly or similar code) took 0.4 seconds to run.
Adding some system.diagnostics stopwatches showed that that minute was being spent executing a line of LINQ code that hadn’t changed at all since the time it was executing in under a second. The Visual Studio profiler showed most of the time was spend executing LINQ IQuerable code and wasn’t giving me very much more. Reading internet advice on improving EF performance suggested many things that might achieve a marginal improvement, but nothing that could explain such a crash in performance.
So I suggested upgrading to Entity Framework 6.1.2. Worth a shot I thought. Within 5 minutes, I was upgraded and it was executing the same query in 0.6 seconds. So it really was >100 times faster.
So my advice is if you are using an old version of Entity Framework, get it upgraded to the latest version. There are many reasons for doing so: better performance is the most essential one.
Update 22nd Feb:
Rob Conery has just written a provocative anti-ORM blog post (link below). The whole ORM vs Sprocs argument is much like the TDD one: it’s very old and the problem is people tend to have very polarised views on it, either always use an ORM or always use Stored Procedures. Most of the time, neither of those is a very good idea.
Stored procedures are better for some jobs and an ORM is better for some other jobs. So my advice is don’t stop your developers from using either. Let them use the best tool for the job. Although Entity Framework has been around for many years, it is only just reaching maturity. But it is unlikely to ever completely do away with stored procedures. If you are considering using EF I can’t give you a simple yes/no answer as to whether it is the right choice for you. Do your research and think carefully before making your decision. But don’t consider any earlier version of EF. Version 6+ is what you (might) want.
Get over that Stored Procedure aversion
2 thoughts on “Entity Framework 6.1 is over 100 times faster!”
Did you restart your computer between these tests? The performance difference you are talking about seems to be a parameter sniffing problem to me, rather than version differences in EF. Did you use sql server profiler to see what was sent to the sql server?
Yes I used SQL server profiler. I suspect that I restarted the PC between the tests but as it was more than 2 years ago I can’t be sure. Regardless I strongly recommend anyone using an old version of EF upgrades to at least the latest v6.1.x. For some developers EF Core will suit them better, others not.