Why would this query cause a Merge Cartesian Join in Oracle

前端 未结 1 1187
别那么骄傲
别那么骄傲 2021-01-19 09:34

I have a query that was recently required to be modified.

Here\'s the original

SELECT RTRIM (position) AS \"POSITION\",
   .  // Other fields
   .
           


        
相关标签:
1条回答
  • 2021-01-19 10:05

    The problem is, Oracle doesn't know that get_fiscal_year_start_date (SYSDATE) returns one single result. So it's assuming that it will generate lots of rows.

    Obviously I don't have a test harness to hand, but this version of your query ought to banish the merge cartesian join.

    SELECT RTRIM (position) AS "POSITION", 
    .  // Other fields 
    . 
    . 
    FROM schema.table x 
         , ( select get_fiscal_year_start_date (SYSDATE) as fiscal_year 
             from dual ) fy
    WHERE hours > 0  
    AND pay = 'RGW' 
    AND NOT EXISTS( SELECT position 
                    FROM  schema.table2 y 
                    where y.date = fy.fiscal_year
                    AND y.position = x.position ) 
    

    Oracle knows that DUAL has a single row, and hence that the sub-query will return one value.

    0 讨论(0)
提交回复
热议问题