ITPub博客

首页 > 数据库 > Oracle > SQL调优真实案例

SQL调优真实案例

原创 Oracle 作者:pxbibm 时间:2015-02-03 17:20:46 0 删除 编辑
 

select a.user_code,

       case

         when b.aa is null then

          0

         else

          b.aa

       end valuecount

  from (select cu.user_code

          from crm_user_role r, crm_user cu

         where r.user_id = cu.user_id

           and r.role_id = 'role8687e756117248a4a264ea2758769d25'

           and cu.user_code in ('1015',

                                '1017',

                                '1021',

                                '1025',

                                '1030',

                                '1031',

                                '1032',

                                '1033')) a,

       (select t.act_user_code, count(*) aa

          from crm_campaign_log t

         where t.act_user_code in ('1015',

                                   '1017',

                                   '1021',

                                   '1025',

                                   '1030',

                                   '1031',

                                   '1032',

                                   '1033')

           and t.log_property = '2'

           and trunc(t.operate_time) >= trunc(sysdate)

         group by t.act_user_code) b

where a.user_code = b.act_user_code(+)

order by valuecount

语句分析:

原SQL语句的执行计划如下:执行的时间大约是5秒钟。Cost需要49014.消耗较多的CPU资源。

 

改进建议:修where条件中的trunc(t.operate_time) >= trunc(sysdate)改为t.operate_time >= to_date(to_char(sysdate,'yyyy-mm-dd'),'yyyy-mm-dd')

select a.user_code,

       case

         when b.aa is null then

          0

         else

          b.aa

       end valuecount

  from (select cu.user_code

          from crm_user_role r, crm_user cu

         where r.user_id = cu.user_id

           and r.role_id = 'role8687e756117248a4a264ea2758769d25'

           and cu.user_code in ('1015',

                                '1017',

                                '1021',

                                '1025',

                                '1030',

                                '1031',

                                '1032',

                                '1033')) a,

       (select t.act_user_code, count(*) aa

          from crm_campaign_log t

         where t.act_user_code in ('1015',

                                   '1017',

                                   '1021',

                                   '1025',

                                   '1030',

                                   '1031',

                                   '1032',

                                   '1033')

           and t.log_property = '2'

           and t.operate_time >= to_date(to_char(sysdate,'yyyy-mm-dd'),'yyyy-mm-dd')

         group by t.act_user_code) b

where a.user_code = b.act_user_code(+)

order by valuecount

修改后的SQL语句执行计划如下:

修改后的语句由原来的执行时间5秒钟减少为0.05秒,cost 有原来的49014 减少到24.

原因是trunc(sysdate)函数把时间转成了字符串,字符串比较时是从左到右逐个字节进行比较,消耗较多CPU资源。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12798004/viewspace-1425333/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
【DBAplus社群】联合发起人。ACOUG核心成员,Oracle用户组年轻专家。熟悉Oracle的体系结构,擅长Oracle数据库架构规划、SQL优化、OWI方面性能的优化。曾服务于航天、航空、机车、大型制造行业数据库的维护以及系统开发工作,开发过基于Oracle数据库的大型ERP系统。
  • 博文量
    341
  • 访问量
    2469814