You're going to have to clean up your query, right now you're not using indexes (so the initial match with the specific name is slow), and then you perform a cartesian product against all :User nodes, then create strings for each row.
So first, create an index on :USER(name) so you can find your start node fast.
Then we'll have to clean up the rest of the match.
Try something like this instead:
MATCH (n:USER) WHERE n.name = "name"
WITH n, "2000" as number
MATCH (n)<-[:CREATED_BY]-(:COMMENT)-[:HAS]->(l:LIKE)-[:CREATED_BY]->(o:User)
RETURN n, o, number, count(l)
You should see a similar plan with this query as in the query without the "2000".
The reason for this is that although your plan has a cartesian product with your match to o
, the planner was intelligent enough to realize there was an additional restriction for o
in that it had to occur in the pattern in your last match, and its optimization for that situation let you avoid performing a cartesian product.
Introduction of a new variable number
, however, prevented the planner from recognizing that this was basically the same situation, so the planner did not optimize out the cartesian product.
For now, try to be explicit about how you want the query to be performed, and try to avoid cartesian products in your queries.
In this particular case, it's important to realize that when you have MATCH (o:User)
on the third line, that's not declaring that the type of o
is a :User in the later match, it's instead saying that for every row in your results so far, perform a cartesian product against all :User nodes, and then for each of those user nodes, see which ones exist in the pattern provided. That is a lot of unnecessary work, compared to simply expanding the pattern provided and getting whatever :User nodes you find at the other end of the pattern.
EDIT
As far as getting both :LIKE and :DISLIKE node counts, maybe try something like this:
MATCH (n:USER) WHERE n.name = "name"
WITH n, "2000" as number
MATCH (n)<-[:CREATED_BY]-(:COMMENT)-[:HAS]->(likeDislike)-[:CREATED_BY]->(o:User)
WITH n, o, number, head(labels(likeDislike)) as type, count(likeDislike) as cnt
WITH n, o, number, CASE WHEN type = "LIKE" THEN cnt END as likeCount, CASE WHEN type = "DISLIKE" THEN cnt END as dislikeCount
RETURN n, o, number, sum(likeCount) as likeCount, sum(dislikeCount) as dislikeCount
Assuming you still need that number
variable in there.