Skip to content

Bad example of N+1 in current implementation #64

@lukaseder

Description

@lukaseder

This implementation here should be improved by not fetching single items at a time:

for (JdbcPet pet : pets) {
    owner.addPet(pet);
    pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
    List<Visit> visits = this.visitRepository.findByPetId(pet.getId()); // Problematic call
    for (Visit visit : visits) {
        pet.addVisit(visit);
    }
}

I'm not 100% convinced that the JDBC implementation of the pet clinic is very sensible in general. With SQL, I might be doing things a bit differently from JPA. It looks as though the whole example has been shoehorned into some pre-existing repositories.

But anyway, this one should be a no-brainer. There should be a method like:

interface VisitRepository {
    Map<Integer, List<Visit>> findByPetIds(Integer... ids);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions