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字段索引

 

 

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