我为我的所有关系分配了一个temp属性,但是与大约1,900个关系却花费了很多时间。我只想通过这样做来获得更好的性能。
TraversalDescription tempTraversal = this.database.traversalDescription()
.depthFirst()
.uniqueness(Uniqueness.RELATIONSHIP_GLOBAL);
String uuidString = "flw-"+uuid.toString();
Transaction tx = database.beginTx();
try {
for(Relationship r : tempTraversal.traverse(this.nSource)
.relationships()){
if (r.hasProperty("weight"))
r.setProperty(uuidString,r.getProperty("weight"));
}
tx.success();
} catch (Exception e) {
System.err.println("assingTempProperty: " + e);
tx.failure();
} finally {
tx.close();
}
有更好的解决方案吗?
最佳答案
好的,我调查了一下并学到了一些东西。
如果您有一个稀疏连接的图并应用关系唯一性,则必须探索非常长的路径才能真正找到图中的所有唯一关系,因此您必须来回走动,直到找到最后一个全局唯一关系为止。
您可以沿着一条路径探索整个图形。
为了对此进行编号,我生成了一个包含400个节点且连接了10%的节点的图形。
您最终得到的最大路径长度为:15845,探索1 288 374条路径。
这是我使用的代码:
@Test
public void testTraversal() throws Exception {
GraphDatabaseService db = new TestGraphDatabaseFactory().newImpermanentDatabase();
List<Node> nodes = new ArrayList<>();
int count=0;
long now = System.currentTimeMillis();
try (Transaction tx = db.beginTx()) {
for (int i=0;i<400;i++) {
nodes.add(db.createNode());
}
DynamicRelationshipType knows = DynamicRelationshipType.withName("KNOWS");
for (Node node1 : nodes) {
for (Node node2 : nodes) {
double random = Math.random();
if (random < 0.1 && node1 != node2) {
node1.createRelationshipTo(node2, knows).setProperty("weight",random);
count++;
}
}
}
tx.success();
}
System.out.println("generated rel-count = " + count+" time "+(System.currentTimeMillis()-now)+" ms");
now = System.currentTimeMillis();
String uuidString = "flw-"+ now;
count=0;
try (Transaction tx = db.beginTx()) {
for (Relationship r : GlobalGraphOperations.at(db).getAllRelationships()) {
if (r.hasProperty("weight"))
r.setProperty(uuidString, r.getProperty("weight"));
count++;
}
tx.success();
}
System.out.println("global graph ops rel-count = " + count+" time "+(System.currentTimeMillis()-now)+" ms");
final AtomicInteger pathLenght=new AtomicInteger();
final AtomicInteger pathCount=new AtomicInteger();
TraversalDescription tempTraversal = db.traversalDescription()
.depthFirst()
.uniqueness(new UniquenessFactory() {
@Override
public UniquenessFilter create(Object optionalParameter) {
return new UniquenessFilter() {
Set<Relationship> rels = new HashSet<Relationship>(100000);
@Override
public boolean checkFirst(TraversalBranch branch) {
pathCount.incrementAndGet();
pathLenght.set(Math.max(pathLenght.get(),branch.length()));
return rels.add(branch.lastRelationship());
}
@Override
public boolean check(TraversalBranch branch) {
pathCount.incrementAndGet();
pathLenght.set(Math.max(pathLenght.get(),branch.length()));
return rels.add(branch.lastRelationship());
}
};
}
}
);
now = System.currentTimeMillis();
Transaction tx = db.beginTx();
count=0;
try {
for(Relationship r : tempTraversal.traverse(nodes.get(0))
.relationships()){
if (r.hasProperty("weight"))
r.setProperty(uuidString,r.getProperty("weight"));
count++;
}
tx.success();
} catch (Exception e) {
System.err.println("assingTempProperty: " + e);
tx.failure();
} finally {
tx.close();
}
System.out.println("rel-count = " + count+" time "+(System.currentTimeMillis()-now)+" ms pathlength "+pathLenght.get()+" pathCount "+pathCount.get());
}
关于java - 如何使Neo4j遍历更快?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23795798/