Last active
May 4, 2020 19:59
-
-
Save yannbriancon/b1c96878a1d044342445397750484e23 to your computer and use it in GitHub Desktop.
Service triggering N+1 queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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