Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yannbriancon
Last active May 4, 2020 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yannbriancon/b1c96878a1d044342445397750484e23 to your computer and use it in GitHub Desktop.
Save yannbriancon/b1c96878a1d044342445397750484e23 to your computer and use it in GitHub Desktop.
Service triggering N+1 queries
void logMessages() {
// Get all the messages from the database
// -> Triggers 1 query
Set<Message> messages = messageDao.findAll();
// Map through the N messages to create the DTO with the author display name
// -> Triggers 1 query to fetch each author so N queries!
messages.stream.map(
message -> logger.info(
message.getAuthor().getName() + ": " + message.getText()
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment