Speed up LINQ inserts

前端 未结 11 1627
自闭症患者
自闭症患者 2021-02-01 07:46

I have a CSV file and I have to insert it into a SQL Server database. Is there a way to speed up the LINQ inserts?

I\'ve created a simple Repository method to save a rec

11条回答
  •  北荒
    北荒 (楼主)
    2021-02-01 08:08

    I wonder if you're suffering from an overly large set of data accumulating in the data-context, making it slow to resolve rows against the internal identity cache (which is checked once during the SingleOrDefault, and for "misses" I would expect to see a second hit when the entity is materialized).

    I can't recall 100% whether the short-circuit works for SingleOrDefault (although it will in .NET 4.0).

    I would try ditching the data-context (submit-changes and replace with an empty one) every n operations for some n - maybe 250 or something.


    Given that you're calling SubmitChanges per isntance at the moment, you may also be wasting a lot of time checking the delta - pointless if you've only changed one row. Only call SubmitChanges in batches; not per record.

提交回复
热议问题