Fastest Way of Inserting in Entity Framework

前端 未结 30 2793
鱼传尺愫
鱼传尺愫 2020-11-21 05:23

I\'m looking for the fastest way of inserting into Entity Framework.

I\'m asking this because of the scenario where you have an active TransactionScope a

30条回答
  •  没有蜡笔的小新
    2020-11-21 05:58

    I've investigated Slauma's answer (which is awesome, thanks for the idea man), and I've reduced batch size until I've hit optimal speed. Looking at the Slauma's results:

    • commitCount = 1, recreateContext = true: more than 10 minutes
    • commitCount = 10, recreateContext = true: 241 sec
    • commitCount = 100, recreateContext = true: 164 sec
    • commitCount = 1000, recreateContext = true: 191 sec

    It is visible that there is speed increase when moving from 1 to 10, and from 10 to 100, but from 100 to 1000 inserting speed is falling down again.

    So I've focused on what's happening when you reduce batch size to value somewhere in between 10 and 100, and here are my results (I'm using different row contents, so my times are of different value):

    Quantity    | Batch size    | Interval
    1000    1   3
    10000   1   34
    100000  1   368
    
    1000    5   1
    10000   5   12
    100000  5   133
    
    1000    10  1
    10000   10  11
    100000  10  101
    
    1000    20  1
    10000   20  9
    100000  20  92
    
    1000    27  0
    10000   27  9
    100000  27  92
    
    1000    30  0
    10000   30  9
    100000  30  92
    
    1000    35  1
    10000   35  9
    100000  35  94
    
    1000    50  1
    10000   50  10
    100000  50  106
    
    1000    100 1
    10000   100 14
    100000  100 141
    

    Based on my results, actual optimum is around value of 30 for batch size. It's less than both 10 and 100. Problem is, I have no idea why is 30 optimal, nor could have I found any logical explanation for it.

提交回复
热议问题