Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypher: Use WHERE clause or MATCH property definition for exact match?

In Neo4j (version 3.0), the following queries return the same results:

1. MATCH (a:Label) WHERE a.property = "Something" RETURN a
2. MATCH (a:Label {property: "Something"}) RETURN a

While playing with some large datasets, I noticed (and verified using EXPLAIN and PROFILE) that for some instances, queries like the second one performs better and faster. While other instances exist where both versions performed equally, I didn't yet see one where the first version performed better.

The neo4j documentation and tutorials are also divided. There's no explicit comparison between the two. The docs and tuts use both versions, often leaning towards the first (probably because non-exact matches can only be done using the WHERE clause). But the guidelines also state that the earlier you narrow down the search, the faster the search is.

  1. Am I right that both versions will always return the same results?
  2. Am I right that the second version will often perform better because it narrows down the search earlier?
like image 357
ADTC Avatar asked Oct 19 '22 09:10

ADTC


1 Answers

  1. yes
  2. no, both evaluate basically to the very same query plan. Since Neo4j uses a cost based optimizer the query plan might change over time since the optimizer consider to change (maybe there's now more data so a index lookup is cheaper than a NodeByLabelScan).
like image 106
Stefan Armbruster Avatar answered Oct 27 '22 09:10

Stefan Armbruster