I have multiple relationships that stem from a node. Each of these relationships are of the same label. The relationships point to a child node (not neccesarily unique). Aft
Try keeping the child nodes in linked lists. The structure will look something like
(p:Parent)-[r1:CHILDREN]->(c1:Child)-[r2:NEXT]->(c2:Child)-[r3:NEXT]->(c3:Child)
This maintains the order of your child nodes and allows you to improve your interaction with the structure in two ways:
Inserting a new node into this structure only involves changes to the relationships 'before' and 'after' the place that node is going, as opposed to the entire structure. For example, to insert newc
between c1
and c2
, you delete r2
, create newc
and create :NEXT
relationships from c1
to newc
to c2
. Analogous for other operations: all your alterations are now local within the structure.
You use relationships and their types to structure your data, instead of relationship properties. This is more flexible and almost always more performant (sometimes much more performant).
To read a single child from this structure you now use your trueindex
to declare the depth in the linked list at which to find the node, i.e.
MATCH (parent:Parent {parentId: 1234})-[:CHILDREN]->()-[:NEXT*3]->(child)
RETURN child
and to retrieve a parent with all its children
MATCH (parent:Parent {parentId: 1234})-[:CHILDREN|NEXT*]->(child)
RETURN parent, COLLECT(child) as children