Skip to content

Entity Framework.Performance

martinsjohansen edited this page Apr 3, 2014 · 3 revisions

Written by Martin S. Johansen

The performance of Entity Framework has evolved since the first versions, since version 4.1 the performance has steadily been getting better. EF is till a young ORM compared to other ORMs but with version 6.1 the performance is finally getting there. Still, there are some need-to-know techniques in order to get EF to perform. There is a different techniques regarding Code First / Model First / Database First, but all variants share the same API for calling queries, and all use deferred execution and all use the same LINQ-provider and the extension methods for calling the database. There are som scenarios where database views or stored procedures can/must be used in order to get good performance.

The basics - Deferred execution

Calling a repository-query and using ToList or ToArray will force the query to execute to the database and fetch all data to memory. This is fine if you really want all the data in memory, but sometimes you really only need a few rows and not all the data in every row. Some expressions cannot be evaluated and converted to SQL, using ToList before the expression seems like the only possibility, but one can use Select() to avoid this.

Lets say you want to fetch all rows which matches a title:

var matches = bookRepository.Set.ToList().Where(b => b.Title.Contains("Hitchhiker's"));

Will translate into EF doing the following query:

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Title] AS [Title], 
    [Extent1].[Isbn] AS [Isbn], 
    [Extent1].[Description] AS [Description], 
    [Extent1].[Publisher_Id] AS [Publisher_Id]
    FROM [dbo].[Book] AS [Extent1]

That is not very efficient when having lots of books in the database, removing the ToList() (or putting tha ToList() last), translates into:

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Title] AS [Title], 
    [Extent1].[Isbn] AS [Isbn], 
    [Extent1].[Description] AS [Description], 
    [Extent1].[Publisher_Id] AS [Publisher_Id]
    FROM [dbo].[Book] AS [Extent1]
    WHERE [Extent1].[Title] LIKE N'%Hitchhiker''s%'

is a much more efficient database-query.

Selecting what you need

In the previous example, we fetched books matching a title. When EF does a query, the default is to select all the columns in the entity, that is sometimes more than you need. Lets say you want to display all the titles that matches the title, and also get the key (Id) so that you can link to a page for more details. The more efficient approach here is to use Select():

var matches = bookRepository.Set.Where(b => b.Title.Contains("Hitchhiker's")).Select(b => new { b.Id, b.Title}).ToList();

Will translate into EF doing the following query:

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Title] AS [Title]
    FROM [dbo].[Book] AS [Extent1]
    WHERE [Extent1].[Name] LIKE N'%Hitchhiker''s%'

In this example there where only a few columns, but with tables with many columns or many relations, selecting all columns all the time can have a huge performance hit. When relations are included in the query, all the related columns are included. Since the selected data will be transmitted over the network, scenarios with slow network connections or scenarios with many clients at the same time will especially benefit from selecting only the data you actually need.

Avoid calling database to often with eager loading

(ToDo)

Track changes only when you need it

One of the biggest performance hits in EF is change tracking. Entity Framework caches the DbContext and keep tracks of the entity state.

Source: Frans Bauma's Blog

Use AsNoTracking() when reading data you don't need to track! For example, when displaying a list of books you are not going to modify after reading, call AsNoTracking() as a part of the reading query:

var matches = bookRepository.Set.AsNoTracking().ToList().Where(b => b.Title.Contains("Hitchhiker's"));

Use AddRange / RemoveRange (Since EF 6.1)

These methods execute much faster than adding or removing a single object at a time because, by default, Entity Framework calls DetectChanges in each Add and Remove method. With the Range methods, you can handle multiple objects while DetectChanges is called only once, improving performance dramatically.

Map class-composition to complex types to avoid database relations (Code First)

(ToDo)

Use TPH (Table per Hierarchy) to avoid database relations (Code First)

(ToDo)

Create database indexes

Some queries will benefit from indexes, a full table scan will take much longer time to execute than querying an index. This is especially true for queries with multiple joins. Analyse queries to find which one who will benefit from indexes.

Creating a view in EF can be done fluent api this:

public class BookMap : EntityTypeConfiguration<Book>
{
	public BookMap()
	{
		HasIndex(c => c.Publisher);
	}
}

Note: In Code First since version 6.01, EF will by default create indexes for all foreign keys. If you want to turn this feature of, you can remove the convention like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<ForeignKeyIndexConvention>();
}

Database views and stored procedures

(ToDo)

Profiling

If your application is an MVC application using Code First, you can use StackEchanges's Miniprofiler. Scott Hanselman describes this tool in his blog at: http://www.hanselman.com/blog/NuGetPackageOfTheWeek9ASPNETMiniProfilerFromStackExchangeRocksYourWorld.aspx.