Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

neo4j cypher Match command concatenation

are these two Chypher statements identical:

//first
match (a)-[r]->(b),b-[r2]->c

//second
match (a)-[r]->(b)
match b-[r2]->c
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The 2 Cypher statements are NOT identical. We can show this by using the PROFILE command, which shows you how the Cypher engine would perform a query.

In the following examples, the queries all end with RETURN a, c, since you cannot have a bare MATCH clause.

As you can see, the first query has a NOT(r == r2) filter that the second query does not. This is because Cypher makes sure that the result of a single MATCH clause does not contain duplicate relationships.

  1. First query

    profile match (a)-[r]->(b),b-[r2]->c return a,c;
    ==> +-----------------------------------------------+
    ==> | a                     | c                     |
    ==> +-----------------------------------------------+
    ==> | Node[1]{name:"World"} | Node[0]{name:"World"} |
    ==> +-----------------------------------------------+
    ==> 1 row
    ==> 2 ms
    ==> 
    ==> Compiler CYPHER 2.3
    ==> 
    ==> Planner COST
    ==> 
    ==> Runtime INTERPRETED
    ==> 
    ==> Projection
    ==>   |
    ==>   +Filter
    ==>     |
    ==>     +Expand(All)(0)
    ==>       |
    ==>       +Expand(All)(1)
    ==>         |
    ==>         +AllNodesScan
    ==> 
    ==> +----------------+---------------+------+--------+----------------+----------------+
    ==> |       Operator | EstimatedRows | Rows | DbHits |    Identifiers |          Other |
    ==> +----------------+---------------+------+--------+----------------+----------------+
    ==> |     Projection |             1 |    1 |      0 | a, b, c, r, r2 |           a; c |
    ==> |         Filter |             1 |    1 |      0 | a, b, c, r, r2 |   NOT(r == r2) |
    ==> | Expand(All)(0) |             1 |    2 |      4 | a, b, c, r, r2 | (b)-[r2:]->(c) |
    ==> | Expand(All)(1) |             2 |    2 |      8 |        a, b, r |  (b)<-[r:]-(a) |
    ==> |   AllNodesScan |             6 |    6 |      7 |              b |                |
    ==> +----------------+---------------+------+--------+----------------+----------------+
    ==> 
    
  2. Second query

    profile match (a)-[r]->(b) match b-[r2]->c return a,c;
    ==> +-----------------------------------------------+
    ==> | a                     | c                     |
    ==> +-----------------------------------------------+
    ==> | Node[1]{name:"World"} | Node[1]{name:"World"} |
    ==> | Node[1]{name:"World"} | Node[0]{name:"World"} |
    ==> +-----------------------------------------------+
    ==> 2 rows
    ==> 2 ms
    ==> 
    ==> Compiler CYPHER 2.3
    ==> 
    ==> Planner COST
    ==> 
    ==> Runtime INTERPRETED
    ==> 
    ==> Projection
    ==>   |
    ==>   +Expand(All)(0)
    ==>     |
    ==>     +Expand(All)(1)
    ==>       |
    ==>       +AllNodesScan
    ==> 
    ==> +----------------+---------------+------+--------+----------------+----------------+
    ==> |       Operator | EstimatedRows | Rows | DbHits |    Identifiers |          Other |
    ==> +----------------+---------------+------+--------+----------------+----------------+
    ==> |     Projection |             1 |    2 |      0 | a, b, c, r, r2 |           a; c |
    ==> | Expand(All)(0) |             1 |    2 |      4 | a, b, c, r, r2 | (b)-[r2:]->(c) |
    ==> | Expand(All)(1) |             2 |    2 |      8 |        a, b, r |  (b)<-[r:]-(a) |
    ==> |   AllNodesScan |             6 |    6 |      7 |              b |                |
    ==> +----------------+---------------+------+--------+----------------+----------------+
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...