Skip to content

Analyze Query_Neo4j

Faizan Ali edited this page Jan 4, 2021 · 2 revisions

There are two Cypher keywords you can prefix a Cypher statement with to analyze a query:

  • EXPLAIN provides estimates of the graph engine processing that will occur, but does not execute the Cypher statement.

  • PROFILE provides real profiling information for what has occurred in the graph engine during the query and executes the Cypher statement.

Using EXPLAIN:

The EXPLAIN keyword provides the Cypher query plan. A Cypher query plan has operations where rows are processed and passed on to the next operation (step). You can compare different Cypher statements to understand the stages of processing that will occur when the Cypher executes.

Here is an example. We have set the actorName and year parameters for our session:

:params {actorName: 'Hugo Weaving', year: 2000}

Then we execute this Cypher code:

EXPLAIN MATCH (p:Person)-[:ACTED_IN]->(m:Movie) WHERE p.name = $actorName AND m.released < $year RETURN p.name, m.title, m.released

With EXPLAIN, the query does not run, the graph engine simply produces the query plan.

A major goal for a good graph data model and query is one where the number of rows processed is minimized. Because we have an index on the released property of the Movie node, the initial step is simply an index lookup. You want to see the use of indexes in your queries.

For a better metric for analyzing how the Cypher statement will run, you use the PROFILE keyword which runs the Cypher statement and gives you run-time performance metrics.

Cypher query best practices

Here is a very high-level list of some of the best practices you should aim for in your Cypher queries:

  • Create and use indexes effectively.

  • Use parameters rather than literals in your queries.

  • Specify node labels in MATCH clauses.

  • Reduce the number of rows passed processed.

  • Aggregate early in the query, rather than in the RETURN clause, if possible.

  • Use DISTINCT and LIMIT early in the query to reduce the number of rows processed.

  • Defer property access until you really need it.

Source: https://neo4j.com/graphacademy/training-intro-40/15-using-query-best-practices/

Clone this wiki locally