EXPLAIN and PROFILE
EXPLAIN / PROFILE
Create the relevant index first. A parameter used by the query still needs a value from your driver or embedding API.
Prefix any query with EXPLAIN to see the chosen plan without executing it.
The plan lists operators and, for each MATCH anchor, the scan strategy the
planner picked (index seek / label scan / all-nodes scan). The anchor may be
either end of the pattern — the operator line shows the scan of the end the
planner chose.
EXPLAIN MATCH (p:Person {email: $e}) RETURN p;
// Match: NodeIndexSeek(:Person .email)
// ProduceResults
PROFILE <query> runs the query — including its writes — and returns, in
the result summary (summary.profile in a driver), one entry per top-level
clause in execution order: the operator as EXPLAIN names it, the rows it
produced, the anchor candidates it examined, the nodes and relationships whose
properties it loaded, and its time. Work done inside a CALL { … } or subquery
expression is counted under the clause that runs it. EXPLAIN returns its plan
in summary.plan as well as rows.
PROFILE MATCH (p:Person) WHERE p.age > 30 RETURN p;
// Match: NodeByLabelScan(:Person) rows 120 candidates 5000
// ProduceResults rows 120 materialised 120
How the planner chooses
From version 1.2.0:
-
The smallest index seek is used. When several indexes could find a pattern's starting nodes (an equality, a range, an
INlist, a list element), they are compared by how many nodes each finds, so a broad equality no longer hides a narrow range. Sizes are known for literals always, and for parameters when the query runs;EXPLAINdoes not evaluate parameters, so a parameterised query may use a different index than it shows.EXPLAIN MATCH (p:Person) WHERE p.active = true AND p.score >= 990 RETURN p; // Match: NodeIndexSeekByRange(:Person .score) -
Pattern order does not matter. The comma-separated patterns of one
MATCHare matched selective-first when they share a variable or theWHERErelates them, soMATCH (a:Person), (b:Person {id: $id}) WHERE a.manager = b.idfindsbfirst however it is written. WithoutORDER BYthe rows of such a query may arrive in a different order than in earlier versions. -
Aggregates use constant memory per group.
count,sum,avg,minandmaxkeep a running value;collectand theDISTINCTforms keep the values they return or have seen.
How a match runs
From version 2.4.1:
- Identity reads no properties.
id(n),elementId(n),labels(n),type(r),n:Label,n IS NULLanda = bon nodes and relationships read the element's record.count(DISTINCT n),RETURN DISTINCT nand grouping by a node or relationship go by identity, and load the element once for each result row. ANDandORstop when the left side decides. Put the cheaper or more selective condition first. A right side that can raise an error (arithmetic, a function call,IN) is always evaluated, so a statement that reported an error still reports it.
From version 2.4.2:
- A filter is tested as soon as its variables are bound. In
MATCH (a:Person)-[:KNOWS]->(b) WHERE a.group = 3, a person outside group 3 is dropped before any of their relationships are read. This applies to conditions joined byANDthat compare properties, parameters, literals and labels, in the clause'sWHEREand in inline(n WHERE …)predicates. When a filter includes arithmetic, a function call orIN, the pattern is filtered on complete matches. - Name the relationship type. An expansion reads each relationship's type and other end from the node's adjacency, and on the Paged engine a pattern that names one type skips the entries of every other type.
- Shortest paths search by node. See
shortestPath.
Related articles
Indexes and constraints · Procedure directory · Transactions and concurrency