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

Fix #1302 n+1 queries in owners #1303

Closed
wants to merge 9 commits into from

Conversation

tekshteint
Copy link

@tekshteint tekshteint commented Jul 24, 2023

Issue #1302
When using the parameterless GET request on the /owners endpoint as well as searching for an empty string on the webpage for owners, the petclinic application ends up creating N+1 queries. This is due to the following code:

OwnerController.processFindForm

                if (owner.getLastName() == null) {
			owner.setLastName(""); // empty string signifies broadest possible search
		}

		
		Page<Owner> ownersResults = findPaginatedForOwnersLastName(page, owner.getLastName());
		if (ownersResults.isEmpty()) {
			// no owners found
			result.rejectValue("lastName", "notFound", "not found");
			return "owners/findOwners";
		}

This can be solved by adding a new query to the Owner Repository and updating the controller. These changes can be observed using digma.ai as I've left the necessary imports/dependencies for Open Telemetry as well.

@dsyer
Copy link
Member

dsyer commented Aug 24, 2023

It's not a terrible idea, but maybe not really all that necessary (given that n=5). Anyway, if you would like to remove the otel stuff and squash and rebase to a single commit I will take another look.

commit fd32ee7
Author: Tom Ekshtein <tekshteint@gmail.com>
Date:   Fri Sep 8 12:24:10 2023 -0700

    Removed OTEL

commit 33e2235
Author: Tom Ekshtein <tekshteint@gmail.com>
Date:   Mon Jul 24 11:24:02 2023 -0700

    fixed dependencies

commit 7fd676e
Author: Tom Ekshtein <tekshteint@gmail.com>
Date:   Mon Jul 24 11:20:59 2023 -0700

    removed old file

commit 0e5db6f
Author: Tom Ekshtein <tekshteint@gmail.com>
Date:   Mon Jul 24 11:20:00 2023 -0700

    N+1 fixed

    only my changes are present, everything else is as made by Spring
@tekshteint
Copy link
Author

Not a problem, sorry for the mess as I'm still new to Git but I hope that this should be more proper now.

Branches squashed and merged, OTEL has been removed and only the fix remains.

@dsyer
Copy link
Member

dsyer commented Sep 12, 2023

The changes to build.gradle are just whitespace right? So we don't need them. Also the build failed (error message tells you how to fix it).

Ran mvn spring-javaformat:apply as shown from github's build failure
@tekshteint
Copy link
Author

@dsyer Everything should be taken care of, thanks again for your patience with me.

Copy link
Contributor

@andrzejsydor andrzejsydor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the commit message is different that the changes

@tekshteint
Copy link
Author

the commit message is different that the changes

How so? I don't believe I changed anything else aside from running the spring formatting maven command

Copy link
Member

@dsyer dsyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more minor stuff to tweak.

@@ -34,6 +35,7 @@
import org.springframework.web.servlet.ModelAndView;

import jakarta.validation.Valid;
import org.thymeleaf.util.StringUtils;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Spring's StringUtils.

model.addAttribute("currentPage", page);
model.addAttribute("totalPages", paginated.getTotalPages());
model.addAttribute("totalItems", paginated.getTotalElements());
model.addAttribute("listOwners", listOwners);
model.addAttribute("listOwners", paginated);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"listOwners" is now added twice?

id 'org.springframework.boot' version '3.1.1'
id 'io.spring.dependency-management' version '1.1.0'
id 'org.graalvm.buildtools.native' version '0.9.23'
id 'java'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please get rid of whitespace changes (I don't think this file changed at all so you could just grab it from main).

@dsyer
Copy link
Member

dsyer commented May 20, 2024

I think we ran out of energy on this one. Assume not needed.

@dsyer dsyer closed this May 20, 2024
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.

None yet

3 participants