The distinct hint is used to guide the optimizer to take specific actions when processing queries with the DISTINCT keyword.
Hint type |
Description |
|---|---|
USE_HASH_DISTINCT |
Enables the optimizer to use the HASH algorithm for executing DISTINCT operations. Its reverse operation is the NO_USE_HASH_DISTINCT hint. |
NO_USE_HASH_DISTINCT |
Disables the optimizer from using the HASH algorithm for executing DISTINCT operations. Its reverse operation is the USE_HASH_DISTINCT hint. |
DISTINCT_PUSHDOWN |
Advises the optimizer to allow DISTINCT pushdown in a distributed environment. Its reverse operation is the NO_DISTINCT_PUSHDOWN hint. |
NO_DISTINCT_PUSHDOWN |
Advises the optimizer to prohibit 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 the distinct operation. The opposite 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 optimizer from using the HASH algorithm for the DISTINCT operation. 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 counterpart 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. By default, the DISTINCT operation is pushed down if no hint is specified.
-- 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 `DISTINCT` operations 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 |
+---------------------------------------------------------------------------+
27 rows in set
The PQ_DISTINCT hint
The PQ_DISTINCT hint specifies how data for the DISTINCT operator is distributed during distributed execution. It is similar to the PQ_GBY hint, but it controls the DISTINCT (deduplication) operator rather than the GROUP BY operator. If this hint is not specified, the optimizer chooses the distribution method for the DISTINCT operator based on the following rules: If the deduplication key includes the table's partitioning key, deduplication can be completed within each partition without redistribution, and the plan consists of a single HASH DISTINCT operator (partition-wise). Otherwise, data is redistributed via HASH based on the deduplication key, with HASH DISTINCT executed on both sides of the distribution (two-stage deduplication). The PQ_DISTINCT hint allows you to explicitly specify the distribution method.
Syntax
/*+ PQ_DISTINCT( [ @qb_name ] distribute_method ) */
27 rows in set
## Parameters
| Parameter | Description |
|------|------|
| qb_name | Optional. The name of the query block defined by the `QB_NAME` hint in the statement. |
| distribute_method | The distribution method for the `DISTINCT` operator. Valid values:<br>- `BASIC`: Allows the optimizer to choose the distribution method, which is the default behavior when the `PQ_DISTINCT` hint is not specified.<br>- `NONE`: Partition-wise distribution. Deduplication is performed independently within each partition without cross-partition redistribution.<br>- `HASH`: Data is redistributed via HASH based on the deduplication key, with `HASH DISTINCT` executed on both sides of the distribution (two-stage deduplication).<br>- `LOCAL`: Executes according to the local data distribution method of the node.<br>- `HASH_LOCAL`: Performs HASH-based redistribution within the node based on the deduplication key. In the plan, this is represented as `EXCHANGE OUT DISTR (HASH LOCAL)`. |
<main id="notice" type='explain'>
<h4>Note</h4>
<p>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 <code>USE_HASH_DISTINCT</code>). Unlike the <code>PQ_GBY</code> hint, the <code>PQ_DISTINCT</code> hint does not support the <code>dop</code> parameter. If the specified distribution method is not feasible (for example, specifying <code>NONE</code> for a deduplication key that does not include the partitioning key), the optimizer ignores the hint. You can confirm whether the hint was accepted through the <code>Used Hint</code> field in the <code>EXPLAIN EXTENDED</code> output.</p>
</main>
## Examples
In the following example, the `customer_orders` table is partitioned by HASH on the `cust_id` column. By default, when deduplicating the partitioning key `cust_id`, the plan completes deduplication within each partition (partition-wise, single `HASH DISTINCT`). After using `PQ_DISTINCT(HASH)`, the plan is forced to use the two-stage deduplication method with HASH-based redistribution.
```sql
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;
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 the two-stage deduplication method 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,