我正在尝试用当前可以在文件中访问的关系填充图形数据库。它们的形式是关系中的每一行,csv具有关系所描述的两个节点的唯一it以及它所描述的关系的类型。
csv关系中的每一行都有以下几条线:
uniqueid1,uniqueid2,relationship_name,property1_value,..., propertyn_value我已经创建了所有节点,并正在努力匹配每个文件中指定的唯一节点,然后创建它们之间的关系。
然而,往往需要很长的时间来为每一种关系创造,我的怀疑是我做错了什么。
csv文件有250万行,具有不同的关系类型。因此,我手动将relationships.rela属性设置为其中之一,并尝试通过创建与该关系相关的所有节点来运行,并使用我的where子句跟踪下一个节点。
每个节点的属性数量减少了一个省略号(.)名字也被删掉了。
目前,我有一个查询来创建以下列方式设置的关系
:auto USING PERIODIC COMMIT 100 LOAD CSV WITH HEADERS FROM 'file:///filename.csv' as relationships
WITH relationships.uniqueid1 as uniqueid1, relationships.uniqueid2 as uniqueid2, relationships.extraproperty1 as extraproperty1, relationships.rela as rela... , relationships.extrapropertyN as extrapropertyN
WHERE relations.rela = "manager_relationship"
MATCH (a:Item {uniqueid: uniqueid1})
MATCH (b:Item {uniqueid: uniqueid2})
MERGE (b) - [rel: relationship_name {propertyvalue1: extraproperty1,...propertyvalueN: extrapropertyN }] -> (a)
RETURN count(rel)如果能推荐替代模式的话,将不胜感激。
发布于 2022-08-24 06:27:40
索引是数据库使用的一种机制,用于加速数据查找。在您的例子中,由于Item节点没有索引,这两个匹配可能需要很长的时间,特别是如果Item节点的数量非常多的话。
MATCH (a:Item {uniqueid: uniqueid1})
MATCH (b:Item {uniqueid: uniqueid2})为了加快速度,您可以在Item节点uniqueid属性上创建一个索引,如下所示:
CREATE INDEX unique_id_index FOR (n:Item) ON (n.uniqueid)当您在创建索引后运行导入查询时,它将更快。但这仍然需要一些时间,因为有250万的关系。在neo4j 这里中阅读更多有关索引的内容。
发布于 2022-08-24 12:53:42
除了Charchit关于创建索引的建议外,我还建议使用APOC函数apoc.periodic.iterate,它将执行10k行的并行批查询。
https://neo4j.com/labs/apoc/4.4/overview/apoc.periodic/apoc.periodic.iterate/
例如:
CALL apoc.periodic.iterate(
"LOAD CSV WITH HEADERS FROM 'file:///filename.csv' as relationships RETURN relationships",
"WITH relationships.uniqueid1 as uniqueid1, relationships.uniqueid2 as uniqueid2, relationships.extraproperty1 as extraproperty1, relationships.rela as rela... , relationships.extrapropertyN as extrapropertyN
WHERE relations.rela = "manager_relationship"
MATCH (a:Item {uniqueid: uniqueid1})
MATCH (b:Item {uniqueid: uniqueid2})
MERGE (b) - [rel: relationship_name {propertyvalue1: extraproperty1,...propertyvalueN: extrapropertyN }] -> (a)",{batchSize:10000, parallel:true})第一个参数将返回csv文件中的所有数据,然后每批将行划分为10k,并使用默认并发(50个工作人员)并行运行。
当我在大约30分钟内加载40M节点/边时,我经常使用它。
https://stackoverflow.com/questions/73464256
复制相似问题