In many cases the structure of the domain can be leveraged to improve performance. Let's say that you know that in general your A
entities have less x
relationships compared to the number of y
relationships on the B
entities. Then you could traverse two steps from the A node and see where the B
node shows up, and filter out the C
nodes this way. Here's some code for this approach:
Set<Node> found = new HashSet<Node>();
for ( Relationship firstRel : a1.getRelationships( Reltypes.x, Direction.OUTGOING ) )
{
Node cNode = firstRel.getEndNode();
for ( Relationship secondRel : cNode.getRelationships( Reltypes.y, Direction.INCOMING ) )
{
Node bNode = secondRel.getStartNode();
if ( bNode.equals( b1 ) )
{
found.add( cNode );
break;
}
}
}
Another way would be to start two threads that scan the relationships from either side.
A third approach would be to create a specialized index that would help answering this kind of queries, which would obviously hurt insert performance.