【发布时间】:2014-08-12 18:34:10
【问题描述】:
我有一个带有 200,000 个节点和接近 400 万个关系(两种关系)的索引 neo4j 数据库。使用 C# API,我正在尝试执行“Baconator”类型的查询,它不仅为我提供了最短路径,还为我提供了其他一些较短的路径。 (例如,如果最短路径有 3 个节点,我也想看看长度为 4、5.. 等的其他亚军结果。
这是我首先使用的查询(在 C# 调用中):
string whereClause = "a.Id='" + userId.ToUpper() + "' and b.Id='" + targetId.ToUpper() + "'";
var query = graphClient.Cypher
.Match("p=allShortestPaths((a:Person)-[*..6]-(b:Person))")
.Where(whereClause)
.ReturnDistinct<List<Person>>("nodes(p)").Limit(5);
这只会给我最短长度的路径。为了获得其他较短的路径,我使用以下方法:
//nextDegree starts at the degree of the shortest path
while (nextDegree <= 6)
{
var auxiliaryQuery = graphClient.Cypher
.Match("p=(a:Person)-[*" + (nextDegree) + ".." + (nextDegree) + "]-(b:Person)")
.Where(whereClause)
.ReturnDistinct<List<Person>>("nodes(p)").Limit(limit);
}
这可行,但性能会受到很大影响,因为查询在长度达到 5 时需要 30 多秒,而在达到 6 时需要几分钟。
我的问题是:有没有办法优化它? (通过使用 Cypher 中的简单路径算法或其他方法)
【问题讨论】: