Prev |  | Next

## 9.7. Using

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 starting 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 filter 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 have been used, and instead do a label scan.

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

### 9.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[3]{name:"Andres",age:36,awesome:true,surname:"Taylor"}`

### 9.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[1]{name:"Stefan",surname:"Plantikow"}`

### 9.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 its work by finding all `:German` labeled nodes and filtering them by the surname property.

Result

m
1 row
`Node[1]{name:"Stefan",surname:"Plantikow"}`

Copyright © 2014 Neo Technology

Prev | Up | Next
| Home |