Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize entity deletion in SimpleJpaRepository #3564

Closed

Conversation

Seol-JY
Copy link
Contributor

@Seol-JY Seol-JY commented Aug 7, 2024

Optimize the delete method in SimpleJpaRepository to improve performance in certain scenarios. The main change is to first check if the entity is already managed by the EntityManager.

  • Reordered the deletion logic to prioritize managed entities
  • Added a check for entityManager.contains(entity) at the beginning of the method

The goal of this change is to reduce unnecessary database lookups when deleting managed entities, potentially improving performance in high-throughput scenarios.

The goal of this change is to optimize the deletion process for entities that are already managed by the EntityManager (present in the first-level cache). This can potentially improve performance in scenarios where entities are likely to be in the cache before deletion, such as when using the deleteById method after recent read or write operations.

Please point out any potential risks or edge cases where this change might lead to unexpected behavior.

This change improves the performance of the delete method by first checking if the entity is already managed by the EntityManager. If so, it removes the entity directly without additional database queries. This optimization can reduce unnecessary database lookups in certain scenarios.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 7, 2024
@quaff
Copy link
Contributor

quaff commented Aug 9, 2024

The goal of this change is to reduce unnecessary database lookups when deleting managed entities

entityManager.find(type, entityInformation.getId(entity)) will not trigger database lookup If entityManager.contains(entity).

@mp911de mp911de self-assigned this Aug 12, 2024
@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 13, 2024
@mp911de mp911de closed this in 58fe95f Aug 13, 2024
mp911de added a commit that referenced this pull request Aug 13, 2024
Defer user-class lookup.

See #3564
mp911de pushed a commit that referenced this pull request Aug 13, 2024
This change improves the performance of the delete method by first checking if the entity is already managed by the EntityManager. If so, it removes the entity directly without additional database queries. This optimization can reduce unnecessary database lookups in certain scenarios.

Closes #3564
mp911de added a commit that referenced this pull request Aug 13, 2024
Defer user-class lookup.

See #3564
@mp911de
Copy link
Member

mp911de commented Aug 13, 2024

Thank you for your contribution. That's merged, polished, and backported now.

@mp911de mp911de added this to the 3.3.3 (2024.0.3) milestone Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants