spring-projects / spring-projects/spring-data-relational

Saving entities with byte[] is slow

Open
#2,063 0 comments 0 reactions 1 assignee View on GitHub

@schauder is already working on this.

Since Jun 2, 2025.

status: waiting-for-triage
Dominant language
Java
Stars
827
Forks
394
PR merge metrics
No merged PRs in 30d

Description

Saving entities with byte arrays (byte[]) is very slow, especially when using larger byte arrays.

When debugging this, one can see that the byte[] is converted to a Byte[] inbetween. So I assume that every byte in the source byte array is boxed into an object. Which is slow and consumes lots of memory.

I created a reproducer for this: https://github.com/daspilker/spring-data-jdbc-reproducer

Saving an entity with a 20MB byte array takes more than 3 seconds in an H2 memory database. See https://github.com/daspilker/spring-data-jdbc-reproducer/actions/runs/15320075825/job/43101704776#step:4:749

I created a workaround to by-pass the converter for byte[] by using a custom JdbcConverter. See https://github.com/daspilker/spring-data-jdbc-reproducer/blob/main/src/test/java/org/example/ExampleRepositoryWorkaroundTest.java#L48

In that case, saving a 20MB byte array only takes a few milliseconds. See https://github.com/daspilker/spring-data-jdbc-reproducer/actions/runs/15320075825/job/43101704776#step:4:791

Is there a better way to avoid the conversion? Or is there a better representation for BLOBs than byte[]?

PS: I know that storing large byte arrays is a database is not the best idea, but it should not be unnecessary inefficient.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.