DEV Community

Discussion on: How the N+1 query problem can burn your database

Collapse
 
aarone4 profile image
Aaron Reese

Hmmm.
Two things. Firstly in the function getDrinksByIDsIn you are passing a dynamic query to the database with a custom list of IDs each time. Although this is avoiding the N+1 issue there is likely to be a performance hit because the database cannot cache an optimised query plan.
Second, I was struggling to follow the code as I don't recognize the language and syntax but I am pretty sure the getDrinkById is doing an N+1 search as it has to internally iterate the array to find the correct drink Id.
Probably faster in memory than network latency to the database but certainly not free.
As for the comment about returning multiple meal records if a meal has more than one drink, a properly structured object with a collection of drinks would solve the issue. Modelling your data is the key to happy applications.