Skip to content

Easily detect and fix N plus One SELECT with QuickPerf

Jean Bisutti edited this page Feb 21, 2020 · 26 revisions

N+1 select antipattern can lead to many JDBC roundtrips in production.

With QuickPerf you can easily detect and fix this antipattern from your tests.

What is an N+1 select?

N+1 select coming from an eager fetch type

N+1 select with a lazy fetch type...

Easily detect N+1 selects with QuickPerf

You can detect N+1 select by adding @ExpectSelect annotation on a test method to check the number of executed SELECT statements.

The outcome of an N+1 select is to have the same SELECT statements with different values. You can systematically detect this by configuring @DisableSameSelectTypesWithDifferentParams annotation with a global scope. In the previous examples, the outcome of the N+1 select was to have additional SELECT statements on Team table. These additional SELECT statements are the same apart from the id value of the Team table.

Hibernate code examples are available to play with these two ways of detecting N+1 selects:
πŸ‘‰ Β  Hibernate JUnit 4 code example
πŸ‘‰ Β  Hibernate JUnit 5 code example
πŸ‘‰ Β  Hibernate TestNG code example

The use of QuickPerf to detect N+1 selects in a Spring Boot, a Quarkus or a Micronaut application is demonstrated in this repository.

Easily fix N+1 selects with QuickPerf

QuickPerf automatically detects that the tested code uses Hibernate or Spring Data JPA. When a test is failing possibly due to an N+1 select, QuickPerf proposes ways to fix an N+1 select with Hibernate or Spring Data JPA:

Perhaps you are facing a N+1 select issue
	* With Hibernate, you may fix it by using JOIN FETCH
	                                       or LEFT JOIN FETCH
	                                       or FetchType.LAZY
	                                       or ...
	* With Spring Data JPA, you may fix it by adding
		@EntityGraph(attributePaths = { "..." }) on repository method.

Annotations

πŸ‘‰ Β Core

πŸ‘‰ Β JVM

πŸ‘‰ Β SQL

πŸ‘‰ Β Scopes

πŸ‘‰ Β Create an annotation

Supported frameworks

πŸ‘‰ Β JUnit 4

πŸ‘‰ Β JUnit 5

πŸ‘‰ Β TestNG

πŸ‘‰ Β Spring

How to

πŸ‘‰ Β Detect and fix N+1 SELECT

Project examples

πŸ‘‰ Β Maven performance

πŸ‘‰ Β Spring Boot - JUnit 4

πŸ‘‰ Β Spring Boot - JUnit 5

πŸ‘‰ Β Micronaut Data - JUnit 5

πŸ‘‰ Β Micronaut - Spring - JUnit 5

πŸ‘‰ Β Quarkus - JUnit 5

Miscellaneous

πŸ‘‰ Β FAQ

πŸ‘‰ Β QuickPerf code

Clone this wiki locally