一直以来都有这个疑问
一张文章表,文章有点赞、浏览记录,评论,都是三个独立的表,查询文章列表数据时需要展示这三个数量。
两种方案:
在文章表里增加三个数量字段,每次被点赞(取消点赞)/评论(删除评论)/浏览,都去更新这个字段,这样未免效率过低了,尤其是浏览量每次都要更新,而且冗余了字段
每次去关联查询总数,这样速度太慢。
如果放到缓存/搜索引擎里,那还需要每次都去更新吗,这样也太浪费了
高洛峰2017-04-28 09:05:59
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
黄舟2017-04-28 09:05:59
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.