Entity Framework 5 - Performance Considerations
While Entity Framework (EF) 5 promises to bring significant performance improvements over EF 4, there are still some considerations to be aware of to use the ORM effectively – namely View generation, Caching, auto-compiled queries, Query execution options and Design-time considerations.
Microsoft has released a white paper outlining various performance considerations while using the yet-to-be-released Entity Framework 5 (which will run under the up-coming .NET 4.5).
Some interesting factors to watch out are -
- Cold-vs-Warm query execution – there is a view generation step (transformations necessary to go from database schema to conceptual schema and vice-versa) which adds to the overhead the first time a query is run – later runs are faster due to view-caching. You could pre-generate views to improve performance.
- Caching – at object level (especially improves DbContext Find() in conjunction with disabling AutoDetectChanges), Query Plan Caching, MetaData Caching and Results Caching. There is still no second-level caching for EF, although there is guidance on how to implement one – a sample on codeplex and Julia Lerman’s article “Second Level Caching in Entity Framework and Windows Azure”
- Option for No-Tracking queries (without the overhead of state tracking). This is available only for ObjectQuery though and not for DbSet and DbQuery classes
- Fast Micro-ORM style SqlQuery on Database and ExecuteStoreQuery
- Design-time considerations – Table-per-hierarchy (TPH) vs. Table-per-Type (TPT) vs. Table-per-Class (TPC); TPT is the worst in terms of resulting query complexity and performance
- Lazy-vs-Eager loading
Entity Framework is a Commercial Object-Relational Mapper for .NET applications provided by Microsoft free of cost. Apart from EF, there are several mature alternatives (both Open Source and Commercial) available for .NET developers, like NHibernate and LightSpeed, along with light-weight micro-ORMs like Dapper.NET, PetaPoco and Massive.