Chief
Chief

Reputation: 23

neo4j not cluing into the multiple labels

My DB consists of two node groups (labels) x and y. y nodes also have additional labels (colors: blue, red, green, etc).

My query is:
MATCH (n1:y)-->(n2:x)<--(n3:blue) RETURN n2.idx

The profile shows an expand for n3 without any reference to it being blue, resulting in 12,000 DB hits, pushing out 12,000 rows. The next stage is a filter for blue, resulting in nearly 24,000 DB hits returning 1,036 rows.

I have constraints on both idx and idy being unique, and I have the index on each of the colors.

I have tried using a color attribute on the y nodes, changing the query to the following, without any difference in the profile.
MATCH (n1:y)-->(n2:x)<--(n3:y {color:blue}) RETURN n2.idx

I've tried using index n3:blue(idy) before the RETURN statement, but that gives me a syntax error. I'm still trying to decypher that (pardon the pun). How can I avoid the stage db hit bloat described above, and have it just start with only blue nodes?

Upvotes: 0

Views: 105

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30407

You can use a scan hint to start using a label scan on n3.

MATCH (n1:y)-->(n2:x)<--(n3:blue) 
USING SCAN n3:blue
RETURN n2.idx

Also, this may produce duplicates if :x nodes can have multiple relationships to :y nodes or :blue nodes. If you only want :x nodes that have a connection to a :y node, then you might try this instead:

MATCH (n2:x)<--(n3:blue) 
USING SCAN n3:blue
WHERE (:y)-->(n2)
WITH DISTINCT n2
RETURN n2.idx

Upvotes: 0

Related Questions