I am trying to profile a query in Neo4j Cypher.
I started from this query:
PROFILE MATCH (t1:Trip{Direction:1, Route:"01"}),(t2:Trip{Direction:0, Route:"01"})
WITH t1, t2 LIMIT 1
MATCH p4 = (t2)−[:STARTS|STOPS|ENDS]−>(:BusStop), p3=(t1)−[:STARTS|STOPS|ENDS]−(:BusStop)
RETURN p3, p4;
and I get the following execution plan:
with a total db hits of 10. The profiling of the query quite fast.
In this webinar:
Tuning Your Cypher: Tips & Tricks for More Effective Queries
at the 32 minute, it suggests to avoid Cartesian Product operations. According to this I transformed the query to the following:
PROFILE MATCH (t1:Trip{Direction:1, Route:"01"})
WITH t1 LIMIT 1
MATCH (t2:Trip{Direction:0, Route:"01"})
WITH t1, t2 LIMIT 1
RETURN t1, t2;
resulting in this execution plan:
with a total db hits of 11: it increased.
Now the profiling shows that the Cartesian product disappeared however the execution seems much slower and the db hits increased.
Why is it slower although the Cartesian product disappeared?
Which query is the better: the one without Cartesian product or the one with fewer db hits? How can I improve my query?
I asked it also on SO: