快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Oraclecasewhen改写SQL

目前创新互联已为上1000家的企业提供了网站建设、域名、网页空间、网站托管运营、企业网站设计、鄠邑网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

Oracle case when 改写 SQL

--- 说明:案例来自《 收获,不止SQL 优化

创建测试数据:

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

结论: SQL 改写后 T2 表访问次数由 6 次降到 1 次,逻辑读 consistent gets 由 320100 降到 2580 ,性能有所提升。

欢迎关注我的微信公众号"IT小Chen",共同学习,共同成长!!!

Oracle case when改写SQL


新闻名称:Oraclecasewhen改写SQL
分享链接:http://6mz.cn/article/jdegjg.html

其他资讯