日期:2014-05-16  浏览次数:20914 次

帮忙优化SQL!提速就结贴!
select c.tskillgrp_code, nvl(sum(case when b.composite_timeout='0' and b.handle_timeout = '0' and b.reply_timeout = '0' and b.archive_timeout = '0' then 1 end),0) notTimeoutCount,nvl(sum(case when b.composite_timeout = '1' then 1 else 0 end),0) compositeCount,nvl(sum(case when b.handle_timeout = '1' then 1 else 0 end),0) handleCount,nvl(sum(case when b.sreply_timeout = '1' then 1 else 0 end),0) replyCount,nvl(sum(case when b.archive_timeout = '1' then 1 else 0 end),0) archiveCount,nvl(count(t.case_no),0) allCount from table_one t,table_two b,table_three c where t.case_no = b.case_no and t.case_no = c.case_no and c.deal_flag = '3' group by c.tskillgrp_code

每个表中都有三百万以上的数据,所以关联查询起来相当慢,所以请帮忙优化一下,谢谢!

------解决方案--------------------
JScript code

select c.tskillgrp_code,
       nvl(sum(case
                 when b.composite_timeout = '0' and b.handle_timeout = '0' and
                      b.reply_timeout = '0' and b.archive_timeout = '0' then
                  1
               end),
           0) notTimeoutCount,
       nvl(sum(case
                 when b.composite_timeout = '1' then
                  1
                 else
                  0
               end),
           0) compositeCount,
       nvl(sum(case
                 when b.handle_timeout = '1' then
                  1
                 else
                  0
               end),
           0) handleCount,
       nvl(sum(case
                 when b.sreply_timeout = '1' then
                  1
                 else
                  0
               end),
           0) replyCount,
       nvl(sum(case
                 when b.archive_timeout = '1' then
                  1
                 else
                  0
               end),
           0) archiveCount,
       nvl(count(t.case_no), 0) allCount
  from table_one t, table_two b, table_three c
 where t.case_no = b.case_no
   and t.case_no = c.case_no
   and c.deal_flag = '3'
 group by c.tskillgrp_code

------解决方案--------------------
额,表的索引页不经建太多,建多了也影响查询速度的。
SQL code
select d.tskillgrp_code,
       sum(notTimeout) notTimeoutCount,
       sum(composite) compositeCount,
       sum(handleCount) handleCount,
       sum(reply) replyCount,
       sum(archive) archiveCount,
       count(t.case_no) allCount
  from (select c.tskillgrp_code,
               case
                 when b.composite_timeout = '0' and b.handle_timeout = '0' and
                      b.reply_timeout = '' 0
                  '' and b.archive_timeout = '' 0 '' then
                  1
               end notTimeout,
               case
                 when b.composite_timeout = '1' then
                  1
                 else
                  0
               end composite,
               case
                 when b.handle_timeout = '' 1 '' then
                  1
                 else
                  0
               end handle,
               case
                 when b.sreply_timeout = '' 1 '' then
                  1
                 else
                  0
               end reply,
               case
                 when b.archive_timeout = '1' then
                  1
                 else
                  0
               end archive,
               t.case_no
          from table_one t, table_two b, table_three c
         where t.case_no = b.case_no
           and t.case_no = c.case_no
           and c.deal_flag = '3') d
 group by d.tskillgrp_code