我的结果检索速度出奇地慢ResourceIterator<Node>
当我从 Java 中的 Cypher 查询执行获得结果时。next()
命令平均需要 156 毫秒,标准偏差为 385!这是预期的行为,还是我做错了什么?有人能建议一种更有效的方法来实现同样的目标吗?
图结构
我有以下图形布局,其中 Point 节点与其他点有 LinksTo 关系:
节点:点
特性:
- idPoint(新样式模式对此属性的唯一约束)
- x(此属性的新样式架构索引)
- y(此属性的新样式架构索引)
关系:链接
特性:
- idLink
- length
(...关系在我的问题中甚至没有发挥作用...)
图表统计:
- 节点数量:890,000
- 关系数量:910,000
Old code
(在 Ubuntu 上使用 Neo4j 2.0.0 stable 和 Oracle Java 7)
(基本上,此代码在给定点周围的 60x60 正方形中搜索节点(点)。)
GraphDatabaseService graphDB = new GraphDatabaseFactory ( ).newEmbeddedDatabase ("points_db");
ExecutionEngine engine = new ExecutionEngine (graphDB);
for (Coordinate c : coords) // coords holds 500 different coordinates
{
int size = 30;
int xMin = c.x - size;
int xMax = c.x + size;
int yMin = c.y - size;
int yMax = c.y + size;
String query = "MATCH (n:POINT) " +
" WHERE n.x > " + xMin +
" AND n.x < " + xMax +
" AND n.y > " + yMin +
" AND n.y < " + yMax +
"RETURN n AS neighbour";
ExecutionResult result = engine.execute (query); // command1
ResourceIterator<Node> ri = result.columnAs ("neighbour"); // command2
while (ri.hasNext ( ))
{
Node n = ri.next ( ); // command3
// ... some code ...
}
}
测量
命令1平均执行时间:7.5毫秒
command2平均执行时间:command3 平均执行时间:156 ms(标准差为 358)
(500次迭代(不同坐标)进行的测量,每次迭代平均找到6个点。测量是可重复的。)
编辑 1 (根据 Luanne 和 Michael 的建议)
带参数化的新的、更快的代码
(在 Ubuntu 上使用 Neo4j 2.0.0 stable 和 Oracle Java 7)
(基本上,此代码在给定点周围的 60x60 正方形中搜索节点(点)。)
GraphDatabaseService graphDB = new GraphDatabaseFactory ( ).newEmbeddedDatabase ("points_db");
ExecutionEngine engine = new ExecutionEngine (graphDB);
Map<String, Object> params = new HashMap<> ( );
int size = 30;
String query = "MATCH (n:POINT) " +
" WHERE n.x > {xMin}" +
" AND n.x < {xMax}" +
" AND n.y > {yMin}" +
" AND n.y < {yMax}" +
" RETURN n AS neighbour";
for (Coordinate c : coords) // coords holds 500 different coordinates
{
params.put ("xMin", (int) c.x - size);
params.put ("xMax", (int) c.x + size);
params.put ("yMin", (int) c.y - size);
params.put ("yMax", (int) c.y + size);
ExecutionResult result = engine.execute (query, params); // command1
ResourceIterator<Node> ri = result.columnAs ("neighbour"); // command2
while (ri.hasNext ( ))
{
Node n = ri.next ( ); // command3
// ... some code ...
}
}
测量
command1平均执行时间:1.7 ms
command2平均执行时间:command3 平均执行时间:112 ms(标准差为 270)(500次迭代(不同坐标)进行的测量,每次迭代平均找到6个点。测量是可重复的。)