Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Improving Queries with EXPLAIN

How to use the GridGain 9 SQL EXPLAIN command to read query execution plans, diagnose common performance issues, and improve query performance.

The SQL EXPLAIN command is a powerful tool used to analyze and understand the execution plan of a query without actually executing it.

When you use the EXPLAIN command, it returns the query execution plan, which includes details such as:

  • The order in which tables are accessed.

  • The type of join operations used (for example, nested loops, hash joins, or merge joins).

  • Any indexes that are used to speed up data retrieval.

  • Estimated costs and row counts for different parts of the query.

This information is crucial for optimizing query performance, identifying bottlenecks, and making informed decisions about database schema design and indexing strategies.

EXPLAIN Command Syntax

GridGain 9 supports two variations of the EXPLAIN command:

EXPLAIN [PLAN | MAPPING] FOR <query>

If neither PLAN nor MAPPING is specified, then PLAN is implicit.

Parameters:

  • PLAN - explains query in terms of relational operators tree. This representation is suitable for investigation of performance issues related to the optimizer.

  • MAPPING - explains query in terms of mapping of query fragment to a particular node of the cluster. This representation is suitable for investigation of performance issues related to the data colocation.

Examples:

EXPLAIN SELECT * FROM lineitem;
EXPLAIN PLAN FOR SELECT * FROM lineitem;
EXPLAIN MAPPING FOR SELECT * FROM lineitem;

Understanding The Output

Each query plan is represented as a tree-like structure composed of relational operators.

A node in the plan includes:

  • A name, indicating the relational operator (e.g., TableScan, IndexScan, Sort, Join types)

  • A set of attributes, relevant to that specific operator

Operator Naming

The operator name reflects the specific algorithm or strategy used. For example:

  • TableScan – Full scan of a base table.

  • IndexScan – Access via index, possibly sorted.

  • Sort – Explicit sorting step.

  • HashJoin, MergeJoin, NestedLoopJoin – Types of join algorithms.

  • Limit, Project, Exchange – Execution-related transformations and controls.

  • SearchBounds - Shows index lookup constraints: searchBounds: <v1, v2, ...> for an exact lookup, [<l1, l2>..<u1, u2>) for a range look up.

Hierarchical Plan Structure

The plan is structured as a tree, where:

  • Leaf nodes represent data sources (e.g., TableScan)

  • Internal nodes represent data transformations (e.g., Join, Sort)

  • The root node (topmost) is the final operator that produces the result

Common Query Optimization Issues

SQL EXPLAIN output analysis can help you optimize slow query execution. You can avoid common bottlenecks in SQL execution by following these guidelines:

  • Avoid scanning an entire table.

  • Avoid scanning non-optimal indexes.

  • Avoid suboptimal join ordering or join algorithm.

  • Ensure optimal data colocation for your queries.

In the following sections, we will see some common issues with queries and ways to identify and fix them.

Full Scan Instead of Index Scan

Suppose related sql execution flow looks like:

And possible EXPLAIN output:

For simplicity, here and below, information that is not related to the example is omitted from the EXPLAIN output.

We can see a full scan (TableScan operator) with predicate. The execution planner chooses which scan implementation (TableScan or IndexScan) to use. If you expect that index scan is preferable, you can use the FORCE_INDEX hint to manually force IndexScan approach:

Will show a different plan, like:

Suboptimal Indexes

Indexes with less prediction can be chosen, for example schema and query may look as follows:

and a possible plan would be:

We can see that the execution uses the T_COL1_IDX index, through both predicates COL1 = '1' AND COL2 = '2' are involved and the T_COL1_COL2_IDX is preferable. In this case, the optimal plan would be:

You can also use the FORCE_INDEX hint to achieve this:

Unexpected Sort Operation

By default, sorted indexes store their entries in ascending order. You can adjust the ordering of a sorted index by including the options ASC or DESC.

Let's suppose the schema and related query look like this:

And the possible execution plan looks like this:

In the example above, the planner adds the Sort operation before performing the IndexScan, as the index used is sorted in descending order, while ascending order is required.

Extra Sort operations adds performance costs, and we can avoid it by creating an index with the appropriate sort ordering:

And plan will no longer display the Sort operation, improving query execution speed:

Performance Impact of Correlated Subqueries

The SQL-99 standard allows for nested subqueries at nearly all places within a query, so Ignite 3 supports nested subqueries, both correlated and not. Performance of certain complex correlated subqueries may be insufficient. Let's consider a correlated query:

We can see nested correlated subquery here, lets check the plan:

The example above shows the slow CorrelatedNestedLoopJoin operation. Queries with this operation may cause a number of issues:

  • Such subqueries may become bottlenecks.

  • Queries can cause high CPU load.

  • Certain queries may perform slower than expected.

If performance issues are found in similar queries, it would be more efficient to rewrite the query without nested subqueries, for example:

And new plan becomes:

Without the CorrelatedNestedLoopJoin operation, the query should perform much better than the previous one.

Excessive Sorting

Lets explain we have an index involved two columns one of them is participate in predicate and other in ordering, or in sql terms:

Expectations:

  • Index need to be used here.

  • No additional sort is needed because index is ordered by name column is satisfies initial query ordering.

But the real execution plan shows a different result:

We can see a redundant Sort operator. A bit query refactoring can help to avoid excessive sorting:

And the plan becomes as follows:

Select Count Optimization

Some queries can be optimized to use more optimal plans which brings performance speed up. For example, plan for:

Can look like this:

But there are numerous cases where such optimization is not applicable. In such a cases, a plan can be different and the execution may require more time.

The same query as above, but with explicit transaction may produce a different plan, for example:

Index Scan Without Exact Search Bounds

Table scans are available in two implementations: direct table scan and scan through index. Index scans contain predicate and search bounds. Predicate provides final rows comparison. If search bounds are absentm the query degenerates into table scan through index scan (requiring an additional store look up), with further predicate comparison that incurs additional performance overhead costs.

Let's suppose we have schema and query like this:

And possible plan would look like this:

We can see here - only predicate (and no searchBounds) which means that all rows from index will go through predicate and bring additional performance penalty.

Two type of solutions are possible here:

  • You can prohibit suboptimal index usage;

  • You can explicitly help the planner with type derivation.

Prohibit Index Usage

For the first approach, use the NO_INDEX hint to prohibit index usage:

As a result, you will have a plan similar to this:

Manual Type Casting

You can append additional cast to the same query to explicitly cast data as a specific type:

We can see here both searchBounds and predicate which means that only exact lookup through index will be involved.

The same case as above but for a bit complicated query:

Possible plan:

And we also can see that no search bounds are involved here.

Try to change it like:

And the possible plan will become:

We can see that searchBounds are present - thus more productive execution flow is expected here.

Colocation Usage

As mentioned above, right colocated columns choice plays a significant role in query execution performance. For example, if initially tables are created without any thoughts about further usage columns colocation you can have the following scenario:

And query as follows:

Bring plan like:

We can see two Exchange operators, which means that all rows are transferred into a single node and then are joined. This execution flow brings a performance cost and slows down query execution.

Let's try to improve it by adding explicit colocation for the dept table by the ID column:

Now the dependent rows from emp table are transferred into the appropriate node where dept holds the rows according to DEPT.ID distribution:

Only one Exchange operator for now, which, once again, mean only rows transferring from emp table to appropriate dept one.

And finally, both join predicate related columns are colocated:

The following colocation example will only work if the emp and dept tables belong to the same distribution zone.

Now, the emp and dept tables are both colocated.

And the final plan will look like this:

No Exchange operators are involved in the explanation, which means that no excessive rows transfer has occurred.

Comprehensive EXPLAIN Examples

Example: Complex Join Query

The resulting output is:

This execution plan represents a query that joins three tables: USERS, REVIEWS, and PRODUCTS, and selects four fields after filtering by product name.

  • Project (root node): Outputs the final selected fields — USERNAME, PRODUCTNAME, REVIEWTEXT, and RATING.

  • HashJoins (two levels): Perform the inner joins.

    • The first (bottom-most) joins REVIEWS with PRODUCTS on PRODUCTID.

    • The second joins the result with USERS on USERID.

  • TableScans: Each table is scanned:

    • REVIEWS is fully scanned.

    • PRODUCTS is scanned with a filter on PRODUCTNAME.

    • USERS is fully scanned.

  • Exchange nodes: Indicate data redistribution between operators.

Each node includes:

  • fieldNames: Output columns at that stage.

  • predicate: Join or filter condition.

  • est: Estimated number of rows at that point in the plan.

Example: Query Mapping

A result of EXPLAIN MAPPING command includes additional metadata providing insight at how the query is mapped on cluster topology. So, for the command like below:

The resulting output is:

Where:

  • Fragment#0 means fragment with id=0

  • A root marks a fragment which is considered as root fragment, i.e. a fragment which represents user's cursor

  • A distribution attribute provides an insight into which mapping strategy was applied to this particular fragment

  • A executionNodes attribute provides a list of nodes this fragment will be executed on

  • A partitions attribute provides an insight into which partitions of which tables will be read from which nodes

  • A tree attribute specifies which part of the relational tree corresponds to this fragment

The output above shows how the query is broken into multiple execution fragments and distributed across the cluster. It gives insight into both the logical execution plan and how it maps to the physical topology.

The query starts execution in Fragment#0, which serves as the root of the plan — this is where the final result is produced. It runs on a single node (node_1) and contains the main logic of the query, including the projection and two nested hash joins. Instead of scanning tables directly, it receives data from other fragments through Receiver operators. These incoming streams correspond to the REVIEWS, PRODUCTS, and USERS tables.

The actual table scans happen in Fragments 1 through 3, each responsible for one of the involved tables. These fragments operate in parallel across the cluster. Each performs a scan on its respective table and then sends the results back to Fragment#0.

  • Fragment#1 handles the REVIEWS table. It runs on all nodes and uses a random distribution strategy. Data is partitioned across nodes, and after scanning the table, results are sent upstream.

  • Fragment#2 is in charge of the PRODUCTS table. It also spans all nodes but follows a zone-based distribution linked to the table's partitioning. There's a filter applied to PRODUCTNAME, which limits the amount of data sent to the root.

  • Fragment#3 covers the USERS table. Like the others, it's distributed and reads from table partitions spread across the cluster.

Each fragment includes metadata such as the nodes it's executed on, how data is partitioned, and how results are sent between fragments. This layout provides a clear view of not only how the query is logically processed, but also how the workload is split and coordinated in a distributed environment.

This page is: Copyright © 2026 MariaDB. All rights reserved.

Last updated

Was this helpful?