Neo4j 家谱关系设计

Neo4j Family Tree Relationship Design

我正在使用 Neo4j 设计大家族树。 在设计关系的过程中,我想到了两种方法:

  1. CREATE (p:Person)-[:PARENT_OF]->(s:Person) CREATE (p:Person)-[:STEPPARENT_OF]->(s:Person) CREATE (p:Person)-[:MARRIED_TO]->(s:Person)

通过这种方法,我为每个案例创建了不同的关系(请记住,会有很多案例 = 很多关系)

  1. CREATE (p:Person)-[r:PARENT_OF {type:'natural'}]->(s:Person) CREATE (p:Person)-[r:PARENT_OF {type:'step'}]->(s:Person) CREATE (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).

请记住,应根据应用程序需求构建图数据库模型。也就是说:这基本上取决于您向数据库询问的查询类型。

  • 如果您需要在图形横向查询中评估 type 关系,最好将其拆分为单独的关系类型。
  • 否则,将其保留为通用关系类型的 属性。