SurrealDB Docs Logo

Enter a search query

EXPLAIN clause

The EXPLAIN clause is used to explain the query plan of a query. It is particularly useful when you want to understand how a query is executed and how it is optimized by the database.

When EXPLAIN is used, the statement returns an explanation, essentially revealing the execution plan to provide transparency and understanding of the query performance.

Syntax

Clause Syntax
@query EXPLAIN [FULL]

Using the EXPLAIN clause in addition to the FULL keyword is expeciallly useful when you want to understand the performance of a query and can provide more details when debugging.

Examples

For example, consider the performance of the following query when the field email is not indexed. We can see that the execution plan will iterate over the whole table.

Index not used
CREATE person:tobie SET name = "Tobie", address = "1 Bagshot Row", email = "tobie@surrealdb.com"; SELECT * FROM person WHERE email='tobie@surrealdb.com' EXPLAIN; SELECT * FROM person WHERE email='tobie@surrealdb.com' EXPLAIN FULL;
Output
-------- Query -------- [ { detail: { table: 'person' }, operation: 'Iterate Table' }, { detail: { type: 'Memory' }, operation: 'Collector' } ] -------- Query -------- [ { detail: { table: 'person' }, operation: 'Iterate Table' }, { detail: { type: 'Memory' }, operation: 'Collector' }, { detail: { count: 1 }, operation: 'Fetch' } ]

On the other hand, here is the result when the field email is indexed. We can see that the execution plan will use the index to retrieve the record.

Index used
DEFINE INDEX fast_email ON TABLE person FIELDS email; CREATE person:tobie SET name = "Tobie", address = "1 Bagshot Row", email = "tobie@surrealdb.com"; SELECT * FROM person WHERE email='tobie@surrealdb.com' EXPLAIN; SELECT * FROM person WHERE email='tobie@surrealdb.com' EXPLAIN FULL;
Output
-------- Query -------- [ { detail: { plan: { index: 'fast_email', operator: '=', value: 'tobie@surrealdb.com' }, table: 'person' }, operation: 'Iterate Index' }, { detail: { type: 'Memory' }, operation: 'Collector' } ] -------- Query -------- [ { detail: { plan: { index: 'fast_email', operator: '=', value: 'tobie@surrealdb.com' }, table: 'person' }, operation: 'Iterate Index' }, { detail: { type: 'Memory' }, operation: 'Collector' }, { detail: { count: 1 }, operation: 'Fetch' } ]