我正在使用 Neo4j 设计大家族树。 在设计关系的过程中,我想到了两种方法:
CREATE (p:Person)-[:PARENT_OF]->(s:Person) 创建(p:人)-[:STEPPARENT_OF]->(s:人) 创建 (p:Person)-[:MARRIED_TO]->(s:Person)
通过这种方法,我为每个案例创建了不同的关系(请记住,会有很多案例 = 很多关系)
CREATE (p:Person)-[r:PARENT_OF {type:'natural'}]->(s:Person) 创建 (p:Person)-[r:PARENT_OF {type:'step'}]->(s:Person) 创建 (p:Person)-[r:SPOUSE_OF {type:'marriage'}]->(s:Person)
使用这种方法,关系会减少,但设计会有点困惑。
我想知道哪种方法更好,为什么?
最佳答案
您正在选择细粒度(:PARENT_OF
、:STEPPARENT_OF
、:MARRIED_TO
)或通用关系(:PARENT_OF {type:'natural'}
, :PARENT_OF {type:'step'}
, :SPOUSE_OF {type:'marriage'}
).
Ian Robinson、Jim Webber 和 Emil Eifrém 所著的图形数据库一书(可在 Neo4j 网站上以 download 的价格获得)说:
Differentiating by relationship name is the best way of eliminating large swathes of the graph from a traversal. Using one or more property values to decide whether or not to follow a relationship incurs extra I/O the first time those properties are accessed because the properties reside in a separate store file from the relationships (after that, however, they’re cached).
请记住,应根据应用程序需求构建图数据库模型。也就是说:这基本上取决于您向数据库提出的查询类型。
- 如果您需要评估图横向查询中关系的
类型
,将其拆分为单独的关系类型可能是个好主意。 - 否则,将其保留为通用关系类型的属性。
关于Neo4j 家谱关系设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44386230/