We’ve been debating how to approach SQL data access in an upcoming project. Previous experience with NHibernate and Entity Framework was disappointing in terms of performance, for two main reasons:
There were other problems as well but we’ll touch the performance issue here.
For the first problem - badly structured SQL - your options are:
As for the second problem - slow processing of the SQL results - this again can be solved by switching to a more performance-oriented “micro-orm”. We were considering to switch from EF to Dapper which takes prides in its performance. Since we have reasons to stick with EF (type-safe queries being a main point), I decided to test the performance of the latest EF release using the same benchmark used in the Dapper charts.
Turns out, the main performance gains come from simply switching to .NET 4.5. Not only is EF’s performance boosted by over 80%, also other ORMs (including NHibernate, although to a lesser degree) are faster when running on .NET 4.5 as compared to .NET 4. So here are the results. I performed these tests using Dapper’s test program by just running them on my Visual Studio 2010 with standard .NET 4, then installing .NET 4.5 RC, rebuilding, and running them again.
[caption id=”attachment_111” align=”aligncenter” width=”1024”] (click for full-sized image)[/caption]
(Disclaimer: this is based on the Dapper test, and assumes that it’s a fair, relevant test for ORM query performance)
With .NET 4, EF and NHibernate are orders of magnitude slower than Dapper (and other fast frameworks). But with .NET 4.5, the difference is not so great (a factor of 2) and it may not be worth abandoning the high-level feature for such a slow down. Of course this depends on use case, requirements, etc.
Here are the same results in terms of performance improvement (calculated as: (original - new) / original):
[caption id=”attachment_112” align=”aligncenter” width=”1024”] (click for full-sized image)[/caption]
This is addressed to EF users mainly. If you’re worried about performance and:
…then you can stick around with EF.
However if the ORM performance is an absolutely critical bottleneck, you should still consider using something like Dapper which will give you greater control over query structure and processes results twice as fast as EF even under .NET 4.5, out-of-the-box.
P.S. If you’re using EF’s code-first with the fluent interface for mapping (rather than attributes), you’ll be able to switch to another solution in case you need to. This is a point against the pretty designer-based EDMX model-first approach. I like the clean classes being separate from the database-related code, so I use code-first anyway.