Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to find the distinct values in a nested table

Scott SwankSep 24 2019 — edited Sep 25 2019

Let's start with a nested table

create type tt_int as table of integer;

There are two ways to find its distinct values. One is obvious

SELECT DISTINCT column_value

BULK COLLECT INTO v_result

FROM table(p_values);

And the other less obvious

v_result := p_values MULTISET UNION DISTINCT new tt_int();

At least on Oracle 12.1 it turns out that there is no clear winner between the two. Above 150-200 elements the SQL engine performs better (even considering consistent gets), but below that MULTISET UNION DISTINCT wins out.

    c_empty_ints           CONSTANT tt_int := NEW tt_int();

    c_sql_hash_threshold   CONSTANT PLS_INTEGER := 200;

    FUNCTION distinct_values(p_values IN tt_int)

        RETURN tt_int

    IS

        v_distinct   tt_int;

    BEGIN

        IF p_values.COUNT < c_sql_hash_threshold

        THEN

            v_distinct := p_values MULTISET UNION DISTINCT c_empty_ints;

        ELSE

            SELECT DISTINCT COLUMN_VALUE

              BULK COLLECT INTO v_distinct

              FROM TABLE(p_values);

        END IF;

        RETURN v_distinct;

    END distinct_values;

I'd be interested to hear if this holds with newer releases.

Cheers.

Comments
Post Details
Added on Sep 24 2019
12 comments
2,980 views