Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
894 views
in Technique[技术] by (71.8m points)

entity framework - Exponentially deteriorating performance on inserts in SQL Server Compact 4.0 tables with an Identity column

EDIT: the issue below has been fixed in the Entity Framework 6.

Running the code below takes a disappointing 2 minutes and 10 seconds. Running it a second time takes 6.5 minutes. This question is related to this one

Private Sub RunTest()
    Dim sw As New Stopwatch
        sw.Restart()
        Using db As New TestDB
            db.Configuration.AutoDetectChangesEnabled = False
            For n = 1 To 100
                For m = 1 To 100
                    db.Tops.Add(New Top)
                Next
            Next
            db.SaveChanges()
        End Using
        MsgBox(sw.Elapsed.ToString)
    End Sub

The Entity:

Public Class Top
    Public Sub New()
        MyBase.New()
        One = "arerjlwkerjglwejrglwergoiwerhgiowehrowerlwelfvbwlervbowerghpiweurhgpiwuerviiervljwebbrlvjnepvjnweprvupiweurv"
        Two = "w;lrjgwwergkjwervgjwelrgjhwelghlwekglwergiuwehrgwjergjwervgjwerjgnwekrngpwergjpowergllwejrnglkwerngpoierhpiiuewrpjwenrwenrv;lwenrvkjernpgpsrvpi"

    End Sub

    'ID'
    Public Property ID As Integer

    'NATIVE PROPERTIES'
    Public Overridable Property One As String
    Public Overridable Property Two As String

    'NAVIGATION PROPERTIES'

End Class 

Moving the Using block inside the second level interation and calling SaveChanges there makes it only worse.

TestDB just inherits DBContext without any further configuration.When I disable database generation of the Top.ID property and supply the ID myself the performance improves a thirty fold.This problem makes using Database generated ID's in SQL Server Compact impossible. Is there a solution other than using client side generated IDs?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the issue, it is caused by the statement to get the generated id causing table scans, I propose a fix here, tested with 4000 entities, down from 17 secs to 2 secs. http://entityframework.codeplex.com/workitem/857 - it will be include in the EF6 build after Alpha 3


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...