I'm new to Neo4J, and have been learning Cypher query language the past few days.
I realized I can write my query like this...
MATCH (b:Beverage)<-[:likes]-(p:Person)-[:likes]->(r:Restaurant)
WHERE b.name = 'Beer' and r.name = 'KFC'
RETURN p.name
... or like this...
MATCH (b:Beverage{name:'Beer'})<-[:likes]-(p:Person)-[:likes]->(r:Restaurant{name:'KFC'})
RETURN p.name
Which approach is better in terms of performance? And why?
Thank you.
The MATCH clause allows you to specify the patterns Neo4j will search for in the database. This is the primary way of getting data into the current set of bindings. It is worth reading up more on the specification of the patterns themselves in Patterns.
An OPTIONAL MATCH matches patterns against your graph database, just like a MATCH does. The difference is that if no matches are found, OPTIONAL MATCH will use a null for missing parts of the pattern. OPTIONAL MATCH could be considered the Cypher equivalent of the outer join in SQL.
I'm sorry to say, but @a-rodin's reply is wrong here: both of your statements result in the very same query plan. You can verify that by prefixing the statement with EXPLAIN
and comparing the query plans.
For readability I'd structure the query in question:
MATCH (p:Person)-[:likes]->(b:Beverage{name:'Beer'}),
(p)-[:likes]->(r:Restaurant{name:'KFC'})
RETURN p.name
Here the query reads like a sentence in plain English "match a person that likes beer and and like KFC restaurants".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With