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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?