This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
The following loop takes 37 seconds to complete (on 5700 rows of data): var context = new LightSpeedContext<LightSpeedModel1UnitOfWork>("Development"); Now take the same loop, but add "true" to SaveChanges and it completes in 3 seconds: var context = new LightSpeedContext<LightSpeedModel1UnitOfWork>("Development"); Now while we might all agree that calling SaveChanges inside a foreach loop is questionable (lets just say the original code has a reason for it). There's even a more questionable reason why there's such a big performance difference between the two versions. And probably an even bigger question as to what SaveChanges is doing. First of all, no records are changed, so what exactly is SaveChanges doing? Secondly, why is the second version so much faster then the first version? This test was done against an SQL Server 2008 database, VS2010, .NET 4.0 and LightSpeed Nightly Build 27-04-2010. |
|
|
Calling SaveChanges(true) causes the UnitOfWork to reset which flushes the identity map of all entries. Calling it this way within a loop means that the first iteration will enumerate all entities in the map and then flush it, the remaining calls will be iterating over no entries. Calling SaveChanges() means the identity map is not reset between calls so it will iterate over all loaded entities N number of times. The difference in performance you are seeing is simply the difference in cost of iterating over the loaded set N number of times rather than 1. If your intention is to make changes within the scope of the loop against just the series object itself you could gain a similar performance boost by calling SaveChanges(true) but remember to attach the series object back to the UnitOfWork each time (call UnitOfWork.Attach(series) prior to the SaveChanges(true)).
Jeremy |
|
|
While I totally understand the difference between the two, the thing I do not get is *why* save changes is taking so long to check a changed-flag. I just timed it, SaveChanges (outside of the loop) takes 15ms to complete. About 70% of the time is spend in a function which (I think) is used to check if the entities have changed. I cant tell you which function it is due to obfuscation. Isnt that a bit much for checking a "simple" Changed-flag? (I assume you have a "Changed" flag) |
|