18

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.

Nicole White
  • 7,720
  • 29
  • 31
limc
  • 39,366
  • 20
  • 100
  • 145

1 Answers1

11

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

Stefan Armbruster
  • 39,465
  • 6
  • 87
  • 97
  • Yes, you are right, I didn't know about the ``explain`` command. – Alexander Rodin Aug 20 '15 at 15:33
  • 2
    Very helpful post, thank you. I didn't know I can break up `MATCH` statement with commas. Stumbled upon this article regarding using `EXPLAIN` for debugging purposes: http://neo4j.com/blog/neo4j-detecting-potential-typos-using-explain/ – limc Aug 20 '15 at 16:40