为什么我无法在临时表上创建索引?

时间:2017-04-01 11:57:32

标签: sql oracle oracle12c

首先我创建了一个临时表

CREATE GLOBAL TEMPORARY TABLE TMP ON COMMIT PRESERVE ROWS AS
SELECT *
FROM A

然后

INSERT INTO TMP
SELECT *
FROM B

COMMIT

最后

CREATE INDEX IDX ON TMP (COLA, COLB, COLC);

创建索引后,我收到以下错误

  

ORA-14452:尝试在已使用的临时表上创建,更改或删除索引

我在同一会话中执行了这些步骤。

我想做什么(使用临时表)

  1. 禁用/删除索引
  2. 插入大数据
  3. 启用/创建索引
  4. 我怎样才能实现这个目标?

3 个答案:

答案 0 :(得分:3)

  

ORA-14452:尝试在已使用的临时表上创建,更改或删除索引

     

当尝试执行DDL的会话已在表上创建DML时,尝试在具有on commit preserve行的全局临时表上执行DDL时,会发生此错误。       为了执行DDL,必须先截断表,否则必须退出会话。

http://www.adp-gmbh.ch/ora/err/ora_14452.html

答案 1 :(得分:1)

我不得不承认对此感到惊讶。到目前为止,我提出的唯一解决方案是重新建立会话

SQL> drop table tmp;

Table dropped.

SQL> 
SQL> CREATE GLOBAL TEMPORARY TABLE TMP ON COMMIT PRESERVE ROWS AS
  2  SELECT *
  3  FROM emp
  4  ;

Table created.

SQL> 
SQL> 
SQL> INSERT INTO TMP
  2  SELECT *
  3  FROM emp
  4  ;

14 rows created.

SQL> 
SQL> COMMIT
  2  ;

Commit complete.

SQL> 
SQL> connect scott/tiger
Connected.
SQL> 
SQL> CREATE INDEX IDX ON TMP (empno);

Index created.

答案 2 :(得分:0)

当您在临时表中插入行时,它将被锁定并且无法创建索引。 您应该在插入数据之前创建索引:

CREATE GLOBAL TEMPORARY TABLE tmp ON COMMIT PRESERVE ROWS  AS
        SELECT *
          FROM scott.dept;
CREATE INDEX idx ON
    tmp (deptno);
ora-14452: tentativa de criar, alterar ou eliminar um índice em uma tabela temporária que já está sendo usada
14452. 00000 -  "attempt to create, alter or drop an index on temporary table already in use"
*Cause:    An attempt was made to create, alter or drop an index on temporary
           table which is already in use.
*Action:   All the sessions using the session-specific temporary table have
           to truncate table and all the transactions using transaction
           specific temporary table have to end their transactions.

TRUNCATE TABLE tmp;

Table TMP truncado.

CREATE INDEX idx ON tmp (deptno); 

Index idx criado.

INSERT INTO tmp
    SELECT *
      FROM scott.dept;

4 linhas inserido.

COMMIT;

Commit concluído.