Neo4j 等时线改进

Neo4j Isochrones improvement

我目前正在使用 Neo4j 和 PostGIS 上的等时线。

我在 neo4j 中的问题是我计算等时线的查询效率不高。

match (n:node) where n.id_gis='155'
with n
match path=(n)-[*0..15]-(e)
with e, min(reduce(cost=0.0, r IN rels(path) | cost + toFloat(r.cost2)*3600)) as cost
where cost < 30
return cost, collect(e) as isochrones
order by cost

正如您在上面的代码中看到的,我目前对最大跳数有限制,否则它会在计算最大成本之前搜索我的数据库中的所有可能路径。

有谁知道我如何 change/improve 我的查询,以便它在 "normal" 时间内执行并且不限制关系的数量?

恐怕你不能用 Cypher 做得更好。

不过,使用 traversal framework in Java. See this 2-part blog post comparing the Cypher implementation of Dijkstra's algorithm (similar problem, just with fixed start and end nodes instead of open ended) with APOC's: part 1 and part 2 可以做得更好。

使用遍历,您可以在遍历时计算成本,这样您就可以:

  • 达到最大成本后立即修剪分支
  • 收集端节点迄今为止的最佳成本,如果当前分支的成本低于同一端节点的最佳成本,则修剪该分支

您可以使用 Map<Node, Double> 来收集成本,但如果您的图形足够大,使用原始地图(例如 fastutil, Trove, HPPC, Koloboke 等)将使用更少的内存并且通常更快(更紧凑,更少装箱和拆箱;只需使用节点的 long id 作为键)。

遍历完成后,您只需将地图反转为成本和相关端节点的多重地图即可获得结果。

它比执行 Cypher 查询更复杂,但会快得多。