快速找到Neo4j中的特定路径 [英] Finding Specific Path in Neo4j Quickly

查看:854
本文介绍了快速找到Neo4j中的特定路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经

I've recently asked about how to find all paths between two types os nodes in a way where all the edges in the path had the same attribute (like the same ID). This would be something like:

MATCH (a {type: 'cin1'})-[rels:Next*1.. {value: 1}]->(b {type: 'cancer'}) 
RETURN (a), (b)

在这里,除了具有 value:1 之外,我还会具有 value:所有边缘相同.

where instead of having value: 1 I would have value: same for all edges.

我找到了一种方法来解决此问题(如我的其他问题所回答):

I found a way to solve this by using something like this (as answered in my other question):

MATCH (a:Label {type: 'cin1'})
MATCH (b:Label {type: 'cancer'})
MATCH shortestPath((a)-[rels:Next*1..20]->(b))
WHERE ALL(r in tail(rels) WHERE (head(rels)).value = r.value)
RETURN (a), (b)

我遇到的问题是,这种方法首先创建了所有可能的不同路径,然后对其进行过滤,在我的情况下,这会成倍地增加许多路径.例如,取下图

The problem I'm having is that this approach first create all the possible different paths to then filter them, which in my case creates exponentially many paths. For example, take the following graph

:

给出的方法将首先匹配所有路径:

The approach given will first match all the paths:

id:1 -> id:1 -> id:1 
id:1 -> id:2 -> id:1 
id:1 -> id:1 -> id:2 
id:1 -> id:2 -> id:2 
id:1 -> id:2 -> id:3 
...

然后仅过滤这些选项以返回 1-> 1-> 1 2-> 2-> 2 3-> 3-> 3 ,依此类推.因此,事实证明这种方法非常无效,我想知道是否有更简单的方法.

And only then filter these options to return 1->1->1, 2->2->2, 3->3->3 and so on. Therefore it turns out that this approach is very not effective, and I wonder if there is an easier way.

推荐答案

就优化而言,我将首先研究在起始和结束节点上优化快速匹配的方法,因为这将大大减少扫描的节点.

As far as optimizations, I'd first look at ways to optimize quick matches on start and end nodes, as that should significantly reduce the nodes scanned.

我的直觉是,您可能未在:Label.type上建立索引,并且如果这是与节点进行匹配的主要方式,则可能需要考虑和测试.

My hunch is that you probably aren't indexing on :Label.type, and if that's your primary means of matching to nodes, this is probably something to consider and test.

查看是否可以在开始或结束节点上添加标签也可能很有效.至少看起来:癌症可能会被有效地应用,尽管如果这意味着要成为一个更通用的医疗应用,则这种查询对于许多疾病都需要有效,因此我不确定这是否可行你.

It may also be effective to see if you can add labels on your start or end nodes. At the least it seems like :Cancer could likely be applied effectively, though if this is meant to be a more general medical app this kind of query would need to be effective for many diseases, so I'm not sure if this is feasible for you.

在这种情况下,我认为您最好的选择是优化shortestPath(),并配置查询以确保在计算shortestPath时使用了WHERE ALL子句,而不是在进行详尽搜索后用作过滤器(请参见最短路径

In that case I think your best bet is to optimize the shortestPath(), and PROFILE your query to ensure that your WHERE ALL clause is being used during calculation of the shortestPath, and not as a filter after exhaustive searches (see Shortest Path and Shortest Path Planning). If profiling reveals that it's being used as a filter rather than while constructing shortest path, you'll want to see if you can pick a more efficient predicate shortest path will fail fast for non-matching patterns.

这篇关于快速找到Neo4j中的特定路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆