EF Core's ExecuteUpdate Cuts 8,956 DB Queries to One for Bulk Updates
A developer discovered that a nightly archiving job built with EF Core was fetching 8,956 complete database rows just to flip a single boolean field on each, a pattern common in load-modify-save workflows. Benchmarking on a 20,000-row SQLite table showed this approach issued 8,956 SQL commands, took 27.4 ms, and allocated 1.8 MB of memory. Replacing the foreach loop with EF Core's ExecuteUpdate method reduced the operation to a single SQL UPDATE statement, cutting execution time to 10.6 ms and memory use to 68 KB. However, ExecuteUpdate bypasses change tracking, meaning in-memory entities are not updated and domain events or concurrency checks tied to SaveChanges are skipped. The developer recommends using load-modify-save only for entities with business logic, while bulk maintenance tasks should use set-based SQL operations instead.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in