Optimal query: matching on UUID alone v. matching on UUID+:Label+bunch-of-stuff

My question surrounds if there is some kind of optimised hashing on indexes/UUIDs specifically that might mean that other "query narrowing" might be unnecessary.

My nodes use UUID as PK.

My question is: In the case of indexed PK (in this case UUID) is it equally (or possibly more) efficient just to use:

MATCH (n {uuid: 'my-uuid'}) RETURN n
edit: per @andrew.bowman's answer below: need :Label for index to work of course

rather than:

MATCH (n:Label {uuid: 'my-uuid'}) RETURN n
or
MATCH (n:LabelFew:LabelMany {uuid: 'my-uuid'}) RETURN n
or even:
MATCH (n:LabelFew:LabelMany {category: 'my-category', uuid: 'my-uuid'}) RETURN n

Hopefully the question is clear, any advice is appreciated.


Background/discussion:

Generally it seems as though queries are more optimal if the query is "narrowed" using good design decisions (eg using :LabelWithThousandsNodes before :LabelWithMillionsNodes).

BUT indexed queries in some databases are generally extremely optimal (using BST or like) and the number of steps to find the result are very few.

In this case (with other databases that aren't neo4j) adding "query narrowing" might actually make the query be more "expensive". Therefore I'm wondering what the situation is with neo4j ...

You need to have the label present in order to leverage the index, otherwise it turns into the worst-performing AllNodesScan for the lookup.

You can check an EXPLAIN plan of the query to see if indexes are being leveraged.

And if you supply multiple labels, while that will require some additional filtering steps, if a certain index that can be leveraged has fewer nodes for the label, you might see a performance increase as the index lookup may be less expensive.

1 Like

Thank you!

Of course label is necessary for index.

I'd originally written this scenario:

MATCH (n:LabelFew {uuid: 'my-uuid'}) RETURN n

I'll stick with this, unless I encounter performance problems.