dongpaipu8394 2017-12-23 01:12
浏览 149
已采纳

Laravel / Eloquent查询效率:是否应该为这两列使用单独的表?

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...

  • 写回答

1条回答 默认 最新

  • dshun123456 2017-12-23 02:39
    关注

    It seems you're pre-optimizing.

    Do whatever's simplest for you right now to see your idea through. At your current scale (which I assume is the "prototype phase"), the difference between joining a second table or having two extra columns on a single model is negligible.

    The being said, creating a many-to-many relationship in Laravel is painfully easy and the added flexibility of separating your Generes may benefit you as you're fleshing out your concept.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable