

# EXPLAIN

Shows the plan that a member cluster of a database cluster group for Aurora uses for a routed statement.

## Description
<a name="databaseclustergroups-sql-explain-description"></a>

At the routing endpoint, `EXPLAIN` runs on the member cluster that the statement routes to. The routing layer has no planner and no table statistics, so it builds no plan and prints no cost of its own. The plan that you read is the member cluster's plan, in its native form, and it names objects by their cluster names. The routing layer doesn't rewrite cluster names back to router names, so you match a plan node to your query yourself.

By default, the routing layer adds its routing decision to the plan: the member cluster that it selected and the cluster name behind each router name. Use these routing rows to relate a plan node to your query. The routing rows keep the native column layout of the engine, so a client that reads the output by column position reads well-formed rows. Where a tool needs only the native plan, turn the routing rows off with [`dbclustergroup_explain_routing_details`](databaseclustergroups-router-session-variables.md#databaseclustergroups-sysviews-explain-routing-details), for example `SET SESSION dbclustergroup_explain_routing_details = OFF` on Aurora MySQL.

On Aurora MySQL, the routing row appears only in `TRADITIONAL` output, as the first row, before the member cluster's plan: `id` 0, `select_type` `ROUTER`, `table` `router`, and the routing decision in `Extra`. `FORMAT=JSON` and `FORMAT=TREE` return the member cluster's plan with no routing information.

## Syntax
<a name="databaseclustergroups-sql-explain-syntax"></a>

The options are those of the engine's own `EXPLAIN`.

```sql
EXPLAIN [{{options}}] {{routable_statement}};
```

## Parameters and options
<a name="databaseclustergroups-sql-explain-parameters"></a>

The statement takes the following parameters.

{{options}}  
Optional. The output options, as for `EXPLAIN` on a member cluster. On Aurora MySQL, `FORMAT = { TRADITIONAL | JSON | TREE }`.

{{routable\_statement}}  
A statement that the routing layer routes to one member cluster, on an object with any mapping type. For a scaleout table, the statement carries a routing key predicate that resolves it to one member cluster.

## Where it runs and required privileges
<a name="databaseclustergroups-sql-explain-privileges"></a>

Run the statement at the routing endpoint, in a session that isn't pinned. In a pinned session, and on a member cluster endpoint, the member cluster answers `EXPLAIN` itself, with no routing information.

The member cluster checks the member cluster user's privileges on the objects in the statement, as for the statement itself. For the privileges for database cluster groups for Aurora, see [Privileges](databaseclustergroups-sql-reference.md#databaseclustergroups-sql-privileges).

## Usage notes
<a name="databaseclustergroups-sql-explain-usage"></a>

Keep the following in mind when you use the statement.
+ The plan comes from the writer of the member cluster, which runs routed statements.
+ The routing layer doesn't support `EXPLAIN` on a query over a `dbclustergroup_sys` table, because no member cluster holds a plan for it.
+ An `EXPLAIN` whose inner statement spans member clusters returns an error rather than a partial plan.
+ `EXPLAIN ANALYZE` runs the statement on the member cluster, as it does on a direct connection. For a data change statement, it doesn't change any rows. On Aurora MySQL, for a multi-table `UPDATE` or `DELETE`, it runs the plan and shows timing. For a single-table `UPDATE` or `DELETE`, it returns `<not executable by iterator executor>`, with no plan and no timing. For an `INSERT` with `VALUES` or a `REPLACE`, it shows the plan with no timing, and for an `INSERT ... SELECT` it times the `SELECT`. The timing covers the member cluster's execution only, not the time that the routing layer uses.
+ A bare `ANALYZE` with no table has nothing to resolve, and the routing layer refuses an `ANALYZE` of a scaleout table or of tables on more than one member cluster. To analyze a member table, use a pinned session or a direct connection.

`EXPLAIN ANALYZE` works on a scaleout table when a routing key predicate resolves the statement to one member cluster. A statement that doesn't resolve fails as the statement would without `EXPLAIN`.

## Examples
<a name="databaseclustergroups-sql-explain-examples"></a>

The following example explains a read of one key of the scaleout table `app.orders`.

```sql
EXPLAIN SELECT * FROM app.orders WHERE tenant_id = 42 AND order_id = 7;
```

## Errors
<a name="databaseclustergroups-sql-explain-errors"></a>

`EXPLAIN` follows the routing rules of the statement that it wraps, and returns the errors of that statement. For all errors from the routing layer, see [Error codes for database cluster groups for Aurora](databaseclustergroups-error-reference.md).

On Aurora MySQL, the routing layer refuses the following `EXPLAIN` statements with error `1235` (SQLSTATE `42000`) and runs nothing on a member cluster:
+ An `EXPLAIN` or `EXPLAIN ANALYZE` of a statement on a `dbclustergroup_sys` table, including a data mapping write. The message is `This statement type is not supported`, and the write doesn't run.
+ An `EXPLAIN` of a statement on a scaleout table that has no routing key predicate. The message is `Cannot resolve routing target: insufficient routing information`.
+ An `EXPLAIN` of a statement whose routing key values resolve to more than one member cluster. The message says that statements across member clusters aren't supported.

## Related statements
<a name="databaseclustergroups-sql-explain-related"></a>

For more information, see the following:
+ [EXPLAIN ALTER DBCLUSTERGROUP MOVE COLLOCATION](databaseclustergroups-sql-explain-move-collocation.md)
+ [SET router\_cluster\_pin](databaseclustergroups-sql-set-router-cluster-pin.md)