Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j: Cypher WHERE Clause vs. Curly Braces in MATCH Clause - Which is Better?

Tags:

neo4j

cypher

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.

like image 929
limc Avatar asked Aug 20 '15 12:08

limc


People also ask

Why would you use Neo4j match?

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.

What is optional match in Neo4j?

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.


1 Answers

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".

like image 60
Stefan Armbruster Avatar answered Sep 18 '22 13:09

Stefan Armbruster