but queries are supposed to denormalize
Well, that's how they've always worked, yes.Might there be a better way for some use cases?
denormalize to present information in the desired format
What if my "desired format" is a structure that maintains the relationships specified in the database?This is an incredibly common pattern, particularly with ORMs.
# psuedocode
for each recipe in Recipe.joins(Ingredient).select_all() do
for each ingredient in recipe.ingredients do
print(f"{recipe.name} requires {ingredient.name}")
Depending on the ORM and the query options specified, this results in one of the following:1. Everything getting denormalized into a single, wasteful result table and then teased back out into Recipes and Ingredients by the ORM
2. Two SQL queries. One to fetch the recipes, and then one to fetch the ingredients once we know the id's of the recipies.
3. A big ol' N+1 situation with 1 query to fetch recipes and then N additional queries to fetch the ingredients.
They all have various inefficiencies. What if we just returned the relational structures directly?