DbSet.Find method ridiculously slow compared to .SingleOrDefault on ID

dbcontext dbset ef-code-first entity-framework sql-server-ce-4


I have the following code (Database is SQL Server Compact 4.0):

Dim competitor=context.Competitors.Find(id)

When I profile this the Find method takes 300+ms to retrieve the competitor from a table of just 60 records.

When I change the code to:

Dim competitor=context.Competitors.SingleOrDefault(function(c) c.ID=id)

Then the competitor is found in just 3 ms.

The Competitor class:

Public Class Competitor
    Implements IEquatable(Of Competitor)

    Public Sub New()
        CompetitionSubscriptions = New List(Of CompetitionSubscription)
        OpponentMeetings = New List(Of Meeting)
        GUID = GUID.NewGuid
    End Sub

    Public Sub New(name As String)
        Me.Name = name
    End Sub

    Public Property ID As Long
    Public Property GUID As Guid

    Public Property Name As String

    Public Overridable Property CompetitionSubscriptions As ICollection(Of CompetitionSubscription)
    Public Overridable Property OpponentMeetings As ICollection(Of Meeting)
End Class

I defined the many to many relations for CompetitionSubscriptions and OpponentMeetings using the fluent API.

The ID property of the Competitor class is a Long which is translated by Code First to an Identity column with a primary key in the datatable (SQL Server Compact 4.0)

What is going on here??

7/27/2012 10:42:35 AM

Accepted Answer

Find calls DetectChanges internally, SingleOrDefault (or generally any query) doesn't. DetectChanges is an expensive operation, so that's the reason why Find is slower (but it might become faster if the entity is already loaded into the context because Find would not run a query but just return the loaded entity).

If you want to use Find for a lot of entities - in a loop for example - you can disable automatic change detection like so (can't write it in VB, so a C# example):

    context.Configuration.AutoDetectChangesEnabled = false;
    foreach (var id in someIdCollection)
        var competitor = context.Competitors.Find(id);
        // ...
    context.Configuration.AutoDetectChangesEnabled = true;

Now, Find won't call DetectChanges with every call and it should be as fast as SingleOrDefault (and faster if the entity is already attached to the context).

Automatic change detection is a complex and somewhat mysterious subject. A great detailed discussion can be found in this four-part series:

(Link to part 1, the links to parts 2, 3 and 4 are at the beginning of that article)


7/27/2012 12:54:52 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow