Search code examples
neo4jcypher

Neo4j not exists vs is null


What's the difference between running

MATCH (n) WHERE NOT exists(n.foo) RETURN n

and

MATCH (f) WHERE f.foo IS NULL RETURN f

I ran both queries and got the same result, is the any difference in performance or something else?


Solution

  • Let's create a small example dataset:

    CREATE (n1 {foo: 'bar'}), (n2)
    

    We use PROFILE to show the execution plan and the number of rows in each step.

    For query MATCH (n) WHERE NOT exists(n.foo) RETURN n:

    enter image description here

    For query MATCH (f) WHERE f.foo IS NULL RETURN f:

    enter image description here

    The execution plans show that the number of actual rows (traveling between the processing steps) and the number of database hits are the same. The filter conditions are a bit different, but I would not expect any significant differences - the execution time will likely to be dominated by disk access and, as EJP commented, by network transmission. Of course, to be sure, you would have to run a benchmark on a larger data set.