sql调整一

一个查询:

open outcurlist for
      select t.*
      from  tbcharge t
      where t.state=-1
      and not exists
      (
        select 1 from
          (
                select t13.ptid  from tbcharge t13
                where
                t13.inserttime>sysdate-5/1440
                group by t13.ptid
                having count(t13.ptid)>2
          union all
              SELECT distinct t1.PTID
                FROM AccountRecharge t1
                WHERE
                 t1.ip in (
                  SELECT t12.ip
                  FROM AccountRecharge t12
                  WHERE
                  t12.inserttime>sysdate-5/1440
                  GROUP BY t12.ip
                  HAVING COUNT(1)>30)
          )a where a.ptid=t.ptid
      );

 

 

问题点:每次查询的时候都要进行过滤

 

调整思路一:单独执行过滤部分

 

for c1 in (
        SELECT   t13.ptid
                FROM tbcharge t13
                WHERE t13.inserttime > SYSDATE - 5 / 1440
        GROUP BY t13.ptid
        HAVING COUNT (t13.ptid) > 2
        UNION ALL
        SELECT DISTINCT t1.ptid
                        FROM accountrecharge t1
                WHERE  t1.ip IN (SELECT   t12.ip
                                        FROM tbcharge t12
                                        WHERE t12.inserttime > SYSDATE - 5 / 1440
                                GROUP BY t12.ip
                                        HAVING COUNT (1) > 30)
        ) loop

 update tbprohibitcharge t
  set updatetime = sysdate
 where t.prohibitptid = c1.ptid;
 if SQL%rowcount =0 then
 insert into tbprohibitcharge
  (prohibitptid,prohibitdesc ,updatetime)
  values
  (c1.ptid,c1.prohitbitdesc,sysdate)  ;
 end if;    
 commit;
end loop;

 

    open outcurlist for
      select t.*
      from  tbcharge t
      where t.state=-1
        and not exists
         (select 1
            from tbcharge t1
           where t.ptid=t1.prohibitptid);

 

 

调整思路继续: 单独通过作业实现过滤部分,避免每次查询都调用过滤

 

调整思路继续:观察分析state字段索引

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章