Skip to content

Fix N+1 Query in Vets Endpoint-created-by-agentic #84

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 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/main/java/org/springframework/samples/petclinic/vet/Vet.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.samples.petclinic.model.Person;

import jakarta.persistence.Entity;
import org.springframework.samples.petclinic.model.Person;import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
Expand All @@ -42,10 +40,11 @@
* @author Arjen Poutsma
*/
@Entity
@Table(name = "vets")
public class Vet extends Person {
@Table(name = "vets")public class Vet extends Person {

@ManyToMany(fetch = FetchType.EAGER)
@ManyToMany(fetch = FetchType.LAZY)
@BatchSize(size = 100)
@ToString.Exclude
@JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"),
inverseJoinColumns = @JoinColumn(name = "specialty_id"))
private Set<Specialty> specialties;
Expand Down Expand Up @@ -76,4 +75,4 @@ public void addSpecialty(Specialty specialty) {
getSpecialtiesInternal().add(specialty);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import org.springframework.transaction.annotation.Transactional;import java.util.Collection;

/**
* Repository class for <code>Vet</code> domain objects All method names are compliant
Expand All @@ -34,15 +32,16 @@
* @author Juergen Hoeller
* @author Sam Brannen
* @author Michael Isvy
*/
public interface VetRepository extends Repository<Vet, Integer> {
*/public interface VetRepository extends Repository<Vet, Integer> {

/**
* Retrieve all <code>Vet</code>s from the data store.
* @return a <code>Collection</code> of <code>Vet</code>s
*/
@EntityGraph(attributePaths = {"specialties"})
@Transactional(readOnly = true)
@Cacheable("vets")
@Query("SELECT DISTINCT vet FROM Vet vet JOIN FETCH vet.specialties")
Collection<Vet> findAll() throws DataAccessException;

/**
Expand All @@ -55,4 +54,4 @@ public interface VetRepository extends Repository<Vet, Integer> {
@Cacheable("vets")
Page<Vet> findAll(Pageable pageable) throws DataAccessException;

}
}
3 changes: 3 additions & 0 deletions src/main/resources/db/postgres/indexes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add indexes for vet_specialties and specialties tables
CREATE INDEX IF NOT EXISTS idx_vet_specialties_vet_id ON vet_specialties(vet_id);
CREATE INDEX IF NOT EXISTS idx_specialties_id ON specialties(id);