Uploaded image for project: 'Hibernate OGM'
  1. OGM-1344

Inefficient query for retrieving embedded nodes in Neo4j

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 5.2.0.Beta1
    • Fix Version/s: 5.2.0.CR1
    • Component/s: neo4j
    • Labels:
      None
    • Environment:
      hibernate.ogm.datastore.provider=neo4j_bolt

      Description

      When accessing the Neo4j datastore via hibernate.ogm.datastore.provider=neo4j_bolt, the runtime complexity of the query for BoltNeo4jEntityQueries.getFindEntityQuery() depends on how many nodes are reachable in the overall graph by following the outgoing relations of the searched node. This bad runtime complexity seems to be caused by the query pattern that is added to the findEntityQuery by BaseNeo4jEntityQueries.appendOptionalMatchOwnerEmbeddedNodes().

      Given the following Entity class

      @Entity
      public class Node {
      
          @Id
          private UUID uuid;
      
          @OneToOne(fetch = FetchType.LAZY)
          private Node previousNode;
      
          @OneToOne(mappedBy = "previousNode")
          private Node nextNode;
      
          //getter, setter ...
      

      the findEntityQuery in BoltNeo4jEntityQueries is as follows:

      MATCH (owner:ENTITY:Node {uuid: {0}}) OPTIONAL MATCH (owner) -[r*]-> emb:EMBEDDED) RETURN owner, r
      

      The runtime complexity of this query depends on how many nodes can be reached from the node with uuid: {0} by following the previousNode relationship because the pattern (owner) -[r*]-> emb:EMBEDDED) potentially expands all those previous nodes.

      For example, if we are using the Node entity for basically saving a linked list of nodes, e.g. node1 <- previousNode - node2 <- previousNode - node3 ..., then Neo4J is using the following query plan during executing the findEntityQuery:

      This query plan becomes more and more inefficient, the more nodes are matched by the subpattern (owner)-[r*]->(emb). In case of a linked list of several thousand nodes, when trying to retrieve the last node of the linked list via its uuid, this may lead to query times of the findEntityQuery of several seconds.

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: