一次插入某些记录

时间:2020-05-05 23:22:48

标签: sql oracle

请问我如何一次在全局临时表中插入1000条记录,如果我一次插入50-60k条记录,查询将变得很慢。请检查下面的插入查询以获取更多信息。谢谢

Create or replace PROCEDURE Employee(

emp_refno IN CLOB

)

AS

Begin
with inputs ( str ) as (  
       select to_clob(emp_refno )
       from dual
     ),
     prep ( s, n, token, st_pos, end_pos ) as (
       select ',' || str || ',', -1, null, null, 1
         from inputs
       union all
       select s, n+1, substr(s, st_pos, end_pos - st_pos),
              end_pos + 1, instr(s, ',', 1, n+3)
         from prep
         where end_pos != 0
     )
INSERT into GlobalTemp_EMP   
select token from prep;
commit;
OPEN p_resultset FOR


select e.empname, e.empaddress, f.department
from employee e
join department f on e.emp_id = t.emp_id
and e.emp_refno  in (SELECT emp_refno from GlobalTemp_EMP) //using GTT In subquery

1 个答案:

答案 0 :(得分:2)

您可以使用BULK COLLECT and LIMIT

将以下内容用于您的要求
   CREATE OR replace PROCEDURE employee( emp_refno IN CLOB ) AS 
    c_limit PLS_INTEGER := 1000;

      CURSOR token_cur IS
        WITH inputs
             (
                  str
             )
             AS
             (
                    SELECT to_clob(emp_refno)
                    FROM   dual
             )
      ,
      prep
      (
           s,
           n,
           token,
           st_pos,
           end_pos
      )
      AS
      (
             SELECT ','
                           || str
                           || ',',
                    -1,
                    NULL,
                    NULL,
                    1
             FROM   inputs
             UNION ALL
             SELECT s,
                    n + 1,
                    substr(s, st_pos, end_pos - st_pos),
                    end_pos + 1,
                    instr(s, ',', 1, n + 3)
             FROM   prep
             WHERE  end_pos != 0
      )
    SELECT token
    FROM   prep
    WHERE  n > 0;

    TYPE token_t
    IS
      TABLE OF CLOB;
      rec_token_t TOKEN_T;
    BEGIN
      OPEN token_cur;
      LOOP
        FETCH token_cur bulk collect
        INTO  rec_token_t limit c_limit;
        EXIT WHEN rec_token_t.count = 0;
        --IF rec_token_t.count > 0 THEN
          forall rec IN rec_token_t.first ..rec_token_t.last
          INSERT INTO globaltemp_emp VALUES
                      (
                                  rec_token_t(rec)
                      );

          COMMIT;
        --END IF;
        --EXIT
      --WHEN rec_token_t.count = 0;
      END LOOP;
      OPEN p_resultset FOR
      SELECT e.empname,
             e.empaddress,
             f.department
      FROM   employee e
      join   department f
      ON     e.emp_id = t.emp_id
      AND    e.emp_refno IN
             (
                    SELECT emp_refno
                    FROM   globaltemp_emp) //USING gtt IN subquery
    END;