Explore the query execution summary

After all results coming from a query have been processed, the server ends the transaction by returning a summary of execution. It comes as a ResultSummary object, and it contains information among which:

  • Query counters — What changes the query triggered on the server

  • Query execution plan — How the database would execute (or executed) the query

  • Notifications — Extra information raised by the server while running the query

  • Timing information and query request summary

Retrieve the execution summary

When running queries with Driver.execute_query(), the execution summary is part of the default return object, under the summary key.

records, result_summary, keys = driver.execute_query("""
    UNWIND ["Alice", "Bob"] AS name
    MERGE (p:Person {name: name})
    """, database_="neo4j",
)
# or result_summary = driver.execute_query('<QUERY>').summary

If you are using transaction functions, or a custom transformer with Driver.execute_query(), you can retrieve the query execution summary with the method Result.consume(). Notice that once you ask for the execution summary, the result stream is exhausted. This means that any record which has not yet been processed is not available any longer.

def create_people(tx):
    result = tx.run("""
        UNWIND ["Alice", "Bob"] AS name
        MERGE (p:Person {name: name})
    """)
    return result.consume()

with driver.session(database="neo4j") as session:
    result_summary = session.execute_write(create_people)

Query counters

The property ResultSummary.counters contains counters for the operations that a query triggered (as a SummaryCounters object).

Insert some data and display the query counters
summary = driver.execute_query("""
    MERGE (p:Person {name: $name})
    MERGE (p)-[:KNOWS]->(:Person {name: $friend})
    """, name="Mark", friend="Bob",
    database_="neo4j",
).summary
print(summary.counters)
"""
{'_contains_updates': True, 'labels_added': 2, 'relationships_created': 1,
 'nodes_created': 2, 'properties_set': 2}
"""

There are two additional boolean properties which act as meta-counters:

  • contains_updates — whether the query triggered any write operation on the database on which it ran

  • contains_system_updates — whether the query updated the system database

Query execution plan

If you prefix a query with EXPLAIN, the server will return the plan it would use to run the query, but will not actually run it. The plan is then available under the property ResultSummary.plan, and contains the list of Cypher operators that would be used to retrieve the result set. You may use this information to locate potential bottlenecks or room for performance improvements (for example through the creation of indexes).

_, summary, _ = driver.execute_query("EXPLAIN MATCH (p {name: $name}) RETURN p", name="Alice")
print(summary.plan['args']['string-representation'])

"""
Planner COST
Runtime PIPELINED
Runtime version 5.0
Batch size 128

+-----------------+----------------+----------------+---------------------+
| Operator        | Details        | Estimated Rows | Pipeline            |
+-----------------+----------------+----------------+---------------------+
| +ProduceResults | p              |              1 |                     |
| |               +----------------+----------------+                     |
| +Filter         | p.name = $name |              1 |                     |
| |               +----------------+----------------+                     |
| +AllNodesScan   | p              |             10 | Fused in Pipeline 0 |
+-----------------+----------------+----------------+---------------------+

Total database accesses: ?
"""

If you instead prefix a query with the keyword PROFILE, the server will return the execution plan it has used to run the query, together with profiler statistics. This includes the list of operators that were used and additional profiling information about each intermediate step. The plan is available under the property ResultSummary.profile. Notice that the query is also run, so the result object also contains any result records.

records, summary, _ = driver.execute_query("PROFILE MATCH (p {name: $name}) RETURN p", name="Alice")
print(summary.profile['args']['string-representation'])

"""
Planner COST
Runtime PIPELINED
Runtime version 5.0
Batch size 128

+-----------------+----------------+----------------+------+---------+----------------+------------------------+-----------+---------------------+
| Operator        | Details        | Estimated Rows | Rows | DB Hits | Memory (Bytes) | Page Cache Hits/Misses | Time (ms) | Pipeline            |
+-----------------+----------------+----------------+------+---------+----------------+------------------------+-----------+---------------------+
| +ProduceResults | p              |              1 |    1 |       3 |                |                        |           |                     |
| |               +----------------+----------------+------+---------+----------------+                        |           |                     |
| +Filter         | p.name = $name |              1 |    1 |       4 |                |                        |           |                     |
| |               +----------------+----------------+------+---------+----------------+                        |           |                     |
| +AllNodesScan   | p              |             10 |    4 |       5 |            120 |                 9160/0 |   108.923 | Fused in Pipeline 0 |
+-----------------+----------------+----------------+------+---------+----------------+------------------------+-----------+---------------------+

Total database accesses: 12, total allocated memory: 184
"""

For more information and examples, see Basic query tuning.

Notifications

After executing a query, the server can return notifications alongside the query result. Notifications contain recommendations for performance improvements, warnings about the usage of deprecated features, and other hints about sub-optimal usage of Neo4j.

For driver version >= 5.25 and server version >= 5.23, two forms of notifications are available (Neo4j status codes and GQL status codes). For earlier versions, only Neo4j status codes are available.
GQL status codes are planned to supersede Neo4j status codes.
Example 1. An unbounded shortest path raises a performance notification

The property ResultSummary.summary_notifications contains a list of SummaryNotification objects.

records, summary, keys = driver.execute_query("""
    MATCH p=shortestPath((:Person {name: 'Alice'})-[*]->(:Person {name: 'Bob'}))
    RETURN p
    """, database_="neo4j"
)
"""
[SummaryNotification(
    title='The provided pattern is unbounded, consider adding an upper limit to the number of node hops.',
    code='Neo.ClientNotification.Statement.UnboundedVariableLengthPattern',
    description='Using shortest path with an unbounded pattern will likely result in long execution times. It is recommended to use an upper limit to the number of node hops in your pattern.',
    severity_level=<NotificationSeverity.INFORMATION: 'INFORMATION'>,
    category=<NotificationCategory.PERFORMANCE: 'PERFORMANCE'>,
    raw_severity_level='INFORMATION',
    raw_category='PERFORMANCE',
    position=SummaryNotificationPosition(line=1, column=22, offset=21)
)]
"""

With version >= 5.25, the property ResultSummary.gql_status_objects contains a sequence of GqlStatusObjects. These are GQL-compliant status objects.

Some (but not all) GqlStatusObjects are notifications, whereas some report an outcome status: 00000 for "success", 02000 for "no data", and 00001 for "omitted result". ResultSummary.gql_status_objects always contains at least one entry, containing the outcome status.

records, summary, keys = driver.execute_query("""
    MATCH p=shortestPath((:Person {name: 'Alice'})-[*]->(:Person {name: 'Bob'}))
    RETURN p
    """, database_="neo4j"
)
for status in summary.gql_status_objects:
    print("GQLSTATUS:", status.gql_status)
    print("Description:", status.status_description)
    # Not all statuses are notifications.
    print("Is notification:", status.is_notification)

    # Notification and thus vendor-specific fields.
    # These fields are only meaningful for notifications.
    if status.is_notification:
        # The position in the query that caused the notification.
        print("Position:", status.position)

        # The notification's classification is counterpart to `neo4j.NotificationCategory`.
        # However, the term `category` has a different meaning in the context of GQL.
        print("Classification:", status.classification)
        print("Unparsed classification:", status.raw_classification)

        print("Severity:", status.severity)
        print("Unparsed severity:", status.raw_severity)

    # Any raw extra information provided by the DBMS:
    print("Diagnostic record:", status.diagnostic_record)
    print("=" * 80)
"""
GQLSTATUS: 02000
Description: note: no data
Is notification: False
Diagnostic record: {'OPERATION': '', 'OPERATION_CODE': '0', 'CURRENT_SCHEMA': '/'}
================================================================================
GQLSTATUS: 03N91
Description: info: unbounded variable length pattern. The provided pattern `(:Person {name: 'Alice'})-[*]->(:Person {name: 'Bob'})` is unbounded. Shortest path with an unbounded pattern may result in long execution times. Use an upper limit (e.g. `[*..5]`) on the number of node hops in your pattern.
Is notification: True
Position: line: 1, column: 22, offset: 21
Classification: NotificationClassification.PERFORMANCE
Unparsed classification: PERFORMANCE
Severity: NotificationSeverity.INFORMATION
Unparsed severity: INFORMATION
Diagnostic record: {'_classification': 'PERFORMANCE', '_status_parameters': {'pat': "(:Person {name: 'Alice'})-[*]->(:Person {name: 'Bob'})"}, '_severity': 'INFORMATION', '_position': {'offset': 21, 'line': 1, 'column': 22}, 'OPERATION': '', 'OPERATION_CODE': '0', 'CURRENT_SCHEMA': '/'}
================================================================================
"""

Filter notifications

By default, the server analyses each query for all categories and severity of notifications. Starting from version 5.7, you can use the parameters notifications_min_severity and/or notifications_disabled_categories/notifications_disabled_classifications to restrict the severity and/or category/classification of notifications that you are interested into. There is a slight performance gain in restricting the amount of notifications the server is allowed to raise.

The severity filter applies to both Neo4j and GQL notifications. Category and classification filters exist separately only due to the discrepancy in lexicon between GQL and Neo4j; both filters affect either form of notification though, so you should use only one of them. If you provide both a category and a classification filter, their contents will be merged. You can use any of those parameters either when creating a Driver instance, or when creating a session.

You can disable notifications altogether by setting the minimum severity to 'OFF'.

Allow only WARNING notifications, but not of HINT or GENERIC category
# at driver level
driver = neo4j.GraphDatabase.driver(
    URI, auth=AUTH,
    notifications_min_severity='WARNING',  # or 'OFF' to disable entirely
    notifications_disabled_classifications=['HINT', 'GENERIC'],  # filters categories as well
)

# at session level
session = driver.session(
    database="neo4j",
    notifications_min_severity='INFORMATION',  # or 'OFF' to disable entirely
    notifications_disabled_classifications=['HINT']  # filters categories as well
)