Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Cypher's ORDER BY uses the index?

Tags:

neo4j

cypher

I have an index on :Label(Uid) and :Label(Name)

However, when I profile (in the shell) the following two queries I get the same codepath for both. The issue is that I have 700,000 items of :Label, and it's starting to be painfully slow to order the items.

Query ordering by a property with index:

MATCH (item:Label) RETURN item.Name ORDER BY item.Name SKIP 1000 LIMIT 50

Query ordering by a property without index:

MATCH (item:Label) RETURN item.Name ORDER BY item.Created SKIP 1000 LIMIT 50

The profiler gets me (almost) the same thing for both, only the parameters are changed:

==> ColumnFilter(symKeys=["item", "item.Name", "  UNNAMEDS885193287"], returnItemNames=["item.Name"], _rows=30, _db_hits=0)
==> Slice(skip="Literal(1000)", _rows=30, _db_hits=0)
==>   Top(orderBy=["SortItem(Cached(  UNNAMEDS885193287 of type Any),true)"], limit="Add(Literal(1000),Literal(50))", _rows=1030, _db_hits=0)
==>     Extract(symKeys=["item"], exprKeys=["item.Name", "  UNNAMEDS885193287"], _rows=768596, _db_hits=1537192)
==>       NodeByLabel(identifier="item", _db_hits=0, _rows=768596, label="Label", identifiers=["item"], producer="NodeByLabel")
like image 504
Arturo Sevilla Avatar asked May 23 '14 17:05

Arturo Sevilla


People also ask

Does Neo4j have indexes?

There are different types of indexes available in Neo4j but they are not all compatible with the same property predicates. Indexes are commonly used for MATCH and OPTIONAL MATCH clauses that combine a label predicate with a property predicate.

What makes creating a full text schema index different from creating a property index?

What makes creating a full-text schema index different from creating a property index? Full-text schema indexes can use relationship properties. Full-text schema indexes can check for uniqueness. Full-text schema indexes can use multiple types of nodes for the index.

What is unwind in Neo4j?

With UNWIND , you can transform any list back into individual rows.

How do you create a relationship between two nodes in Neo4j?

We can create a relationship using the CREATE clause. We will specify relationship within the square braces “[ ]” depending on the direction of the relationship it is placed between hyphen “ - ” and arrow “ → ” as shown in the following syntax.


2 Answers

As of now, neo4j does not take advantage of an existing index to speed up the ORDER BY clause.

You should probably let the neo4j folks know you would like this to be supported :-).

[UPDATE]

There is, as of this update (Jan. 10, 2018), an open feature request for this capability. According to a recent comment, it is supposed to be a "high prioritized feature".

like image 162
cybersam Avatar answered Oct 05 '22 22:10

cybersam


As of Neo4j 3.4 using indexes for order by is supported. But you have to explain the data type of the property to the query engine. Adding a dummy where clause would be sufficient:

MATCH (item:Label)
WHERE item.Name > ""
RETURN item.Name 
ORDER BY item.Name 
SKIP 1000 
LIMIT 50

For details: https://neo4j.com/docs/cypher-manual/current/query-tuning/cypher-index-values-order/#_index_backed_order_by

like image 32
ahmkara Avatar answered Oct 05 '22 21:10

ahmkara