我尝试运行以下查询,其中我在数据库和10个国家/地区拥有10个链。并使用20个地址的数组。 然后我循环通过那些为国家,连锁店和地址的每个组合创建一个商店,期待10 * 10 * 20组合 - 2000个商店。 但相反,查询运行直到创建大约200,000条记录,然后停止并显示未知错误。
MATCH (Chains:Chain), (Countries:Country)
WITH Collect(Chains) as ch,
Collect(Countries) as cou,
['Ullevål','Blindern','Centrum','Kringså','Lysaker','Skøyen','Fornebu','Stortinget','Nationalteatre','KarlJohan',
'Gamle','Grunerløkka','Grønland','Majorstuen','Snarøya','Asker','Sandvika','Drammen','Gothenburg','Stockholm'] as addresses
FOREACH(country in cou |
FOREACH (c in ch |
FOREACH (a in addresses |
CREATE (s:Store {name:c.name+"_"+a, address:a})
CREATE (s-[:BELONGS_TO]->c)
CREATE (s-[:IN]->country) )))
答案 0 :(得分:5)
如果您有以下数据......
FOREACH (i IN RANGE(1, 10) |
CREATE (:Chain {name:'Chain' + i}),
(:Country {name:'Country' + i})
)
...查询的第一部分
MATCH (Chains:Chain), (Countries:Country)
WITH COLLECT(Chains) AS ch, COLLECT(Countries) AS cou
将导致ch
和cou
的长度为100的集合,因为MATCH
正在检索笛卡尔积(10 ^ 2 = 100)。这就是您的200,000条记录的来源:100 * 100 * 20.您可以通过以下方式验证:
MATCH (Chains:Chain), (Countries:Country)
WITH COLLECT(Chains) AS ch, COLLECT(Countries) AS cou
RETURN LENGTH(ch), LENGTH(cou)
会告诉你每个集合的长度为100:
LENGTH(ch) LENGTH(cou)
100 100
您可以在查询的第一部分使用COLLECT(DISTINCT thing)
来解决此问题,只收集唯一的内容......
MATCH (Chains:Chain), (Countries:Country)
WITH COLLECT(DISTINCT Chains) AS ch, COLLECT(DISTINCT Countries) AS cou
RETURN LENGTH(ch), LENGTH(cou)
...会告诉您ch
和cou
是10个集合:
LENGTH(ch) LENGTH(cou)
10 10
现在你的嵌套FOREACH
将创建10 * 10 * 20 = 2000条记录:
MATCH (Chains:Chain), (Countries:Country)
WITH Collect(DISTINCT Chains) as ch,
Collect(DISTINCT Countries) as cou,
['Ullevål','Blindern','Centrum','Kringså','Lysaker','Skøyen','Fornebu','Stortinget','Nationalteatre','KarlJohan',
'Gamle','Grunerløkka','Grønland','Majorstuen','Snarøya','Asker','Sandvika','Drammen','Gothenburg','Stockholm'] as addresses
FOREACH(country in cou |
FOREACH (c in ch |
FOREACH (a in addresses |
CREATE (s:Store {name:c.name+"_"+a, address:a})
CREATE (s-[:BELONGS_TO]->c)
CREATE (s-[:IN]->country) )))
"添加了2000个标签,创建了2000个节点,设置了4000个属性,创建了4000个关系,在1262毫秒内返回了0行。"