更新时间:2025-05-29 GMT+08:00

Unique

算子说明

对下层的数据进行去重处理。在执行过程中,它将会遍历所有输入的数据,对其中的重复记录进行筛选,只保留唯一的记录。

典型场景

关闭enable_hashagg参数,使用带distinct查询。

示例

示例:使用带DISTINCT查询。

-- 数据准备。 
gaussdb=# DROP TABLE IF EXISTS t1;
gaussdb=# CREATE TABLE t1 (id INT , number INT);
CREATE TABLE
gaussdb=# INSERT INTO t1 VALUES(generate_series(1,50), 1);
INSERT 0 50
gaussdb=# INSERT INTO t1 VALUES(generate_series(1,50), 2);
INSERT 0 50

--执行结果。 
gaussdb=# SET enable_hashagg = off;
SET
gaussdb=# EXPLAIN SELECT DISTINCT t1.id FROM t1;
                              QUERY PLAN
----------------------------------------------------------------------
 Streaming (type: GATHER)  (cost=14.18..14.65 rows=26 width=4)
   Node/s: All datanodes
   ->  Unique  (cost=13.37..13.44 rows=26 width=4)
         ->  Sort  (cost=13.37..13.40 rows=21 width=4)
               Sort Key: id
               ->  Seq Scan on t1  (cost=0.00..13.13 rows=20 width=4)
(6 rows)

--删除。
gaussdb=# DROP TABLE IF EXISTS t1;

上述示例中,Unique算子输出信息如下所示。

信息名称

含义

Unique

算子的名称。

Sort

算子的名称。

Sort Key

算子名称,Sort算子排序的依据关键字。示例中为grade。