WEB开发网
开发学院数据库MSSQL Server sql分段处理删除大量数据 阅读

sql分段处理删除大量数据

 2007-11-11 12:17:42 来源:WEB开发网   
核心提示: 最近在QQ群里面,有人问起如何使用分段处理,sql分段处理删除大量数据,竟然忘记如何操作,当时只是COPY一本书里的内容,只记得大致的步骤,先重新温习

 最近在QQ群里面,有人问起如何使用分段处理,竟然忘记如何操作,当时只是COPY一本书里的内容,只记得大致的步骤,先重新温习,以便记忆:

实现功能:

1.分段处理

2.拆分操作

3.批量绑定

--摘自《Oracle(大型网站数据库平台) 数据库性能优化》

CREATE OR REPLACE PROCEDURE del_hubel_SSF_forall
as
 type ridArray is table of rowid index by binary_integer;
 type dtArray is table of varchar2(50) index by binary_integer;
 
 v_rowid ridArray;
 v_mid_to_delete dtArray;
BEGIN
 SELECT mid, rowid bulk_collect into v_mid_to_delete, v_rowid from temp_mid_hubei_bak WHERE ROWNUM<10001;
 FORALL i in 1.. v_mid_to_delete.COUNT
  DELETE FROM  SSF WHERE mid=v_mid_to_delete(i);
  --DBMS_OUTPUT_LINE(TO_CHAR(v_mid_to_delete.COUNT) || ' records deleted from hubei_SSF !!!');
  FORALL i in i .. v_rowid.COUNT
   DELETE FROM temp_mid_hubei_bak WHERE ROWID=v_rowid(i);
  --DBMS_OUTPUT_LINE(TO_CHAR(v_mid_to_delete.COUNT) || ' records deleted from hubei_SSF !!!');
END;


CREATE OR REPLACE PROCEDURE exec_forall
(
 p_RowCount in number,  --Total need to delete rows count
 p_ExeCount in number
)
AS
 n_RowCount number:=0;
 n_ExeTimes number:=0;
 n_Delete number:=0;
BEGIN
 n_RowCount :=p_RowCount;
 
 WHILE n_RowCount>0
 LOOP
  EXECUTE IMMEDIATE 'begin del_hubei_SSF_forall; end;';
  COMMIT;
 
  IF n_RowCount>p_ExeCount THEN
  n_RowCount:=n_RowCount-p_ExeCount;
  n_ExeTime:=n_ExeTimes+1;
  ELSE
  n_ExeTimes :=n_ExeTimes+1;
  n_Delete:=RowCount;
  n_RowCount:=n_RowCount-p_ExeCount;
  END IF;
  --DBMS_OUTPUT_LINE('----------------' || TO_CHAR(n_ExeTimes) || '--------------');
  n_delete :=n_delete+p_ExeCount*(n_ExeTimes-1);
  END LOOP;
  --DBMS_OUTPUT_LINE('Full Finished!!!');
  --DBMS_OUTPUT_LINE('Totally' || TO_CHAR(n_delete) || 'records deleted. !!!');
END;

Tags:sql 分段 处理

编辑录入:coldstar [复制链接] [打 印]
赞助商链接