我想知道为什么我的Cypher查询花费了过多的时间。
基本上,我有一个小的家族树(两个家庭),我正在尝试为每个家庭树添加一个带有一些小元数据的新节点,这样当他们家人更容易彼此隔离时被查询。 (感谢@Tim Kuehn为此advice)。
一旦我运行查询来填充我的两个家庭,我就有了这个,它很快就可以构建而没有问题:
接下来,我想创建上述新节点。第一个节点快速创建,应用于较小的系列(我称之为B系列):
// 'add a :Family node for each relational group, like so:'
CREATE (famB:Family)
WITH famB
MATCH (a:Person {name:"Gramps Johnson"})-[:RELATED_TO*]->(b:Person)
MERGE (famB:Family)<-[:FAMILY]-(a)
MERGE (famB:Family)<-[:FAMILY]-(b)
......这给了我这个。到现在为止还挺好!
然而,向前发展,由于某种原因,从未创建过大的族节点。代码是相同的,但查询只是运行并运行...
// 'add a :Family node for each relational group, like so:'
CREATE (famA:Family)
WITH famA
MATCH (a:Person {name:"Gramps Doe"})-[:RELATED_TO*]->(b:Person)
MERGE (famA:Family)<-[:FAMILY]-(a)
MERGE (famA:Family)<-[:FAMILY]-(b)
为什么会这样?
我的第一个想法是在name
属性上添加一个索引:
// put index' on the name properties of the nodes:
// CREATE INDEX ON :Person(name)
但是没有做任何事情。
所以我试着看一下EXPLAIN,但它并没有真正告诉我任何事情。 (它在执行时也会永久运行在终端上。)
感谢您的帮助。
这是我创建图表的代码:
// FAMILY A2: create grandparents, their son.
CREATE (grampsdoe:Person {name: 'Gramps Doe', id:'1', Gender:'Male', Diagnosis: 'Alzheimers', `Is Alive?`: 'No', Handedness: 'Left', `Risk Score`: 'PURPLE'})
CREATE (gramsdoe:Person {name: 'Grams Doe', id:'2', Gender:'Female', Diagnosis: 'Alzheimers', `Is Alive?`: 'No', Handedness: 'Right', `Risk Score`: 'GIRAFFE'})
CREATE (daddoe:Person {name: 'Dad Doe', id:'3', Gender:'Male', Diagnosis: 'MCI', `Is Alive?`: 'No', Handedness: 'Right', `Risk Score`: 'GIRAFFE'})
CREATE
(grampsdoe)-[:RELATED_TO {relationship: 'Husband'}]->(gramsdoe),
(gramsdoe)-[:RELATED_TO {relationship: 'Wife'}]->(grampsdoe),
(grampsdoe)-[:RELATED_TO {relationship: 'Father'}]->(daddoe),
(gramsdoe)-[:RELATED_TO {relationship: 'Mother'}]->(daddoe),
(daddoe)-[:RELATED_TO {relationship: 'Son'}]->(grampsdoe),
(daddoe)-[:RELATED_TO {relationship: 'Son'}]->(gramsdoe)
// FAMILY A2: create grandparents, their daughter
CREATE (grampssmith:Person {name: 'Gramps Smith', id:'4', Gender:'Male', Diagnosis: 'Normal', `Is Alive?`: 'No', Handedness: 'Left', `Risk Score`: 'PURPLE'})
CREATE (gramssmith:Person {name: 'Grams Smith', id:'5', Gender:'Female', Diagnosis: 'Alzheimers', `Is Alive?`: 'No', Handedness: 'Ambidextrous', `Risk Score`: 'PURPLE'})
CREATE (momsmith:Person {name: 'Mom Doe', id:'6', Gender:'Female', Diagnosis: 'Alzheimers', `Is Alive?`: 'No', Handedness: 'Right', `Risk Score`: 'GIRAFFE'})
CREATE
(grampssmith)-[:RELATED_TO {relationship: 'Husband'}]->(gramssmith),
(gramssmith)-[:RELATED_TO {relationship: 'Wife'}]->(grampssmith),
(grampssmith)-[:RELATED_TO {relationship: 'Father'}]->(momsmith),
(gramssmith)-[:RELATED_TO {relationship: 'Mother'}]->(momsmith),
(momsmith)-[:RELATED_TO {relationship: 'Daughter'}]->(grampssmith),
(momsmith)-[:RELATED_TO {relationship: 'Daughter'}]->(gramssmith)
// FAMILY A3: 'Dad Doe' and 'Mom Smith' get married and have 2 kids who are twins
CREATE (lilbro:Person {name: 'Lil Bro', id:'7', Gender:'Male', Diagnosis: 'Normal', `Is Alive?`: 'Yes', Handedness: 'Right', `Risk Score`: 'PURPLE'})
CREATE (bigsis:Person {name: 'Big Sis', id:'8', Gender:'Female', Diagnosis: 'Normal', `Is Alive?`: 'Yes', Handedness: 'Right', `Risk Score`: 'PURPLE'})
CREATE (daddoe)-[:RELATED_TO {relationship: 'Husband'}]->(momsmith)
CREATE (momsmith)-[:RELATED_TO {relationship: 'Wife'}]->(daddoe)
CREATE (lilbro)-[:RELATED_TO {relationship: 'Brother'}]->(bigsis)
CREATE
(lilbro)-[:RELATED_TO {relationship: 'Grandson'}]->(grampsdoe),
(grampsdoe)-[:RELATED_TO {relationship: 'Grandfather'}]->(lilbro),
(lilbro)-[:RELATED_TO {relationship: 'Grandson'}]->(grampssmith),
(grampssmith)-[:RELATED_TO {relationship: 'Grandfather'}]->(lilbro),
(lilbro)-[:RELATED_TO {relationship: 'Grandson'}]->(grampssmith),
(grampssmith)-[:RELATED_TO {relationship: 'Grandmother'}]->(lilbro),
(lilbro)-[:RELATED_TO {relationship: 'Grandson'}]->(gramssmith),
(gramssmith)-[:RELATED_TO {relationship: 'Grandmother'}]->(lilbro),
(lilbro)-[:RELATED_TO {relationship: 'Son'}]->(daddoe),
(daddoe)-[:RELATED_TO {relationship: 'Father'}]->(lilbro),
(lilbro)-[:RELATED_TO {relationship: 'Son'}]->(momsmith),
(momsmith)-[:RELATED_TO {relationship: 'Mother'}]->(lilbro),
(bigsis)-[:RELATED_TO {relationship: 'Sister'}]->(lilbro),
(bigsis)-[:RELATED_TO {relationship: 'Granddaughter'}]->(grampsdoe),
(grampsdoe)-[:RELATED_TO {relationship: 'Grandfather'}]->(bigsis),
(bigsis)-[:RELATED_TO {relationship: 'Granddaughter'}]->(grampssmith),
(grampssmith)-[:RELATED_TO {relationship: 'Grandfather'}]->(bigsis),
(bigsis)-[:RELATED_TO {relationship: 'Granddaughter'}]->(gramsdoe),
(gramsdoe)-[:RELATED_TO {relationship: 'Grandmother'}]->(bigsis),
(bigsis)-[:RELATED_TO {relationship: 'Granddaughter'}]->(gramssmith),
(gramssmith)-[:RELATED_TO {relationship: 'Grandfather'}]->(bigsis),
(bigsis)-[:RELATED_TO {relationship: 'Daughter'}]->(daddoe),
(daddoe)-[:RELATED_TO {relationship: 'Father'}]->(bigsis),
(bigsis)-[:RELATED_TO {relationship: 'Daughter'}]->(momsmith),
(momsmith)-[:RELATED_TO {relationship: 'Mother'}]->(bigsis)
// FAMILY B1: create grandparents, their son.
CREATE (grampsjohnson:Person {name: 'Gramps Johnson', id:'9', Gender:'Male', Diagnosis: 'Normal', `Is Alive?`: 'No', Handedness: 'Right', `Risk Score`: 'GIRAFFE'})
CREATE (gramsjohnson:Person {name: 'Grams Johnson', id:'10', Gender:'Female', Diagnosis: 'Normal', `Is Alive?`: 'No', Handedness: 'Right', `Risk Score`: 'GIRAFFE'})
CREATE (johnjohnson:Person {name: 'John Johnson', id:'11', Gender:'Male', Diagnosis: 'MCI', `Is Alive?`: 'Yes', Handedness: 'Right', `Risk Score`: 'GIRAFFE'})
CREATE
(grampsjohnson)-[:RELATED_TO {relationship: 'Husband'}]->(gramsjohnson),
(gramsjohnson)-[:RELATED_TO {relationship: 'Wife'}]->(grampsjohnson),
(grampsjohnson)-[:RELATED_TO {relationship: 'Father'}]->(johnjohnson),
(gramsjohnson)-[:RELATED_TO {relationship: 'Mother'}]->(johnjohnson),
(johnjohnson)-[:RELATED_TO {relationship: 'Son'}]->(grampsjohnson),
(johnjohnson)-[:RELATED_TO {relationship: 'Son'}]->(gramsjohnson)
答案 0 :(得分:2)
为什么会发生这种情况?
正在发生的原因是第二个家庭不再是一个循环,它是“每个人都连接两次”。这意味着“制作一个家庭节点”代码的这一部分:
MATCH (a:Person {name:"Gramps Doe"})-[:RELATED_TO*]->(b:Person)
正在追踪大量图表,因此系统停滞不前。
由于目标组中有8个节点,我将路径限制在1到8跳的范围内( [:RELATED_TO * 1..8] ) -
CREATE (famA:Family)
WITH famA
MATCH (a:Person {name:"Gramps Doe"})-[:RELATED_TO*1..8]->(b:Person)
MERGE (famA:Family)<-[:FAMILY]-(a)
MERGE (famA:Family)<-[:FAMILY]-(b)
然后就完成了。
让疾病已经出现一定次数的整个家庭:
// count the family members with a disease
MATCH (f:Family)<-[:FAMILY]-(person:Person)
WHERE person.Diagnosis = "Alzheimers"
WITH f, count(person) AS Count
WHERE Count > 2
// Then report the family members as a single collection
MATCH (a:Person)-[r1:FAMILY]-(f)
RETURN collect(DISTINCT a)