Skip to content

Fix N+1 query performance issue in Vet.specialties relationship #107

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

shaykeren
Copy link

Problem

Critical N+1 query performance issue in spring-petclinic service affecting /vets.html endpoint with 5 repeated SQL queries instead of optimized batch operations.

Issue ID: e8efc2b4-5f5e-11f0-bbd9-0242ac160009
Affected Endpoint: HTTP GET /vets.html
Trace ID: E78ADC1B4AB172AAA6DAC2D716CB4E0D

Solution

Modified the Vet.specialties relationship configuration to optimize query performance:

  1. Changed fetch strategy: FetchType.EAGERFetchType.LAZY
  2. Added batch optimization: @BatchSize(size = 10) annotation
  3. Added required import: org.hibernate.annotations.BatchSize

Changes Made

  • File: src/main/java/org/springframework/samples/petclinic/vet/Vet.java
  • Before: @ManyToMany(fetch = FetchType.EAGER)
  • After: @BatchSize(size = 10) + @ManyToMany(fetch = FetchType.LAZY)

Expected Impact

This fix should reduce the 5 separate SQL queries to a single optimized batch query for the /vets.html endpoint, significantly improving performance.

Testing

This follows the same solution pattern that was successfully applied to Owner.pets relationship in commit 30fee0f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant