1. 程式人生 > >db2 exists 和in聯合使用優化方法

db2 exists 和in聯合使用優化方法

  對於exists和in,當exists裡面巢狀in的時候,如果in語句裡面只有一個元素的情況下,這時候索引是有效的.如果多於一個元素,這種情況下索引會失效.下面寫一個簡單的例子來驗證以上說法.

sql1:

select   1 from pm_measurement_recalculation reca
where       
  exists (                      
                    select
                    operating_date
                from
                    PM_MEASUREMENT_READING reading
                where
                    reading.measurement_id in(
                     -- 10000828, 10000670, 10000671,215001368
                   10000828,10000670,10000671
                    )
)

執行計劃圖:(索引失效)

sql2:

select   1 from pm_measurement_recalculation reca
where       
  exists (                      
                    select
                    operating_date
                from
                    PM_MEASUREMENT_READING reading
                where
                    reading.measurement_id in(
                     -- 10000828, 10000670, 10000671,215001368 
                   10000671
                    )
)

執行計劃圖:

準備優化的原sql:

update
        pm_measurement_recalculation reca
    set
        reca.from_operating_date=(    select
            min(tempTable.operating_date) as operating_date   
        from
            (    select
                min(r.operating_date) as operating_date
            from
                pm_measurement_reading r
            where
                r.measurement_id in (
                   10000828, 10000670, 10000671, 215001368
                ) 
            group by
                r.operating_date   ) as tempTable  )  
        where
            reca.measurement_id in (
                10000828, 10000670, 10000671,215001368
            )
            and
   exists  (
 select operating_date from PM_MEASUREMENT_READING reading
                where reading.measurement_id in(
                                                                            10000828, 10000670, 10000671,215001368
                                                                             ) 
                    )

執行計劃圖

優化後的sql:

update
        pm_measurement_recalculation reca
    set
        reca.from_operating_date=(    select
            min(tempTable.operating_date) as operating_date   
        from
            (    select
                min(r.operating_date) as operating_date
            from
                pm_measurement_reading r
            where
                r.measurement_id in (
                   10000828, 10000670, 10000671, 215001368
                ) 
            group by
                r.operating_date   ) as tempTable  )  
           where
            reca.measurement_id in (
                10000828, 10000670, 10000671,215001368
            )
            and
   exists  (
 select operating_date from PM_MEASUREMENT_READING reading
                where reading.measurement_id in(
                     select
                   reading.measurement_id
                   from
                    PM_MEASUREMENT_READING reading
                where
                    reading.measurement_id in(
                       10000828, 10000670, 10000671,215001368
                    )
                    group by reading.measurement_id
                    fetch first 1 rows only
            ) 
                    )
        執行計劃圖: