删除后Neo4j 1.9.9传统索引非常慢

时间:2015-03-03 21:20:18

标签: neo4j spring-data-neo4j

使用Neo4j 1.9.9。 我们运行的一些Cypher查询似乎过于缓慢。一些调查显示:

  • 当我使用以下选项删除200k节点时,我的硬件(MacBook Pro)大约需要2-3秒:

    START n=node(*) DELETE n
    
  • 添加WHERE子句不会显着减慢它的速度

  • 如果使用索引选择节点,则它具有类似的性能,例如

    START n=node:__types__(className="com.e2sd.domain.Comment") DELETE n
    
  • 除了重复上一次测试时,它的速度是20倍或更慢,实际时间从80秒到几百秒不等。更奇怪的是,我是否在同一个JVM中重复测试或启动新程序,或者清除数据库中的所有节点并验证它是否为零节点并不重要。基于索引的删除在任何后续运行的测试上都非常慢,直到我用我的neo4j数据目录打开

     rm -R target/neo4j-test/
    

我将在这里给出一些示例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")
  }
}

我们正在使用此处嵌入的Neo4j以及以下Spring Data配置。

<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.*"/>

为什么在描述的条件下DELETE查询会变慢?

1 个答案:

答案 0 :(得分:2)

您必须专门删除旧索引中的条目,删除节点不足以使其从旧索引中删除。因此,当您第二次运行它时,索引中有400k条目,即使其中一半指向已删除的节点。这样,程序运行缓慢,因为重复运行会扩展索引的大小。

当我写一个neo4j空间的扩展来批量加载RTree时,我遇到了这个问题。我必须使用必须从索引中显式删除的Java API与删除节点分开。很高兴我能提供帮助。

相关问题