该篇接着http://589985.blog.51cto.com/160×××/1359260继续讨论ORA-01652的第二种情况,在一次CTAS建表中发现报如下错误

SQL> create table test as select * from dba_objects;

create table test as select * from dba_objects

*

ERROR at line 1:

ORA-01652: unable to extend temp segment by 128 in tablespace SHAO

  这个报错很容易懂就是你的表空间不够用了,处理方法也很简单,给shao加数据文件,或者resize已有数据文件(如果数据文件为不可自动扩展的话)。但是我还是觉得这个报错很奇怪,为什么要在永久表空间里扩展临时段呢?永久表空间里怎么会出现临时段呢?

  很是好奇,然后在mos和谷歌上找了很久加上自己作试验,基本把这个问题给弄清楚了。

我们先来看一下mos上是怎么解释的(Doc ID 19047.1),我截取了其中的一段相关内容

NOTE: A "temp segment" is not necessarily a SORT segment in a temporary tablespace.
        It is also used for temporary situations while creating or dropping objects like tables and indexes in permanent tablespaces.
        eg: When you perform a CREATE INDEX a TEMP segment is created to hold what will be the final permanent index data.

              This TEMP segment is converted to a real INDEX segment in the dictionary at the end of the CREATE INDEX operation.
              It remains a temp segment for the duration of the CREATE INDEX operation and so failures to extend it report ORA-1652 rather than an INDEX related space error.
--这段是说在create index时会在相应的永久表空间上建一个临时段,当create index语句结束该临时段转变为真正的索引段。而在create index语句没有结束之前该段一直是临时段,所以在表空间不足时报的就是不能临时段不能扩展而不是索引段。

A TEMPORARY segment may be from:

A SORTUsed for a SELECT or for DML/DDL
CREATE INDEXThe index create performs a SORT in the users default TEMP tablespace and ALSO uses a TEMP segment to build the final index in the INDEX  tablespace. Once the index build is complete the segment type is changed.
 CREATE PK CONSTRAINT
ENABLE CONSTRAINT
CREATE TABLENew tables start out as TEMPORARY segments.
   Eg: If MINEXTENTS is > 1 or you issue CREATE table as SELECT.
Accessing a GLOBAL TEMPORARY TABLEWhen you access a global temporary table a TEMP segment is instantiated to hold the temporary data.

从上面该表中我们可以知道,CTAS;CREATE PK CONSTRAINT;ENABLE CONSTRAINT等动作同create index一样会在永久表空间中产生临时表(测试发现alter table test move;也是);

下面是我测试的情况

1)create index

SQL> create index IND_TESTFLASH_NAME on test_flash(object_name);

Index created.

--session1中执行(然后立即在session2中执行下面语句)

SQL> select segment_name,segment_type,tablespace_name,buffer_pool from dba_segments where segment_type='TEMPORARY';
SEGMENT_NAME                                                                      SEGMENT_TYPE       TABLESPACE_NAME                BUFFER_
--------------------------------------------------------------------------------- ------------------ ------------------------------ -------
5.266                                                                             TEMPORARY          SHAO                           DEFAULT

--session2 中执行(注意要在session1 中create index语句没有结束之前执行)

SQL>select segment_name,segment_type,tablespace_name,buffer_pool from dba_segments where segment_type='TEMPORARY';

no rows selected

--在session1中create index结束后再次执行该语句结果为空

  由上面的实验可以看出在create index的过程中确实在表空间shao中产生了临时段,create index语句结束时该段又消失了(转变为真正的index段)

  CTAS;CREATE PK CONSTRAINT;ENABLE CONSTRAINT;alter table move等测试结果同上,就不在一一贴出来了。