All Products
Search
Document Center

SORT

Last Updated: Jun 18, 2021

The SORT operator enables you to sort the input data.

Example: Sorting the data in table t1, with column c1 sorted in descending order and column c2 in ascending order

obclient>CREATE TABLE t1(c1 INT, c2 INT);
Query OK, 0 rows affected (0.12 sec)

obclient>CREATE TABLE t2(c1 INT, c2 INT);
Query OK, 0 rows affected (0.12 sec)

obclient>EXPLAIN SELECT c1 FROM t1 ORDER BY c1 DESC, c2 ASC\G;
*************************** 1. row ***************************
Query Plan: 
====================================
|ID|OPERATOR   |NAME|EST. ROWS|COST|
------------------------------------
|0 |SORT       |    |3        |40  |
|1 | TABLE SCAN|t1  |3        |37  |
====================================
Outputs & filters: 
-------------------------------------
  0 - output([t1.c1]), filter(nil), sort_keys([t1.c1, DESC], [t1.c2, ASC])
  1 - output([t1.c1], [t1.c2]), filter(nil), 
      access([t1.c1], [t1.c2]), partitions(p0)

In the preceding example, the function of SORT, the No. 0 operator in the execution plan display, is to sort data of table t1. The Outputs & filters section in the execution plan display shows in detail the output information of the SORT operator.

Field

Description

output

The output columns of the operator.

filter

The filter predicates of the operator.

In this example, the condition is set to nil because no filter is configured for the SORT operator.

sort_keys([column, DESC],[column, ASC] ...)

Sorts data by column.

  • DESC: In descending order.

  • ASC: In ascending order.

For example, SORT keys in sort_keys([t1.c1, DESC],[t1.c2, ASC]) are c1 and c2. Column c1 is sorted in descending order and column c2 in ascending order.