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

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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?