drop   table  t1 purge ; ..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Oracle case when改寫SQL

發(fā)布時間:2020-08-10 11:59:29 來源:ITPUB博客 閱讀:161 作者:chenoracle 欄目:關(guān)系型數(shù)據(jù)庫

Oracle case when 改寫 SQL

--- 說明:案例來自《 收獲,不止SQL 優(yōu)化

創(chuàng)建測試數(shù)據(jù):

SQL >   drop   table  t1 purge ;

SQL >   drop   table  t2 purge ;

SQL >   create   table  t1 as   select   *   from  dba_objects ;

SQL >   create   table  t2 as   select   *   from  dba_objects ;

SQL >   update  t2 set  status = 'INVALID'   WHERE   ROWNUM <= 10000 ;

SQL >   update  t2 set   generated = 'Y'   WHERE   ROWNUM <= 10000 ;

SQL >   update  t2 set   temporary = 'Y'   WHERE   ROWNUM <= 10000 ;

SQL >   update  t2 set   temporary = 'M'   WHERE   temporary <> 'Y' ;

SQL >   update  t2 set   temporary = 'Q'   WHERE   temporary <> 'Y'   or   temporary <> 'M' ;

SQL >   COMMIT ;

SQL >   set  autotrace traceonly

SQL >   set  linesize 1000    

SQL

SQL>           
select t1.object_name,
       t1.object_id,
       (select count(*)
          from t2
         where temporary = 'Y'
           and t2.object_id = t1.object_id) CNT_TEMPORARY_Y,
       (select count(*)
          from t2
         where created >= sysdate - 365
           and t2.object_id = t1.object_id) CNT_CREATED_NEW,
       (select sum(object_id)
          from t2
         where status <> 'VALUD'
           and t2.object_id = t1.object_id) SUM_OBJID_STATUS_V,
       (select sum(object_id)
          from t2
         where generated = 'Y'
           and t2.object_id = t1.object_id) SUM_OBJID_GENERATED_Y,
       (select sum(object_id)
          from t2
         where generated = 'M'
           and t2.object_id = t1.object_id) SUM_OBJID_GENERATED_M,
       (select sum(object_id)
          from t2
         where generated = 'Q'
           and t2.object_id = t1.object_id) SUM_OBJID_GENERATED_Q
  from t1
 where t1.object_id <= 50;

Oracle case when改寫SQL

Oracle case when改寫SQL

case when改造 后的 SQL

with w_t2 as
(select
t2.object_id,
count(case when t2.temporary='Y' then 1 end ) CNT_TEMPORARY_Y,
count(case when created >=sysdate-365  then 1 end ) CNT_CREATED_NEW,
sum(case when t2.status<>'VALID' then t2.object_id end ) SUM_OBJID_STATUS_V,
sum(case when t2.generated = 'Y' then t2.object_id end ) SUM_OBJID_GENERATED_Y,
sum(case when t2.generated = 'M' then t2.object_id end ) SUM_OBJID_GENERATED_M,
sum(case when t2.generated = 'Q' then t2.object_id end ) SUM_OBJID_GENERATED_Q
from  t2
group by t2.object_id)
select t1.object_name,t1.object_id,w_t2.* from t1,w_t2
where t1.object_id=w_t2.object_id
and t1.object_id<=50;

Oracle case when改寫SQL

Oracle case when改寫SQL

結(jié)論: SQL 改寫后 T2 表訪問次數(shù)由 6 次降到 1 次,邏輯讀 consistent gets 320100 降到 2580 ,性能有所提升。

歡迎關(guān)注我的微信公眾號"IT小Chen",共同學(xué)習(xí),共同成長!??!

Oracle case when改寫SQL

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI