Optimize and execute sorting operators

Updated at:

PolarDB-X provides three sorting operators — MemSort, TopN, and MergeSort — to handle ORDER BY queries. Knowing which operator appears in your execution plan lets you assess query performance and spot optimization opportunities.

How sorting operators work

When PolarDB-X executes an ORDER BY query, the query optimizer selects a sorting operator based on the query structure:

  • MemSort — used when sorting cannot be pushed down to the MySQL storage layer (for example, when joining multiple tables)

  • TopN — used when both ORDER BY and LIMIT appear in the same query; manages a list of top N rows sorted by the ORDER BY columns

  • MergeSort — used when the sort can be pushed down to MySQL shards; PolarDB-X merges the pre-sorted shard results rather than re-sorting them

MemSort and TopN run on the PolarDB-X compute layer. MergeSort also runs on the compute layer but relies on sorting already done by MySQL shards — similar to how the Gather operator collects distributed results.

Sorting operators

MemSort

MemSort sorts all input rows in memory using the quicksort algorithm. It runs on the PolarDB-X compute layer and is used when the sort cannot be pushed down — for example, when the ORDER BY columns span multiple joined tables.

explain select t1.name from t1 join t2 on t1.id = t2.id order by t1.name, t2.name;
Project(name="name")
  MemSort(sort="name ASC,name0 ASC")
    Project(name="name", name0="name0")
      BKAJoin(condition="id = id", type="inner")
        Gather(concurrent=true)
          LogicalView(tables="t1", shardCount=2, sql="SELECT `id`, `name` FROM `t1` AS `t1`")
        Gather(concurrent=true)
          LogicalView(tables="t2_[0-3]", shardCount=4, sql="SELECT `id`, `name` FROM `t2` AS `t2` WHERE (`id` IN ('?'))")

The sort columns t1.name and t2.name come from two different tables. Because sorting depends on columns from both sides of the join, it cannot be pushed down to either shard — MemSort runs on the compute layer after the join completes.

If MemSort appears in your plan: consider whether the query can be rewritten as a single-table query with ORDER BY. If so, PolarDB-X will use MergeSort instead, which reduces memory usage on the compute layer.

TopN

When a query uses both ORDER BY and LIMIT, PolarDB-X uses TopN instead of MemSort. TopN manages a list of top N rows: all input rows are sorted based on the columns specified in the ORDER BY clause in ascending or descending order, and after all input rows are processed, the top N rows are returned as the result. In some cases, fewer than N rows are returned.

explain select t1.name from t1 join t2 on t1.id = t2.id order by t1.name, t2.name limit 10;
Project(name="name")
  TopN(sort="name ASC,name0 ASC", offset=0, fetch=?0)
    Project(name="name", name0="name0")
      BKAJoin(condition="id = id", type="inner")
        Gather(concurrent=true)
          LogicalView(tables="t1", shardCount=2, sql="SELECT `id`, `name` FROM `t1` AS `t1`")
        Gather(concurrent=true)
          LogicalView(tables="t2_[0-3]", shardCount=4, sql="SELECT `id`, `name` FROM `t2` AS `t2` WHERE (`id` IN ('?'))")

Adding LIMIT 10 changes the plan from MemSort to TopN. The TopN operator processes all rows from the join and returns only the top 10 rows per the ORDER BY specification.

MergeSort

When an ORDER BY query targets a single table — or when sort columns can be resolved entirely at the shard level — the query optimizer pushes the sort down to each MySQL shard. Each shard returns its rows pre-sorted, and PolarDB-X runs MergeSort to merge the sorted streams. MergeSort is order-aware, similar to how Gather collects distributed results.

explain select name from t1 order by name;
MergeSort(sort="name ASC")
  LogicalView(tables="t1", shardCount=2, sql="SELECT `name` FROM `t1` AS `t1` ORDER BY `name`")

The ORDER BY name clause is pushed into the LogicalView SQL, so each shard sorts its own data. MergeSort on the compute layer only needs to merge two already-sorted streams, consuming less memory than MemSort and offloading sorting work to the MySQL storage layer.

If MergeSort appears in your plan: the query is using the most efficient sorting path. The sort itself requires no further optimization.

Operator selection summary

ScenarioOperatorWhere sorting runs
ORDER BY on a single tableMergeSortMySQL shards + compute layer merge
ORDER BY across joined tablesMemSortPolarDB-X compute layer
ORDER BY + LIMIT across joined tablesTopNPolarDB-X compute layer

To move from MemSort to MergeSort, ensure your ORDER BY columns come from a single table and the query does not require cross-shard joins for sorting.