InfoQ Homepage News Large Collection Performance in Entity Framework and EF Core

Large Collection Performance in Entity Framework and EF Core


Entity Framework Core (EF Core) offers significant performance improvements over Entity Framework 6 (EF 6). None are so readily apparent as when dealing with large collections.

When working with EF 6, a problem you may have experienced is context fouling. The longer you use a single DBContext object, the slower it becomes. This can be hard to see during testing if the number of records is small, but the cost increases proportionally to the number of objects stored in the change tracking system.

For this test, 10,000 rows were inserted into a table with eight columns on a local server. When performed in a single batch, it took over 160 seconds. Since this is a local sever, the network latency is negligible.

Using two batches of 5,000 reduced the total time to 82 seconds. Batches of 1,000 further reduces the run time to roughly 19 seconds.

This progression continues until approximately 100 batches of 100 rows each, which only takes 1.4 seconds. Below that, the run time starts increasing again with a batch size of 25 requiring 6.3 seconds. Coincidentally, a batch size of 250 likewise takes about six and a half seconds.

In the timings above, a new DBContext was created for each batch. In order to determine whether this was actually caused by context fouling, the same test was run again with one modification. Instead of creating a new DBContext for each batch, only one DBContext was used with SaveChangesbeing called between batches. As the line labeled “EF 6 B” shows in the chart below, the frequency in which you call SaveChangeshas no effect.

When looking at the chart, you may have noticed that EF Core appears to have a straight line. This is just an illusion caused by the dramatic increase in run time EF 6 experiences with batch size. Zooming in, you can see that batch size does have a small effect on EF Core.

Again, a batch size of roughly 100 improves performance. But the drop from the worst case of 2 seconds to a best case of 1.4 seconds may not be significant enough to consider.

Does this mean you should automatically start using batches of 100? Not necessarily. As mentioned above, this was using a local database. Once you add the cost of network latency, the sizes of the batches should be increased to reduce the number of round-trips to the database.

In the interest of completeness, ADO.NET without an ORM was also tested. Using multi-value insert statements with 250 rows per batch, it was able to complete the 10,000-row test in as little as 1.2 to 1.7 seconds.

For even better performance, we can turn to database-specific techniques such as SqlBulkCopy. With 10,000 rows, the run time was only 0.059 seconds. Even with 100,000 rows, the run time only increased to 0.647 seconds.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.