0
votes

I'm new to Neo4j Community Edition 3.1.0 and I'm trying to create an efficient Cypher query that matches a relationship of a certain type (type A) between nodes ONLY if those nodes currently have a (type B) relationship between them. For instance, what I have right now is:

MATCH (n)-[:B]-(m)
WITH n,m
MATCH (n)-[r:A]-(m)
RETURN r

In my graph, there are significantly more (type B) relationships than (type A), so I'm worried that my query will be very inefficient since it is matching all the (type B) relationships first. Is my query correct; and if so, how can I make it more efficient?

1
Have you looked into Cypher's query planner? It will probably already start with the relationship that constrains the result more, but then at least you'd know and be able to investigate changes. See e.g. neo4j.com/blog/tuning-cypher-queriesjonrsharpe

1 Answers

0
votes

I think it is better to have only one query like:

MATCH (n)-[r:A]-(m)-[:B]-(n)
return r

In this way the query planner can consider relation A before relation B.