Skip to content

Conversation

shaykeren
Copy link

This PR addresses the N+1 query performance issue in the /vets.html endpoint by:

  1. Changing the Vet entity's specialties relationship from EAGER to LAZY fetching to prevent automatic loading of specialties
  2. Adding @entitygraph and JOIN FETCH to VetRepository methods to optimize specialty loading with a single query
  3. Adding an index on vet_specialties(vet_id) to improve join performance

Changes made:

  • Modified Vet.java to use FetchType.LAZY for specialties
  • Updated VetRepository.java to use @entitygraph and added optimized query method
  • Added database index on vet_specialties.vet_id

These changes will eliminate the N+1 query problem by:

  • Preventing automatic eager loading of specialties
  • Using a single optimized query with JOIN FETCH when needed
  • Improving join performance with the new index

Testing:

  • Verified that specialties are loaded correctly with a single query
  • Confirmed that the index improves join performance
  • Ensured backward compatibility with existing code

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