DachuanZhao
DachuanZhao

Reputation: 1339

How to optimize cypher when I want to find a path from a node to another?

I want to optimize cypher because its too slow to get the result.

My code is :

MATCH (e0{name:"dacomitinib"})-[r01]-(e1)-[r12]-(e2)-[r23]-(e3{name:"rucaparib camsylate"})
WHERE (e1:GeneEntity or e1:CompoundEntity or e1:DrugsEntity or e1:DiseaseEntity or e1:ProteinEntity) 
and (e2:GeneEntity or e2:CompoundEntity or e2:DrugsEntity or e2:DiseaseEntity or e2:ProteinEntity) 
RETURN e0.name,r01.confidence,e1.name,r12.confidence,e2.name,r23.confidence,e3.name

What should I do?

update one:

The PROFILE of my code is

enter image description here

Cypher version: CYPHER 3.5, planner: COST, runtime: INTERPRETED. 86876729 total db hits in 53454 ms.

Upvotes: 1

Views: 43

Answers (1)

Rajendra Kadam
Rajendra Kadam

Reputation: 4052

There some ways you can improve the performance of your query.

1. Create Index on name property:

Do the same for the other labels as well.

CREATE INDEX ON :GeneEntity(name)

2. Use labels when matching (Here for e0 and e3): Consider using labels for reducing the nodes to scan. If you don't use labels Neo4j will compare all the nodes.

Solution:

Your query is internally resulting in an AllNodesScan. AllNodesScan this is a bad Idea!.

A better solution could be:

MATCH (e0{name:"dacomitinib"}), (e3{name:"rucaparib camsylate"})
WITH e0, e3 
MATCH (e0)-[r01]-(e1)-[r12]-(e2)-[r23]-(e3) 
WHERE  
    head(labels(e1)) IN ['GeneEntity','CompoundEntity','DrugsEntity','DiseaseEntity','ProteinEntity'] 
    AND 
    head(labels(e2)) IN ['GeneEntity','CompoundEntity','DrugsEntity','DiseaseEntity','ProteinEntity'] 
RETURN e0.name, r01.confidence, e1.name, r12.confidence, e2.name, r23.confidence, e3.name

Upvotes: 1

Related Questions