Given a tree (not an arbitrary graph) and a node in the tree then is it possible to find the root node of the tree in an efficient way in memgraph?
At the moment I've created an empty memgraph db in docker, and run:
CREATE INDEX ON :Node(id);
FOREACH (i IN range(0,100000) | CREATE (:Node {id: i}));
match (n1:Node),(n2:Node) where n1.id=n2.id-1 create (n1)-[:PARENT]->(n2);
to create a path. I'm interested in performance comparisons with postgres. For instance, using a WITH RECURSIVE
query in postgres takes 3.4s for a path of length one million on my laptop.
Have I created the path correctly in memgraph? Starting from the node (:Node {id: 1}) what is the best way to follow the parent pointers and get to the root node?
I've tried 'cheating' by doing the following
CREATE INDEX ON :Node(id);
FOREACH (i IN range(0,10000) | CREATE (:Node {id: i, is_child: 'yes'}));
CREATE (:Node {id: 100001, is_child:'no'});
match (n1:Node),(n2:Node) where n1.id=n2.id-1 create (n1)-[:PARENT]->(n2);
match (n:Node)-[:PARENT*]->(root:Node) WHERE n.id=1 AND root.is_child = 'no' RETURN n, root;
but it runs in 7.4s for a path only of length 100,000.