有一张表tabacc有9000多万条数据,每次delete大约100万条数据,每次delete的时间很长,我用的方法(在存储过程中实现)是--delete /*+ use_hash(a,b) parallel(a,15)*/ from tabacca where exists (select 1 from temptablea b where a.id=b.id and b.type='1');不是分区表,有索引,因执行时间太长,看高手有什么好的办法?请高手指点!
调试欢乐多
declare
maxrows number default 1000;
tb_row_id type%tabacca.row_id;
cursor my_cur is
select a.rowid row_id
from tabacca a where exists (select 1 from temptablea b where a.id=b.id and b.type='1')
order by a.rowid;
begin
open my_cur;
loop
exit when my_cur%NOTFOUND;
fetch my_cur bulk collect into tb_row_id limit maxrows;
forall i in 1..tb_row_id.count
delete tabacca where rowid= tb_row_id(i);
commit;
end loop;
end;
-- 具体请参考:http://topic.csdn.net/u/20110805/09/87d9d494-9105-44f6-8971-a41aeb406922.html?seed=1796660687&r=74789817#r_74789817
SELECT /*+ use_hash(a,b) parallel(a,16) */ a.ROWID FROM tmpaaa a
WHERE EXISTS (SELECT 1 FROM temptable b WHERE a.tmpaaaid=b.appid and b.mType='1') ORDER BY a.ROWID;
EXECUTE IMMEDIATE 'alter table tmpaaa storage(buffer_pool keep)';
OPEN tmp_cur ;
loop
begin
FETCH tmp_cur BULK COLLECT INTO var_rowid LIMIT LimitRows;
FORALL i IN 1 .. var_rowid.COUNT
DELETE FROM tmpaaa WHERE ROWID = var_rowid(i);
commit;
EXCEPTION WHEN OTHERS THEN ROLLBACK;
end ;
exit when tmp_cur%notfound;
end loop;
CLOSE tmp_cur;
execute immediate 'alter table tmpaaa storage(buffer_pool default)';
commit;
WHERE EXISTS (SELECT 1 FROM temptable b WHERE a.tmpaaaid=b.appid and b.mType='1') ORDER BY a.ROWID;看这个能不能想办法优化下
delete /*+ PARALLEL(table ,4)*/ from table where ...尝试后告诉我结果
楼主可以试试多个job分工,同时进行。
比如开10个job,每个job分十万数据,这样可能会快点
DECLARE
x NUMBER;
BEGIN
FOR i IN 0 .. 9
LOOP
DBMS_JOB.submit (
x,
'begin DELETE tabacca t1
WHERE EXISTS
(SELECT 1
FROM (SELECT id
FROM (SELECT SUBSTR (TO_CHAR (ROWNUM), -1) rn,
id
FROM temptablea
WHERE TYPE = ''1'')
WHERE rn = '''
|| i
|| ''') t2
WHERE t2.id = t1.id);COMMIT; end;'
);
END LOOP;
END;
maxrows number default 1000;
tb_row_id type%tabacca.row_id;
cursor my_cur is
select /*+ use_hash(a,b) parallel(a,16) */ a.rowid row_id
from tabacca a
order by a.rowid;
begin
open my_cur;
loop
exit when my_cur%NOTFOUND;
fetch my_cur bulk collect into tb_row_id limit maxrows;
forall i in 1..tb_row_id.count
delete tabacca where id in(select id from temptablea where type='1') and rowid= tb_row_id(i);
commit;
end loop;
end;
for i in 0.. 9