Jon*_*mer 5 neo4j spring-data-neo4j
使用Neo4j 1.9.9.我们运行的一些Cypher查询似乎过于缓慢.一些调查显示:
在我的硬件(MacBook Pro)上删除200k节点大约需要2-3秒,当我选择它们时:
START n=node(*) DELETE n
Run Code Online (Sandbox Code Playgroud)添加WHERE子句不会显着减慢它的速度
如果使用索引选择节点,则它具有类似的性能,例如
START n=node:__types__(className="com.e2sd.domain.Comment") DELETE n
Run Code Online (Sandbox Code Playgroud)除了在重复之前的测试时,它的速度是20倍或更慢,实际时间从80秒到几百秒不等.更奇怪的是,无论我是在同一个JVM中重复测试还是启动新程序,或者清除数据库中的所有节点并验证它是否为零节点都无关紧要.基于索引的删除在任何后续测试运行中都非常慢,直到我破坏了我的neo4j数据目录
rm -R target/neo4j-test/
Run Code Online (Sandbox Code Playgroud)我将在这里给出一些示例Scala代码.我很乐意根据需要提供更多细节.
for (j <- 1 to 3) {
log("Total nodes in database: " + inNeo4j( """ START n=node(*) RETURN COUNT(n) """).to(classOf[Int]).single)
log("Start")
inNeo4j(""" CREATE (x) WITH x FOREACH(i IN RANGE(1, 200000, 1) : CREATE ({__type__: "com.e2sd.domain.Comment"})) """)
rebuildTypesIndex()
log("Created lots of nodes")
val x = inNeo4j(
"""
START n=node:__types__(className="com.e2sd.domain.Comment")
DELETE n
RETURN COUNT(n)
""").to(classOf[Int]).single
log("Deleted x nodes: " + x)
}
// log is a convenience method that prints a string and the time since the last log
// inNeo4j is a convenience method to run a Cypher query
def rebuildTypesIndex(): Unit = {
TransactionUtils.withTransaction(neo4jTemplate) {
log.info("Rebuilding __types__ index...")
val index = neo4jTemplate.getGraphDatabase.getIndex[Node]("__types__")
for (node <- GlobalGraphOperations.at(neo4jTemplate.getGraphDatabaseService).getAllNodes.asScala) {
index.remove(node)
if (node.hasProperty("__type__")) {
val typeProperty = node.getProperty("__type__")
index.add(node, "className", typeProperty)
}
}
log.info("Done")
}
}
Run Code Online (Sandbox Code Playgroud)
我们使用以下Spring Data配置使用Neo4j.
<bean id="graphDbFactory" class="org.neo4j.graphdb.factory.GraphDatabaseFactory"/>
<bean id="graphDatabaseService" scope="singleton" destroy-method="shutdown"
factory-bean="graphDbFactory" factory-method="newEmbeddedDatabase">
<constructor-arg value="target/neo4j-test"/>
</bean>
<neo4j:config graphDatabaseService="graphDatabaseService" base-package="my.package.*"/>
Run Code Online (Sandbox Code Playgroud)
为什么在描述的条件下DELETE查询会变慢?
您必须专门从旧索引中删除条目,删除节点不足以使其从旧索引中删除。因此,当您第二次运行它时,索引中有 400k 条目,即使其中一半指向已删除的节点。通过这种方式,您的程序会很慢,因为重复运行会扩展索引的大小。
当我编写 Neo4j Spatial 扩展来批量加载 RTree 时,我遇到了这个问题。我必须使用 Java API,您必须在删除节点的同时显式地从索引中删除。很高兴我能帮上忙。
归档时间: |
|
查看次数: |
143 次 |
最近记录: |