2016-04-21 10 views
0

私のCypherクエリが時間がかかりすぎるのはなぜかと思います。Neo4jで新しいノードを追加するときのクエリのパフォーマンス

基本的に私は小さなファミリーツリー(2つのファミリ)を持っていますが、私は各ノードにメタデータの小さなビットを持つ新しいノードを追加しようとしています。彼らは質問される。 (これは@Tim Kuehnのおかげです。advice)。私は2人の家族を移入するクエリを実行すると

、私は問題なく迅速に構築され、これを持っている:

enter image description here

次に、私は前述の新しいノードを作成したいです。最初のノードは、すぐに作成した小さな家族(私は家族の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) 

...私はこれを提供します。ここまでは順調ですね!今後

enter image description here

、しかし、少し大きめの家族のノードが何らかの理由のために作成されることはありません。コードは同じですが、クエリだけで実行され、実行されます...

enter image description here

// '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) 

これはなぜ起こるのでしょうか?

// put index' on the name properties of the nodes: 
// CREATE INDEX ON :Person(name) 

が、それは何もしませんでした。私の最初のアイデア

nameプロパティにインデックスを置くことでした。

私はEXPLAINを見ようとしましたが、実際には何も教えてくれませんでした。 (実行時にも、端末自体に永遠に実行されます。)あなたの助けを

enter image description here

感謝。ここで

は、グラフを作成するために私のコードです:

// 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) 

答えて

2

これはなぜ起こるのでしょうか?

2番目の家族はもうループではなく、誰もが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) 
+0

...天才:

は病気が一定回数を示している家族全員を取得します。だから、正しく作成されていた最初の家族は、 "誰もが2倍につながった"グラフでした...でも、ノードの数は十分に小さかったですか?私はこの「二重接続された」グラフが過度のものだと思っています。感謝!! –

+0

正しい - 小さな家族グループにはそれほど多くのパスがなかったので、合理的な期間内に完了することができました。あなたが別の構造のために行くなら、私は親を一方的に、そして子供はもう一方に提案します。これにより、あなたは家族関係を辿ることができ、これらのスパイダーのつながりに巻き込まれることはありません。 –

+0

はい、接続が多すぎます。この修正でも、9ノードで4秒かかりました。私はそれを1000ノードほどの小さなもの(1000ノードがすべて200ファミリに分かれている場合は特に)にスケーリングすることを心配しています。 –

関連する問題