ORDER BY not using index

From what I've read, ORDER BY should use an existing index. This seems not to be the case, at least with Neo4j Community Edition 5.8.0 which I'm currently evaluating.

I have a very simple model with about ~1 million Item nodes that have a name property.

I've created an index using the following command:
CREATE INDEX item_name FOR (i:Item) ON (i.name)

The following query returns the first 100 name properties in ascending order:
MATCH (n:Item) RETURN n.name ORDER BY n.name LIMIT 100

But the query takes ~300 ms. This feels slow. I expected it to use the item_name index and run much faster.

See attachment for the query plan generated via:
PROFILE MATCH (n:Item) RETURN n.name ORDER BY n.name LIMIT 100

What am I doing wrong? Or is that a limitation of the Community version?

Hello @candritzky and welcome to the Neo4j community :slight_smile:

Can you try this query?

PROFILE MATCH (n:Item) 
WHERE n.name IS NOT NULL 
RETURN n.name 
ORDER BY n.name LIMIT 100

Best regards,
Cobra

Thanks @cobra,

indeed, it works with a WHERE clause on n.name. Can be n.name IS NOT NULL as you suggested, or n.name<>'foobar' or whatever. Query performance is now down to 1 or 2 ms.

Here's the query plan (now using NodeIndexScan instead of NodeByLabelScan).

So what's the trick here? Do I have to use a (dummy) WHERE clause on each of the (indexed) expressions that are part of ORDER BY?

Thanks again,
Chris

Yes, you can find more information here.

Best regards,
Cobra