[英]Neo4j - slow cypher query - big graph with hierarchies
使用Neo4j 2.1.4。 我有一個節點之間具有“ IS A”關系(以及其他類型的關系)的圖形。 我在圖內有一些層次結構(IS A關系),我需要知道一個層次結構的后代(IS A關系),該層次結構與第二層次結構的某些后代具有特定的已知關系。 如果存在該特定的已知關系,則返回第一個層次結構的后代。
輸入: “ ID_parentnode_hierarchy_01”,“ ID_relationship”,“ ID_parentnode_hierarchy_02”。
輸出: “ ID_parentnode_hierarchy_01”的后代(IS關系)與“ ID_parentnode_hierarchy_02”的后代具有“ ID_relationship”。
注意:該圖具有500.000節點和200萬個關系。
我正在使用此密碼查詢,但是它非常慢(在4GB RAM和3GHz Pentium Dual Core 64位PC中大約為40s)。 有可能建立一個更快的查詢嗎?
MATCH (parentnode_hierarchy_01: Node{nodeid : {ID_parentnode_hierarchy_01}})
WITH parentnode_hierarchy_01
MATCH (parentnode_hierarchy_01) <- [:REL* {reltype: {isA}}] - (descendants01: Node)
WITH descendants01
MATCH (descendants01) - [:REL {reltype: {ID_relationship}}] -> (descendants02: Node)
WITH descendants02, descendants01
MATCH (parentnode_hierarchy_02: Node {nodeid: {ID_parentnode_hierarchy_02} })
<- [:REL* {reltype: {isA}}] - (descendants02)
RETURN DISTINCT descendants01;
非常感謝你。
好吧,我可以稍微整理一下您的查詢-這可以幫助我們更好地理解問題。 我懷疑這會運行得更快,但是使用清理后的版本,我們可以討論正在發生的事情:(主要是消除對MATCH
/ WITH
不必要使用)
MATCH (parent:Node {nodeid: {ID_parentnode_hierarchy_01}})<-[:REL* {reltype:{isA}}]-
(descendants01:Node)-[:REL {reltype:{ID_relationship}}]->(descendants02:Node),
(parent2:Node {nodeid: {ID_parentnode_hierarchy_02}})<-[:REL* {reltype:{isA}}]-
(descendants02)
RETURN distinct descendants01;
看起來您正在從根開始搜索兩棵(可能是大棵)樹,以尋找樹中某處由{ID_relationship}
鏈接的兩個節點。
除非您可以提供有關樹中哪個節點可能具有ID_relationship
或類似名稱的查詢提示,否則,最糟糕的是,看起來您最終可能會比較兩棵樹中的每兩個節點。 因此,這似乎需要花費n * k的時間,其中n是第一棵樹中的節點數,k是第二棵樹中的節點數。
以下是一些需要考慮的策略事項-您應根據數據使用哪種策略:
[:REL* {reltype:{isA}}]
的深度上設置范圍嗎? descendants01
和descendants02
? 是否有什么可以使查詢更具選擇性的,從而不會將一棵樹中的每個節點與另一棵樹中的每個節點進行比較? 您可以嘗試的另一種策略是:(這可能是一個可怕的想法,但是值得嘗試)-基本上是在一個isa
類型或另一個isa
任意數量的無向邊緣上尋找從一個根到另一個根的路徑。 您的數據模型具有:REL
關系和reltype
屬性。 這可能是一種反模式; 為什么關系類型不只是reltype
屬性? 這可以防止我想寫 ,下面的查詢:
MATCH p=shortestPath((p1:Node {nodeid: {first_parent_id}})-[:isA|ID_relationship*]-(p2:Node {nodeid: {second_parent_id}}))
return p;
這將通過您想要的網橋將路徑從一個“根”返回到另一個。 然后,您可以使用路徑函數提取所需的任何節點。 請注意,由於您的數據模型,當前無法進行此查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.