13.7. Using

13.7.1. Query using an index hint
13.7.2. Query using multiple index hints
13.7.3. Hinting a label scan

If you do not specify an explicit START clause, Cypher needs to infer where in the graph to start your query. This is done by looking at the WHERE clause and the MATCH clause and using that information to find a useful index.

This index might not be the best choice though — sometimes multiple indexes could be used, and Cypher has picked the wrong one (from a performance point of view).

You can force Cypher to use a specific start point by using the USING clause. This is called giving Cypher an index hint.

If your query matches large parts of an index, it might be faster to scan the label and filtering out nodes that do not match. To do this, you can use USING SCAN. It will force Cypher to not use an index that could be used, and instead do a label scan.

[Note]Note

You cannot use index hints if your query has a START clause.

13.7.1. Query using an index hint

To query using an index hint, use USING INDEX.

Query. 

MATCH n:Swedish
USING INDEX n:Swedish(surname)
WHERE n.surname = 'Taylor'
RETURN n

The query result is returned as usual.

Result

n
1 row

Node[4]{name:"Andres",age:36,awesome:true,surname:"Taylor"}


13.7.2. Query using multiple index hints

To query using multiple index hints, use USING INDEX.

Query. 

MATCH m:German-->n:Swedish
USING INDEX m:German(surname)
USING INDEX n:Swedish(surname)
WHERE m.surname = 'Plantikow' AND n.surname = 'Taylor'
RETURN m

The query result is returned as usual.

Result

m
1 row

Node[2]{name:"Stefan",surname:"Plantikow"}


13.7.3. Hinting a label scan

If the best performance is to be had by scanning all nodes in a label and then filtering on that set, use USING SCAN.

Query. 

MATCH m:German
USING SCAN m:German
WHERE m.surname = 'Plantikow'
RETURN m

This query does it’s work by finding all :German labeled nodes and filtering them by the surname property

Result

m
1 row

Node[2]{name:"Stefan",surname:"Plantikow"}