Hi,
This is regarding the 'Adding Genre nodes challenge' in Graph Data Modeling Fundamentals learning module.
Below is the recomended solution for the question What drama movies did an actor act in?
MATCH (p:Actor)-[:ACTED_IN]-(m:Movie)--(g:Genre)
WHERE p.name = 'Tom Hanks' AND
g.name = 'Drama'
RETURN m.title AS Movie
However, I am wondering whether we can use the below code as well, as it also produces the same result. Is there any particular benefit in the former approach in terms of code efficiency, as its hard to test it with little data.
MATCH (p:Actor)-[:ACTED_IN]-(m:Movie)-[:IN_GENRE]-(g:Genre)
WHERE p.name = 'Tom Hanks' AND
g.name = 'Drama'
RETURN m.title AS Movie
Thank you
They will result in the same result if movie and gene nodes are only related by a IN_GENRE relationship. The first query uses shorthand notation to find a match between two nodes that does not care about the relationship. ()—() is the same as ()--().
Your solution specifies the relationship type, so when the query is executed and the each movie node is expanded to get its corresponding genre nodes, the relationship type between the nodes will need to be checked to be the JN_GENRE type.
1 Like
Thank you, Gary. So, will there be any performance difference between the two queries?
I guess if you have a large number of relationships, then it has to check the type of each when specifying a specific type. This can't be avoid sometimes, as when you have different types that can link to the node you are traversing.
Run an explain plan on the query with and without. You will most likely see a 'filter' operation when the query is expanding the path from the anchor node. You do that by inserting 'explain' in the beginning of the query. The results are access via the new icon presented in the result types, i.e. graph, text, or table.
1 Like