dotnet / dotnet/efcore

Implement bulk update with SaveChanges

Open
#35,401 1 comment 3 reactions 0 assignees View on GitHub
area-perf area-save-changes customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

When multiple identical updates are performed on different rows via SaveChanges, we currently generate N SQL UPDATEs:

```sql
UPDATE foo SET bar = @v1 WHERE id = @id1;
UPDATE foo SET bar = @v2 WHERE id = @id2;
UPDATE foo SET bar = @v3 WHERE id = @id3;
...
```

We could explore identifying such identical updates and collapsing them into a single UPDATE statements, e.g.:

```sql
UPDATE foo
SET bar = CASE
WHEN id = @id1 THEN @v1
WHEN id = @id2 THEN @v2
WHEN id = @id3 THEN @v3
...
END
WHERE "Id" IN (@id1, @id2, @id3);
```

We'd need to explore how this actually performs across different databases etc. This is conceptually similar to what we already do for SQL Server for insertion (where multiple INSERTs are collapsed to a single MERGE), and to #27550 for bulk deletion.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.