Search code examples
neo4jtime-complexitycypher

What's the time complexity of query a node in neo4j?


I'm confused how neo4j query work.

If I have 10 nodes in my graph DB (7 Customer nodes + 3 Product nodes)

Now, I want to query a customer node(ex: C1), what's the time complexity?

And how the query work ? Like hash table O(1)? Or like binary search O(logN) ?


Solution

  • Example

    MATCH (p:Person {name: 'Tom Hanks'})
    RETURN p
    

    Result

    Planner COST
    
    Runtime PIPELINED
    
    Runtime version 5.8
    
    Batch size 128
    
    +------------------+----+------------------------+----------------+------+---------+----------------+------------------------+-----------+---------------------+
    | Operator         | Id | Details                | Estimated Rows | Rows | DB Hits | Memory (Bytes) | Page Cache Hits/Misses | Time (ms) | Pipeline            |
    +------------------+----+------------------------+----------------+------+---------+----------------+------------------------+-----------+---------------------+
    | +ProduceResults  |  0 | p                      |              6 |    1 |       3 |                |                        |           |                     |
    | |                +----+------------------------+----------------+------+---------+----------------+                        |           |                     |
    | +Filter          |  1 | p.name = $autostring_0 |              6 |    1 |     250 |                |                        |           |                     |
    | |                +----+------------------------+----------------+------+---------+----------------+                        |           |                     |
    | +NodeByLabelScan |  2 | p:Person               |            125 |  125 |     126 |            120 |                    3/0 |     0.772 | Fused in Pipeline 0 |
    +------------------+----+------------------------+----------------+------+---------+----------------+------------------------+-----------+---------------------+
    
    Total database accesses: 379, total allocated memory: 184
    
    1 row
    

    As you can see, when you search a node, it will scan all nodes which mean the time complexity is O(N).

    However you can create index for node that can improve search faster.

    More info: document