The DISTINCT hint specifies how the optimizer should handle queries that contain the DISTINCT keyword.
Hint type |
Description |
|---|---|
USE_HASH_DISTINCT |
Enables the optimizer to use the HASH algorithm to execute the DISTINCT operation. Its reverse operation is the NO_USE_HASH_DISTINCT hint. |
NO_USE_HASH_DISTINCT |
Disables the optimizer from using the HASH algorithm when executing the DISTINCT operation. Its reverse operation is the USE_HASH_DISTINCT hint. |
DISTINCT_PUSHDOWN |
Indicates that the optimizer can perform DISTINCT pushdown in a distributed environment. Its reverse operation is the NO_DISTINCT_PUSHDOWN hint. |
NO_DISTINCT_PUSHDOWN |
Indicates that the optimizer cannot perform DISTINCT pushdown in a distributed environment. Its reverse operation is the DISTINCT_PUSHDOWN hint. |
PQ_DISTINCT |
Specifies the data distribution method for the DISTINCT operator during distributed execution. |
USE_HASH_DISTINCT Hint
The USE_HASH_DISTINCT hint enables the optimizer to use the HASH algorithm for the DISTINCT operation. Its reverse operation is the NO_USE_HASH_DISTINCT hint.
Syntax
/*+ USE_HASH_DISTINCT [ ( [ @ qb_name ] ) ] */
Examples
The following query example shows how to use the USE_HASH_DISTINCT hint to enable the HASH deduplication algorithm.
-- Use the `USE_HASH_DISTINCT` hint to enable the `HASH` algorithm for `DISTINCT` operations.
EXPLAIN BASIC SELECT /*+ USE_HASH_DISTINCT */ DISTINCT c1, c2, c3 FROM t1;
| Query Plan |
+-------------------------------------------------------------------+
| =========================== |
| |ID|OPERATOR |NAME| |
| --------------------------- |
| |0 |HASH DISTINCT | | |
| |1 |└─TABLE FULL SCAN|T1 | |
| =========================== |
| Outputs & filters: |
| ------------------------------------- |
| 0 - output([T1.C1], [T1.C2], [T1.C3]), filter(nil), rowset=16 |
| distinct([T1.C1], [T1.C2], [T1.C3]) |
| 1 - output([T1.C1], [T1.C2], [T1.C3]), filter(nil), rowset=16 |
| access([T1.C1], [T1.C2], [T1.C3]), partitions(p0) |
| is_index_back=false, is_global_index=false, |
| range_key([T1.__pk_increment]), range(MIN ; MAX)always true |
+-------------------------------------------------------------------+
14 rows in set
## `NO_USE_HASH_DISTINCT` Hint
The `NO_USE_HASH_DISTINCT` Hint disables the specific algorithm used for executing distinct operations. Its reverse operation is the `USE_HASH_DISTINCT` Hint.
### Syntax
```sql
/*+ NO_USE_HASH_DISTINCT [ ( [ @ qb_name ] ) ] */
Examples
The following query example uses the NO_USE_HASH_DISTINCT Hint to disable the HASH algorithm for executing DISTINCT operations. Since OceanBase Database supports both the HASH and MERGE algorithms for deduplication, using the NO_USE_HASH_DISTINCT Hint results in the use of the MERGE algorithm.
-- Use the `NO_USE_HASH_DISTINCT` hint to ensure that the query does not use the `HASH` algorithm but instead uses the `MERGE` algorithm when performing a `DISTINCT` operation.
EXPLAIN BASIC SELECT /*+ NO_USE_HASH_DISTINCT */ DISTINCT c1, c2, c3 FROM t1;
+-------------------------------------------------------------------+
| Query Plan |
+-------------------------------------------------------------------+
| ============================= |
| |ID|OPERATOR |NAME| |
| ----------------------------- |
| |0 |MERGE DISTINCT | | |
| |1 |└─SORT | | |
| |2 | └─TABLE FULL SCAN|T1 | |
| ============================= |
| Outputs & filters: |
| ------------------------------------- |
| 0 - output([T1.C1], [T1.C2], [T1.C3]), filter(nil), rowset=16 |
| distinct([T1.C1], [T1.C2], [T1.C3]) |
| 1 - output([T1.C1], [T1.C2], [T1.C3]), filter(nil), rowset=16 |
| sort_keys([T1.C1, ASC], [T1.C2, ASC], [T1.C3, ASC]) |
| 2 - output([T1.C1], [T1.C2], [T1.C3]), filter(nil), rowset=16 |
| access([T1.C1], [T1.C2], [T1.C3]), partitions(p0) |
| is_index_back=false, is_global_index=false, |
| range_key([T1.__pk_increment]), range(MIN ; MAX)always true |
+-------------------------------------------------------------------+
## `DISTINCT_PUSHDOWN` Hint
The `DISTINCT_PUSHDOWN` hint tells the optimizer to push down the `DISTINCT` operation during distributed execution, allowing for local deduplication at each partition node. Its reverse operation is the `NO_DISTINCT_PUSHDOWN` hint.
### Syntax
```sql
/*+ DISTINCT_PUSHDOWN [ ( [ @ qb_name ] ) ] */
Examples
The following example shows how to use the DISTINCT_PUSHDOWN hint to enable the pushdown of the DISTINCT operation during distributed execution. If no hint is specified, the DISTINCT operation is pushed down by default.
-- Use the `DISTINCT_PUSHDOWN` hint to push down `DISTINCT` operations to partition nodes in distributed queries.
CREATE TABLE tp1(c1 INT, c2 INT, c3 INT) PARTITION BY HASH(c1) PARTITIONS 10;
EXPLAIN BASIC SELECT /*+ DISTINCT_PUSHDOWN */ DISTINCT c2, c3 FROM tp1;
+---------------------------------------------------------------------------+
| Query Plan |
+---------------------------------------------------------------------------+
| =============================================== |
| |ID|OPERATOR |NAME | |
| ----------------------------------------------- |
| |0 |PX COORDINATOR | | |
| |1 |└─EXCHANGE OUT DISTR |:EX10001| |
| |2 | └─HASH DISTINCT | | |
| |3 | └─EXCHANGE IN DISTR | | |
| |4 | └─EXCHANGE OUT DISTR (HASH)|:EX10000| |
| |5 | └─HASH DISTINCT | | |
| |6 | └─PX PARTITION ITERATOR| | |
| |7 | └─TABLE FULL SCAN |TP1 | |
| =============================================== |
| Outputs & filters: |
| ------------------------------------- |
| 0 - output([INTERNAL_FUNCTION(TP1.C2, TP1.C3)]), filter(nil), rowset=16 |
| 1 - output([INTERNAL_FUNCTION(TP1.C2, TP1.C3)]), filter(nil), rowset=16 |
| dop=1 |
| 2 - output([TP1.C2], [TP1.C3]), filter(nil), rowset=16 |
| distinct([TP1.C2], [TP1.C3]) |
| 3 - output([TP1.C2], [TP1.C3]), filter(nil), rowset=16 |
| 4 - output([TP1.C2], [TP1.C3]), filter(nil), rowset=16 |
| (#keys=2, [TP1.C2], [TP1.C3]), dop=1 |
| 5 - output([TP1.C2], [TP1.C3]), filter(nil), rowset=16 |
| distinct([TP1.C2], [TP1.C3]) |
| 6 - output([TP1.C2], [TP1.C3]), filter(nil), rowset=16 |
| force partition granule |
| 7 - output([TP1.C2], [TP1.C3]), filter(nil), rowset=16 |
| access([TP1.C2], [TP1.C3]), partitions(p[0-9]) |
| is_index_back=false, is_global_index=false, |
| range_key([TP1.__pk_increment]), range(MIN ; MAX)always true |
+---------------------------------------------------------------------------+
## `NO_DISTINCT_PUSHDOWN` Hint
The `NO_DISTINCT_PUSHDOWN` hint prevents the optimizer from pushing down the `DISTINCT` operation during distributed execution. Its reverse operation is the `DISTINCT_PUSHDOWN` hint.
### Syntax
```sql
/*+ NO_DISTINCT_PUSHDOWN [ ( [ @ qb_name ] ) ] */
Examples
The following query example uses the NO_DISTINCT_PUSHDOWN hint to prevent the optimizer from pushing down the DISTINCT operation during distributed execution.
-- Use the NO_DISTINCT_PUSHDOWN hint to prohibit pushdown of DISTINCT operations in distributed queries
CREATE TABLE tp1(c1 INT, c2 INT, c3 INT) PARTITION BY HASH(c1) PARTITIONS 10;
EXPLAIN BASIC SELECT /*+ NO_DISTINCT_PUSHDOWN */ DISTINCT c2, c3 FROM tp1;
+---------------------------------------------------------------------------+
| Query Plan |
+---------------------------------------------------------------------------+
| =============================================== |
| |ID|OPERATOR |NAME | |
| ----------------------------------------------- |
| |0 |PX COORDINATOR | | |
| |1 |└─EXCHANGE OUT DISTR |:EX10001| |
| |2 | └─HASH DISTINCT | | |
| |3 | └─EXCHANGE IN DISTR | | |
| |4 | └─EXCHANGE OUT DISTR (HASH)|:EX10000| |
| |5 | └─PX PARTITION ITERATOR | | |
| |6 | └─TABLE FULL SCAN |TP1 | |
| =============================================== |
| Outputs & filters: |
| ------------------------------------- |
| 0 - output([INTERNAL_FUNCTION(TP1.C2, TP1.C3)]), filter(nil), rowset=16 |
| 1 - output([INTERNAL_FUNCTION(TP1.C2, TP1.C3)]), filter(nil), rowset=16 |
| dop=1 |
| 2 - output([TP1.C2], [TP1.C3]), filter(nil), rowset=16 |
| distinct([TP1.C2], [TP1.C3]) |
| 3 - output([TP1.C2], [TP1.C3]), filter(nil), rowset=16 |
| 4 - output([TP1.C2], [TP1.C3]), filter(nil), rowset=16 |
| (#keys=2, [TP1.C2], [TP1.C3]), dop=1 |
| 5 - output([TP1.C2], [TP1.C3]), filter(nil), rowset=16 |
| force partition granule |
| 6 - output([TP1.C2], [TP1.C3]), filter(nil), rowset=16 |
| access([TP1.C2], [TP1.C3]), partitions(p[0-9]) |
| is_index_back=false, is_global_index=false, |
| range_key([TP1.__pk_increment]), range(MIN ; MAX)always true |
+---------------------------------------------------------------------------+
## `PQ_DISTINCT` Hint
`PQ_DISTINCT`The hint is used to specify the distribution during distributed execution.`DISTINCT`The data distribution method of the operator, which is different from`PQ_GBY`Hint is similar, but it controls`DISTINCT`(Deduplication) operator, not`GROUP BY`operator.
When this hint is not specified, the optimizer selects the execution plan based on the following rules:`DISTINCT`Operator distribution method: When the deduplication key contains the table's partitioning key, data deduplication can be completed without redistributing data across partitions. The plan is a single`HASH DISTINCT`partition-wise; otherwise, perform HASH redistribution based on the deduplication key, executing once on each side.`HASH DISTINCT`(Duplication removal in two phases).`PQ_DISTINCT`Hint can force the optimizer to use a specified distribution method.
### Syntax
```sql
/*+ PQ_DISTINCT( [ @qb_name ] distribute_method ) */
Parameters
| Parameter | Description |
| qb_name | Optional. The name of the query block defined by the QB_NAME hint in the statement. |
| distribute_method | The data distribution method for the DISTINCT operator. Valid values:- BASIC: The optimizer automatically chooses the distribution method, which is the behavior when the PQ_DISTINCT hint is not specified.- NONE: Partition-wise distribution. Deduplication is performed independently within each partition without cross-partition redistribution.- HASH: HASH-based redistribution based on the distinct key. HASH DISTINCT is executed twice, once on each side, resulting in a two-phase deduplication process.- LOCAL: Executes according to the local data distribution method of the node.- HASH_LOCAL: HASH-based redistribution based on the distinct key, with redistribution occurring within the node (indicated in the plan as EXCHANGE OUT DISTR (HASH LOCAL)). |
Note
This hint only changes the data distribution method for deduplication; it does not change the deduplication algorithm (HASH deduplication or MERGE deduplication, which are controlled by hints such as USE_HASH_DISTINCT). Unlike the PQ_GBY hint, PQ_DISTINCT does not support the dop parameter. If the specified distribution method is not feasible (for example, specifying NONE for a distinct key that does not contain the partitioning key), the optimizer will ignore this hint. You can confirm whether the hint was accepted through the Used Hint in the EXPLAIN EXTENDED output.
CREATE TABLE customer_orders(cust_id INT, order_id INT) PARTITION BY HASH(cust_id) PARTITIONS 8;
EXPLAIN BASIC SELECT DISTINCT cust_id FROM customer_orders; 27 rows in set The query result is as follows:
==============================================
|ID|OPERATOR |NAME |
----------------------------------------------
|0 |PX COORDINATOR | |
|1 |└─EXCHANGE OUT DISTR |:EX10000 |
|2 | └─PX PARTITION ITERATOR| |
|3 | └─HASH DISTINCT | |
|4 | └─TABLE FULL SCAN |customer_orders|
==============================================
Outputs & filters:
-------------------------------------
0 - output([INTERNAL_FUNCTION(customer_orders.cust_id)]), filter(nil), rowset=16
1 - output([INTERNAL_FUNCTION(customer_orders.cust_id)]), filter(nil), rowset=16
dop=1
2 - output([customer_orders.cust_id]), filter(nil), rowset=16
partition wise, force partition granule
3 - output([customer_orders.cust_id]), filter(nil), rowset=16
distinct([customer_orders.cust_id])
4 - output([customer_orders.cust_id]), filter(nil), rowset=16
access([customer_orders.cust_id]), partitions(p[0-7])
is_index_back=false, is_global_index=false,
range_key([customer_orders.__pk_increment]), range(MIN ; MAX)always true
Use PQ_DISTINCT(HASH) to specify a two-phase deduplication process with HASH-based redistribution:
EXPLAIN BASIC SELECT /*+ PQ_DISTINCT(HASH) */ DISTINCT cust_id FROM customer_orders;
The query result is as follows:
======================================================
|ID|OPERATOR |NAME |
------------------------------------------------------
|0 |PX COORDINATOR | |
|1 |└─EXCHANGE OUT DISTR |:EX10001 |
|2 | └─HASH DISTINCT | |
|3 | └─EXCHANGE IN DISTR | |
|4 | └─EXCHANGE OUT DISTR (HASH)|:EX10000 |
|5 | └─HASH DISTINCT | |
|6 | └─PX PARTITION ITERATOR| |
|7 | └─TABLE FULL SCAN |customer_orders|
======================================================
Outputs & filters:
-------------------------------------
0 - output([INTERNAL_FUNCTION(customer_orders.cust_id)]), filter(nil), rowset=16
1 - output([INTERNAL_FUNCTION(customer_orders.cust_id)]), filter(nil), rowset=16
dop=1
2 - output([customer_orders.cust_id]), filter(nil), rowset=16
distinct([customer_orders.cust_id])
3 - output([customer_orders.cust_id]), filter(nil), rowset=16
4 - output([customer_orders.cust_id]), filter(nil), rowset=16
(#keys=1, [customer_orders.cust_id]), dop=1
5 - output([customer_orders.cust_id]), filter(nil), rowset=16
distinct([customer_orders.cust_id]), partial
6 - output([customer_orders.cust_id]), filter(nil), rowset=16
force partition granule
7 - output([customer_orders.cust_id]), filter(nil), rowset=16
access([customer_orders.cust_id]), partitions(p[0-7])
is_index_back=false, is_global_index=false,