简体   繁体   中英

Oracle SQL: How to replace distinct with where exists

I've read that using a where exists clause could usually be more efficient than writing select distinct. How could I rewrite the below 2 queries using the where exists condition? Not sure if query2 is eligible for this clause or this only applies to joins.

Query 1:

SELECT DISTINCT
    e.field1,
    regexp_substr(substr(TRIM(d.field2), 1, 2), '[A-Za-z]+', 1, 1) postal_group
FROM
    table1   e
    JOIN table1   f ON f.field0 = e.field0
    JOIN table2   g ON g.field3 = f.field3
    JOIN table3   g ON g.field4 = f.field4
    JOIN table4   a ON a.field5 = g.field5
    JOIN table5   b ON ( b.field6 = a.field6
                       AND b.field7 = a.field7 )
    JOIN table6   c ON ( c.field8 = b.field8
                       AND c.field9 = b.field9 )
    JOIN table7   d ON ( d.field10 = c.field10
                       AND d.field11 = c.field11 )

Query 2:

SELECT DISTINCT
    field
FROM
    table1
WHERE
    condition = 'value'

For your second query you can use group by clause to avoid DISTINCT .

SELECT field
FROM
    table1
WHERE
    condition = 'value'
Group by field

Please try below query with where exists instead of Distinct:

SELECT 
    e.field1,
    regexp_substr(substr(TRIM(d.field2), 1, 2), '[A-Za-z]+', 1, 1) postal_group
FROM
    table1   e
    where exists 
    (
        select 1 from table1   f 
        JOIN table2   g ON g.field3 = f.field3
        JOIN table3   g ON g.field4 = f.field4
        JOIN table4   a ON a.field5 = g.field5
        JOIN table5   b ON ( b.field6 = a.field6
                           AND b.field7 = a.field7 )
        JOIN table6   c ON ( c.field8 = b.field8
                           AND c.field9 = b.field9 )
        JOIN table7   d ON ( d.field10 = c.field10
                           AND d.field11 = c.field11 )
        where f.field0 = e.field0
    )

You can't avoid a join with d because you need a column from it. Therefore the best you can do is:

select e.column1
     , d.column2 as postal_group
from   table1 e
       join table7 d
            on  d.column10 = c.column10
            and d.column11 = c.column11
where  exists
       ( select 1 from table1 f
                join table2 g on g.column3 = f.column3
                join table3 h on h.column4 = f.column4
                join table4 a on a.column5 = h.column5
                join table5 b on b.column6 = a.column6 and b.column7 = a.column7
                join table6 c on c.column8 = b.column8 and c.column9 = b.column9
         where  f.column0 = e.column0 );

You only need distinct if table7.column2 can have duplicate values for a (column10, column11) combination.

This may or may not be more efficient than the original version - compare timings, execution plans, reads etc.

(I've renamed 'fields' as columns because that's what tables have. Also there are no brackets in a join clause and adding them tends to confuse code formatters.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM