I'm making a Laravel short story sharing application, and I have a question about how I should structure something:
Let's say I have a table for stories, and each story is allowed to have up to 2 genres applied to it. Right now I'm storing these genres directly in each story row in columns 'genre_primary' and 'genre_secondary'. I initially did this so I didn't have to use a relation between the Story model and a Genre model. I thought it might be more efficient.
BUT! I've learned about using eager loading and the Laravel Debugbar to track queries, and am using it to greatly cut down on querying while grabbing stories' authors for the byline in the main feed (using with('author')
in the Story
model retrieves the author from the User
table at the time of story retrieval, eliminating that n+1 problem of an additional author query per story displayed in the feed).
Like the author name, I also display the story's primary and secondary genres on the story's card in the feed. I'm now wondering if I should keep the genres embedded as two columns the story row, or extract a Genre
model, relate the Story
and Genre
models, and eager load them like the author name?
I guess my concern is over efficiency when loading a larger timeline of recent stories or stories filtered by a genre? It seems like just selecting stories where genre_primary = 'Mystery' would be more efficient than eager loading a relation to a separate Genre
model? Is it fine to denormalize in this case? Or am I just overthinking this...