Neo4J-存储到关系vs节点中 [英] Neo4J - Storing into relationship vs nodes

查看:344
本文介绍了Neo4J-存储到关系vs节点中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道将数据存储到关系或节点中是否有优点或缺点.

I was wondering if there are any advantages or disadvantages in storing data into relationships or nodes.

例如,如果我要将与讨论相关的注释存储到数据库中,我应该将注释数据存储在注释"关系中还是通过单独的关系与讨论相关的注释"节点中存储. /p>

For example, if I were to store comments related to a discussion into the DB, should I store the comment data in a "comment" relationship or a "comment" node that is related to the discussion through a separate relationship.

推荐答案

正确的数据模型取决于您需要进行查询的类型.您应该弄清楚查询是什么,然后确定满足以下条件的数据模型:

The correct data model depends on the types of queries you need to make. You should figure out what your queries are, and then determine a data model that meets these criteria:

  1. 它允许您回答所有查询,
  2. 它使您的查询能够足够快地完成,
  3. 它最大程度地减少了所需的数据库存储.

对于讨论注释,您可能希望按时间顺序查询讨论主题.因此,您不仅需要存储评论的时间,还需要存储评论之间的关系(因为讨论可能会产生不共享相同的先前评论的不连贯线程).

In the case of discussion comments, it is likely that you want to query for discussion threads, ordered chronologically. Therefore, you need to store not just the times at which comments are made, but also the relationships between the comments (because a discussion can spawn disjoint threads that do not share the same prior comments).

让我们尝试一个简单的测试用例.假设有两个由相同的初始注释(我们称为c1)产生的不相交的线程:[c1,c3]和[c1,c2,c4].并假设在这个简单的测试案例中,我们只对查询与主题相关的所有注释线程感兴趣.

Let's try a simple test case. Suppose there are 2 disjoint threads spawned by the same initial comment (which we'll call c1): [c1, c3] and [c1, c2, c4]. And suppose, in this simple test case, that we are only interested in querying for all comment threads related to a subject.

如果注释属性存储在节点中,则数据可能类似于:

If comment properties are stored in nodes, the data might look like:

(u1:User {name: "A"})-[:MADE]->(c1:Comment {time:0, text: "Fee"})-[:ABOUT]->(s1:Subject {title: "Jack"})
(u2:User {name: "B"})-[:MADE]->(c2:Comment {time:1, text: "Fie"})-[:ABOUT]->(c1)
(u3:User {name: "C"})-[:MADE]->(c3:Comment {time:3, text: "Foe"})-[:ABOUT]->(c1)
(u4:User {name: "D"})-[:MADE]->(c4:Comment {time:9, text: "Fum"})-[:ABOUT]->(c2)

如果您将注释属性存储在关系中,则可以尝试以下类似操作,但是有一个大缺点.一个关系无法直接指向另一个关系(正如我们尝试在第2至4行中所做的那样).由于该模型在neo4j中不合法,因此无法满足上述任何条件.

If you instead stored the comment properties in relationships, you might try something like the following, but there is a BIG FLAW. There is no way for a relationship to point directly to another relationship (as we try to do in lines 2 to 4). Since this model is not legal in neo4j, it fails to meet any the criteria above.

(u1:User {name: "A"})-[c1:COMMENTED_ABOUT {time:0, text: "Fee"}]->(s1:Subject {title: "Jack"})
(u2:User {name: "B"})-[c2:COMMENTED_ABOUT {time:1, text: "Fie"}]->(c1)
(u3:User {name: "C"})-[c3:COMMENTED_ABOUT {time:3, text: "Foe"}]->(c1)
(u4:User {name: "D"})-[c4:COMMENTED_ABOUT {time:9, text: "Fum"}]->(c2)

因此,在我们的简单测试用例中,似乎将属性存储在节点中是唯一的选择.

Therefore, in our simple test case, it looks like storing the properties in nodes is the only choice.

这里是一个查询,用于获取不连续的线程路径,包括进行每个注释的用户(WHERE子句过滤掉部分线程):

Here is a query for getting the disjoint thread paths, including the user who made each comment (the WHERE clause filters out partial threads):

MATCH p=(s:Subject)<-[:ABOUT*]-(c:Comment)<-[m:MADE]-(u:User)
WHERE NOT (c)<-[:ABOUT]-()
RETURN p

这篇关于Neo4J-存储到关系vs节点中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆