redis - 如何设计“多对一”在非关系型数据库
ringa_lee
ringa_lee 2017-04-21 11:16:51
0
3
927

现在存文章内容用hash类型:

post:$post_id
    title,
    content
    ...

comment:$comment_id
    content,
    date,
    status

posts:
    $post_id, $comment_id

存了一个hash类型的posts来表示关系,可能是还是没摆脱关系型数据库。

这种“一对多”的模式,如何在redis上更合理……更redis的体现出来.
应该如何设计这里

ringa_lee
ringa_lee

ringa_lee

reply all(3)
伊谢尔伦

NoSQL rarely fully supports the Join function, so denormalized form is generally used to store the expanded relationship in NoSQL, so the relationship is no longer "many-to-one", but "one-to-many".

You can use collection types to store subdocuments, such as list or set in Redis, sub-document in MongoDB, etc.

For NoSQL, there is a simple principle, which is to put related things together, such as articles and comments. Note that if possible, try to put all attributes, including content, of articles and comments together, rather than just in the article. The id of the comment is stored in the record.
The advantage of this is obvious. You only need one read operation to obtain all the data related to this article. No matter how you segment the data, the article and its related things will not be separated into different physical machines. This can be extremely efficient. Greatly improve backend scalability.
Of course, such a design may not always be suitable for your application. For example, the relationship between articles and authors may not be completely denormailizeable, so you need to carefully consider the data presentation method and process to decide how to organize the data.

In short, NoSQL is not a panacea. While it provides higher flexibility, scalability and performance, it also simplifies the original black box database into a white box storage engine. You need to find out among the various factors yourself. The perfect balance, this job does require a lot of experience and a deep understanding of the business.

Of course, if the amount of data is small, this is not a problem.

阿神

Use list?

巴扎黑

Isn’t it possible to use set? Put the id of comments in a post_id: set.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template