+ -
当前位置:首页 → 问答吧 → 同样的SQL语句,为什么效率差别这么大?

同样的SQL语句,为什么效率差别这么大?

时间:2011-10-18

来源:互联网

CREATE OR REPLACE PROCEDURE SP_RPT_MEDIASTAT_T2 
/*
  Description : 短信发送统计报表,供iWeb报表调用
  Author : wfw2527
  Date : 2011-08-17
  Version : 1.0
  Comments :
  */
 AS
  eInfo VARCHAR2(200);
  tSql VARCHAR2(2000);
  V_BEGINPARTID VARCHAR(20);
  V_ENDPARTID VARCHAR(20);
  V_BeginDate VARCHAR(20);
  V_EndDate VARCHAR(20);

BEGIN
  tSql :='insert into t_temp_mscount select getcity(a.sendno) city,
  count(*) AS SENDCOUNT,
  SUM(CASE
  WHEN deliveryreport = ''DELIVRD|000'' THEN
  1
  ELSE
  0
  END) AS SUCCESSCOUNT,
  SUM(CASE
  WHEN deliveryreport = ''DELIVRD|000'' THEN
  0
  ELSE
  1
  END) AS FAILCOUNT,
  to_char(decode(count(*),
  0,
  0,
  round(SUM(CASE
  WHEN deliveryreport = ''DELIVRD|000'' THEN
  1
  ELSE
  0
  END) * 100.0 / count(*),
  2)),
  ''990.99'') || '' % '' AS SUCCESSPER
  from (select *
  from t_ms_media_task t
  where MONTHDAY >= ''1011''
  AND MONTHDAY < ''1017''
  AND SENDTIME >= to_date(''2011-10-11 00:00:00'',''yyyy-mm-dd hh24:mi:ss'')
  AND SENDTIME < to_date(''2011-10-17 00:00:00'',''yyyy-mm-dd hh24:mi:ss'')
  union all
  select *
  from t_ms_media_task_his t
  where MONTHDAY >= ''1011''
  AND MONTHDAY < ''1017''
  AND SENDTIME >= to_date(''2011-10-11 00:00:00'',''yyyy-mm-dd hh24:mi:ss'')
  AND SENDTIME < to_date(''2011-10-17 00:00:00'',''yyyy-mm-dd hh24:mi:ss'')) a,
  SENDADMINUSER.t_send_spinfo b
 where a.spid = b.gid group by getcity(a.sendno)';
 execute immediate tsql;
 commit;
END SP_RPT_MEDIASTAT_T2;
/

作者: bewich   发布时间: 2011-10-18

INSERT INTO t_temp_mscount 
select getcity(a.sendno) city,
  count(*) AS SENDCOUNT,
  SUM(CASE
  WHEN deliveryreport = 'DELIVRD|000' THEN
  1
  ELSE
  0
  END) AS SUCCESSCOUNT,
  SUM(CASE
  WHEN deliveryreport = 'DELIVRD|000' THEN
  0
  ELSE
  1
  END) AS FAILCOUNT,
  to_char(decode(count(*),
  0,
  0,
  round(SUM(CASE
  WHEN deliveryreport = 'DELIVRD|000' THEN
  1
  ELSE
  0
  END) * 100.0 / count(*),
  2)),
  '990.99') || ' % ' AS SUCCESSPER
  from (select *
  from t_ms_media_task t
  where MONTHDAY >= '1011'
  AND MONTHDAY < '1018'
  AND SENDTIME >= to_date('2011-10-11 11:00:00','yyyy-mm-dd hh24:mi:ss')
  AND SENDTIME < to_date('2011-10-18 11:00:00','yyyy-mm-dd hh24:mi:ss')
  union all
  select *
  from t_ms_media_task_his t
  where MONTHDAY >= '1011'
  AND MONTHDAY < '1018'
  AND SENDTIME >= to_date('2011-10-11 11:00:00','yyyy-mm-dd hh24:mi:ss')
  AND SENDTIME < to_date('2011-10-18 11:00:00','yyyy-mm-dd hh24:mi:ss')) a,
  SENDADMINUSER.t_send_spinfo b
 where a.spid = b.gid group by getcity(a.sendno)

作者: bewich   发布时间: 2011-10-18

我test上面的存储过程需要5分钟,而单独执行2楼的SQL语句只要5秒钟。
这是为什么?、、、、、

作者: bewich   发布时间: 2011-10-18

拼sql语句然后执行,和直接执行sql语句,他们在oracle中的解析不一样的。

作者: cyxif   发布时间: 2011-10-19

http://www.itpub.net/thread-843834-1-1.html
看下这个

作者: jym2002   发布时间: 2011-10-19

热门下载

更多