Oracle 截断 table 需要很长时间才能处理 3 亿行 table

Oracle truncate table takes to long for 300 million row table

有时截断 table 需要很长时间。 我在 PL/SQL 过程中有以下命令。在此之前我是否需要任何代码或包含在下面的语句中以加快速度。我在截断后将记录重新插入到此 table 中,截断可能约为 500,000。 table 确实有 5 个索引,但没有任何触发器。

 EXECUTE IMMEDIATE 'TRUNCATE TABLE act_plus_triggers';

@ErsinGülbahar 删除的答案可能是正确的 - table 或相关对象可能被其他进程锁定。 TRUNCATE 是一个 DDL 命令,其创建速度几乎比常规 DELETE 快无限。如果该命令在几秒钟内没有 return,则可能发生了一些奇怪的事情。

大多数系统中,阻塞锁会导致TRUNCATE立即抛出错误消息:

--Run in session #1:
drop table test1;
create table test1(a number);
insert into test1 values(1);
commit;

--Run in session #2:
update test1 set a = 2;
--Do *NOT* commit the results.

--Run in session #1, it will generate:
--ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
truncate table test1;

但是,如果您的会话或系统设置了 DDL_LOCK_TIMEOUT,则会话将等待资源可用。

--Run in session #1.  This will wait either a long time or until the other session commits.
alter session set ddl_lock_timeout = 100000;
truncate table test1;

要解决此问题,请先查找谁阻止了会话:

select sid, final_blocking_session, gv$session.*
from gv$session
where final_blocking_session is not null;