redis - 数据库表设计的一些疑问,比如一篇文章(点赞数,浏览数,评论数要不要存在文章的表里)
phpcn_u1582
phpcn_u1582 2017-04-28 09:03:59
0
2
1394

一直以来都有这个疑问
一张文章表,文章有点赞、浏览记录,评论,都是三个独立的表,查询文章列表数据时需要展示这三个数量。


两种方案:

  1. 在文章表里增加三个数量字段,每次被点赞(取消点赞)/评论(删除评论)/浏览,都去更新这个字段,这样未免效率过低了,尤其是浏览量每次都要更新,而且冗余了字段

  2. 每次去关联查询总数,这样速度太慢。
    如果放到缓存/搜索引擎里,那还需要每次都去更新吗,这样也太浪费了

phpcn_u1582
phpcn_u1582

reply all(2)
小葫芦

When the number of comment tables grows to a certain extent, it is necessary to add redundant fields. Although there are a few more redundant fields to maintain, the performance improvement is obvious. After all, no one wants the database to be overwhelmed, right?

In addition, the number of likes, comments, and readings are not data with high real-time requirements, so there is no need to update this field every time you like (unlike)/comment (delete a comment)/browse, you can Just write a timed script to update those redundant fields every half hour or hour

黄舟

The design of the sub-table is correct. When you consider the increase in data volume, a heavy table is not easy to use. This is even more necessary if you consider that there will be additional information later such as likes.

It is recommended to cache and only update the cache in real time. Specific persistence can be run regularly.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!