1

I'm doing some tests on redisgraph and I'm wondering what is the best practice to match nodes if I have a hierarchy like this:
category => post => comment => reply
I have index on each label _id field and I match according to the _id
First approach: by matching all the way to the target node:

GRAPH.QUERY test "MATCH (:category {_id:1})-[:post]->(:post {_id:1})-[comment_rel_1:comment]->(c1:comment {_id:1}) SET c1.comment = 'changed'"

Second approach: matching the node directly

GRAPH.QUERY test "MATCH (c1:comment {_id:1}) SET c1.comment = 'changed'"

In case of a huge database with a lot of nodes and edges what approach considered to be time efficient?
Thanks

Rawhi
  • 6,155
  • 8
  • 36
  • 57

1 Answers1

2

Here are the execution plans for each of the queries

127.0.0.1:6379> graph.explain test "MATCH (:category {_id:1})-[:post]->(:post {_id:1})-[comment_rel_1:comment]->(c1:comment {_id:1}) SET c1.comment = 'changed'"
1) "Update"
2) "    Filter"
3) "        Conditional Traverse | (anon_2:post)->(c1:comment)"
4) "            Filter"
5) "                Conditional Traverse | (anon_0:category)->(anon_2:post)"
6) "                    Index Scan | (anon_0:category)"

127.0.0.1:6379> graph.explain test "MATCH (c1:comment {_id:1}) SET c1.comment = 'changed'"
1) "Update"
2) "    Index Scan | (c1:comment)"

In case that you just want to update the comment node, regardless to its state in the graph, the second query is more efficient. In case you want to update only the comment node that is connected in the pattern that you described, the first query is the way to go, between the two.

  • Thanks for your answer, anyway every node has a unique ID so in both cases you'll get to the same node, so I guess its always better to match the node directly? My concern is that if I match the node directly it will scan all nodes with the same label, but if I match the pattern then it will only scan the connected nodes (this is an assumption) – Rawhi Apr 28 '20 at 15:26
  • 1
    I didn't completely understand your comment so I'll try to answer as best as I understand: I assume that this node is connected as in the above pattern so although both queries are semantically different you will get the same result. You mentioned that you are using an index on this combination of label and property. You will invoke an index scan (as seen in the `GRAPH.EXPLAIN` result) rather than a complete label scan. Is that answer your question? – Dvir Dukhan Apr 29 '20 at 19:53