Default ordering of nodes and relationships


(Vince) #1

One for the engineering team!

I have noticed that when using a NodeByLabelScan - even with a filter - the nodes are always returned by default in strict ascending id order, and with no gaps - that is I don't need to sort by id to get a sorted list of node ids. I'd like to know if this is a guaranteed default behaviour, or is it just a a co-incidence? As illustration of what I mean, these two queries return identical results.

MATCH (n:T) 
WHERE id(n) >= 100
RETURN id(n) 
ORDER by id(n) 
LIMIT 10
MATCH (n:T) 
WHERE id(n) >= 100
RETURN id(n) 
LIMIT 10

(M. David Allen) #2

The behavior you've observed is correct - that is how neo4j works -- but as a matter of software contract I think it's not a good idea to rely on that. Here what I think matters is the semantics of the cypher language itself. If you have no ORDER BY clause, then it is not legitimate to assume the order of the results. Cypher would still be "right" if it returned the records in another order. Under the hood in neo4j, other complicated things are happening (like node ID reuse and packing). Your cypher queries shouldn't need to be aware of things like that, or how changes in them over time could affect the outcome of your queries.

Remember that as new versions are introduced and neo4j goes through changes, if you come to rely on that sort of behavior it might cause problems later, because AFAIK that's not part of the contract of how cypher works.

Explicit ORDER BY is better.

tl;dr yes it would work but I don't recommend it. :slight_smile: