您的位置:首页 > 其它

慎用create table as select,一定要注意默认值的问题---大一临时表方法

2014-09-12 16:45 645 查看


摘要:

1、再做一些数据迁移时候,很多人会使用create table  as select * from table where id=-1的方式来年建立一摸一样的表,但是这样做有个很大的弊端,不能将原表中的default value也一同迁移过来

2、  Using the CREATE TABLE ... AS SELECT ... command: This command will copy acrooss to the new table all the data,but the constraints triggers ,and so on will not be transferred
to the new table.

        那些都是not null约束,其他的约束和trigger是带不过来了,严格说来not null也是约束的一种,只不过教材上把它排除在外了吧。


慎用create table as select,一定要注意默认值的问题

博客分类:  

Oracleoraclecreate
talbe as select数据库迁移 

再做一些数据迁移时候,很多人会使用create table  as select * from table where id=-1的方式来年建立一摸一样的表,但是这样做有个很大的弊端,不能将原表中的default value也一同迁移过来,可以看下面的例子:

 

第一,新建一个表

 

-- Create table

create table table01

(

  id        number(16),

  add_date  date default sysdate,

  status    number(1),

  entp_code varchar2(200)

)

 

第二,使用create table table02 as

select * From table01 where id=-1

 

第三、看看两个表的结构,会发现第二张表的defaule value没有了,如下2图,可以很明显看出来,表02的add_date的默认值得sysdate没有了

table01的表结构



 table02的表结构



 

所以各位在做数据库迁移时候,使用create table as select时候,一定要注意默认值的问题

 

上周,因为此问题,导致生产环境下产生了大量的问题,头大了一天,特此奉献出来。

====================================================================


Create table as select 语句的两点说明

SQL > create table emp_copy as select * from emp where deptno=10;

第一,注意emp_copy表中没有定义任何列名,因为我们在列子句中用通配符从emp表取得数据,让Oracle像emp表中一样生成emp_copy表中的列——相同名称,相同数据类型定义。

第二,SQL*PLUS中可以发出的任何select语句可以放在create table as select 语句中,然后Oracle会自动获得从emp表选择的数据,在进emp_copy表中。但是 如果select语句的列子句中包括特定列清单,则create table子句要列出表中要包括的列,放在括号中,例如:

SQL > create table emp_copy_2 (empno,sal) as select empno, sal from emp where deptno=10;

========================================================

create table as select 2010-04-18 11:39:26

分类: Linux

大家都知道create table a as select * from b可以创建一个与b表结构一样的表,但是在实际应用中最好不要这么创建表。原因是这样只创建表的结构,而不会将原表的默认值一起创建。

说白了,表结构出来了,默认值没有。

另外,但是有一个我对一个大表执行create table a as select * from b时候报了一个temp表空间不足,不知道是什么原因,记录一下。下次发现在处理吧。

 

转载http://space.itpub.net/9252210/viewspace-660173

----------------------------------------

 

一、Symptoms

        During CTAS (CREATE TABLE AS SELECT), be reported:

       ORA-01652: unable to extend temp segment by 1024 in tablespace <DATA>

二、Cause

        The tablespace where the object is being created doesnt have sufficient space to extend for the CTAS command to succeed.

三、Solution

        Modify the datafile associated for the tablespace to AUTOEXTEND ON till the CTAS command gets executed successfully.

       During the CTAS , it creates a data segment
in the target tablespace and marks this segment as temporary in dictionary.

       Once the table created successfully , the dictionary type is changed from TEMPRORAY to TABLE. In addition, if the SELECT performs a SORT operation,temporary space may be used as for the same.

四、TEST CASE

       1. 创建表空间

          SQL> CREATE TABLESPACE TEST DATAFILE 'D:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL10G\TEST.DBF' SIZE 10M AUTOEXTEND OFF;

           Tablespace created

        2. 为客户分配默认表空间

          SQL> ALTER USER TESTER DEFAULT TABLESPACE TEST;

           User altered.

        3. 查询对象实际大小

         SQL>SELECT BYTES/1024/1024,TABLESPACE_NAME FROM DBA_SEGMENTS WHERE SEGMENT_NAME='DUMMY';

                     11       system    --Size of the DUMMY object is 11 M

       4. CTAS创建新对象

          SQL> CONN TESTER/TESTER

                      Connected.

           SQL> CREATE TABLE DUMMY_123 AS SELECT * FROM SCOTT.DUMMY;

           CREATE TABLE DUMMY_123 AS SELECT * FROM SCOTT.DUMMY

           *

           ERROR at line 1:

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

      The above error message reported above is because the Tablespace TEST is of 10 M size and AUTOEXTEND OFF. The object about to be created "DUMMY_123 " , requires 11 M size and as it doesn't have enough space to extend , it has failed with the ORA-1652
error message in "TEST" tablespace.

一、Temporary Segments Concept

        Oracle often requires temporary work space for intermediate
stages of database processing. There are different kinds of temporary segments in the database.

        Some of them are created explicitly by the users. The others are created and accessed for the user by the system.

       There are SQL operations containing a sorting step which require
temporary segments. However, segments used for sorting are not the only segments having SEGMENT_TYPE=TEMPORARY. Temporary segments can also exist for permanent segments creation.

      Temporary segments for sorting are created in the default temporary tablespace of the user. This tablespace may be of type TEMPORARY or PERMANENT.

      (1)  A TEMPORARY tablespace (Locally Managed Tablespace) is recommended for sort operations.

      (2)  Temporary segments for permanent segments creation are created in the tablespace specified in the create statement or in the user’s default tablespace.

a.  Temporary Tables

     only exists during a transaction or session.

     DML statements on temporary tables do not generate redo logs for the data changes.
However, undo logs for the data and redo logs for the undo logs are generated.

   

b.  Temporary LOBs

     The goal of temporary LOBs is to develop an interface to support the creation and deletion of lobs that act like local variables.

   

c.  Temporary Segments as work area for sorting

     When processing queries, Oracle often requires temporary workspace for intermediate stages of SQL statement execution.

     The sort area is allocated in memory. If the sort operation needs additional memory (above the value specified by the SORT_AREA_SIZE parameter), the sorted rows are written to disk to free up the sort area so that it can be re-used for the remaining sort.

    Oracle automatically allocates this disk space called a temporary segment.

    The following statements may require the use of a temporary segment for sorting:

   CREATE INDEX/SELECT ... ORDER BY/SELECT DISTINCT/SELECT ... GROUP BY/SELECT ... UNION/SELECT ... INTERSECT/SELECT ... MINUS/ANALYZE TABLE/Unindexed joins/Correlated subqueries

d.  Temporary Segments for permanent segments creation

     Besides sort operations, there are other SQL operations, which also require temporary segments:

   CREATE PRIMARY/UNIQUE KEY CONSTRAINT

    ALTER TABLE ... ENABLE PRIMARY/UNIQUE CONSTRAINT

    CREATE TABLE STORAGE (MINEXTENTS>1)

    CREATE TABLE AS SELECT

           The CTAS creates a data segment in the target tablespace and marks this segment as temporary in dictionary.

           On completion, the dictionary type is changed from temporary to table. In addition, if the SELECT performs a SORT operation, temporary space may be used as for a standard select.

   CREATE PARTITION TABLE

    ALTER TABLE ... SPLIT PARTITION

    CREATE SNAPSHOT

    CREATE INDEX

           The CREATE INDEX statement, after sorting the index values, builds a temporary segment in the INDEX tablespace;

           once the index is completely built, the segment type is changed to INDEX.

    DROP TABLE  

   

e.  Temporary Tablespaces

二、Introduction to Direct-Path INSERT

        1. Conventional insert operations:

            Oracle reuses free space in the table, interleaving newly inserted data with existing data. During such operations, Oracle also maintains referential integrity constraints.

        2. Direct-path INSERT operations:

           Oracle appends the inserted data after existing data in the table. Data is written directly into datafiles, bypassing the buffer cache. Free space in the existing data is not reused, and referential integrity
constraints are ignored. These procedures combined can enhance performance.

            a. During direct-path INSERT, you can disable the logging of redo and undo entries

            b.CREATE TABLE ... AS SELECT statement, does not have any indexes defined on it and not null constraint; you must define them later.
  

Note:If the database or tablespace is in FORCE LOGGING mode, then direct path INSERT always logs, regardless of the logging or nologging setting, such as STANDBY database.

========================================================

Create table as select性能简介:

在数据库中移动数据最快的方法是将其从一个表移动到别一个表,而不是需要进入操作系统将数据从一个表移动到别一个表,有一些常见的方法可用于提高数据迁移的性能:

1.             调整表的结构(删除索引和触发器)

2.             在数据迁移期间禁用约束

3.             使用提示和选项来改进事务性能

第一个技巧是调整表的结构,它涉及禁用目标表上面的任何触发器或索引。例如,如果在目标表上有行级触发器,则插入到表中的每一行都会执行触发器。如果可能的话,最好在数据插入之前将目标表的触发器禁用;如果应该为每个插入的行执行该触发器,则可以在批量插入完成以后再创建此触发器,而不应该在批量插入期间每插入一条数据重复执行触发器。

除了禁用触发器,应该在启动数据加载之前删除目标表上面的索引,然后在数据加载完毕后再重新创建索引。如果索引留在表上,ORACLE将在插入每一行时动态管理索引。

注意:删除索引和禁用触发器可以解决大多数数和大型表与表之间数据迁移中遇到的性能问题。

除了禁用索引,还要考虑到表上的约束。如果源数据已经在数据库的表中,在将数据加载到目标表之前,可以检查该数据了解它的相关约束(例如外键或check约束),一旦加载数据完成就可以重新启用这些约束。

如果以上选项无法提供适当的性能,就应该调查Oracle为数据迁移调整引入的选项。这些选项包括如下:
1.插入命令的APPPEND提示   类似于Direct
Path Loader,APPENED提示将数据块加载到表中,从表的高水位线开始。使用APPENED提示可以增加空间利用率。
2.Nologging选项  如果正在执行create
table as select 命令,使用nologing选项可避免在操作期间写入重做日志文件。
3.并行选项   并行查询使用多个进程来完成一个任务。对于create
table as select命令,可并行化create table 部分和查询部分。如果使用并行选项,则也应该使用nologing选项,否则并行操作将不得不由于串行化写入到联机重做日志文件而等待。

在使用这些高级选项之前,应该首先调查目标表的结构,确保已经避免前面提到的一些常见问题。


分享到:        
阅读(2376)| 评论(0)| 转载 (0) |举报

 

ORALCE DBMS_STATS与ANALYZE介绍
 

========================================================

今天鄙人有意翻出ORACLE 9I
Performance Tuning  的官方教材,看到原文:
  Using the CREATE TABLE ... AS SELECT ... command: This command will copy acrooss to the new table all the data,but the constraints triggers ,and so on will
not be transferred to the new table.

  但本人在自己的生产库的操作过程中,发现,只要是用as select CREATE的表,都是带有原表的CONSTRINTS....

有谁能解释一下,.

这难道是官方文件的手误???

Connected to Oracle9i Enterprise Edition Release 9.2.0.4.0 

Connected as serp01

SQL> select constraint_name from dba_constraints where table_name ='CRM_CUSTOMER';

CONSTRAINT_NAME

------------------------------

REFCRM_CUSTOMER2167

REFINV_WAREHOUSE482

REFINV_WAREHOUSE608

REFORG_EMPLOYEES0001

REFORG_EMPLOYEES1474

REFORG_ENTERPRISES1400

REFORG_ORGANIZATIONS000001

REFORG_ORGANIZATIONS2401

REFPUB_AREA_CODE379

REFPUB_CLASSIFY_CODE380

REFPUB_ENTERPRISE_TYPE_CODE381

REFPUB_INDUSTRY_CODE382

REFPUB_PAYCONDI_CODE0000001

REFTPUB_PAYMENT_CODE01

SYS_C0092306

SYS_C0092764

SYS_C0079060

SYS_C0079061

SYS_C0079062

SYS_C0079063

CONSTRAINT_NAME

------------------------------

SYS_C0079064

SYS_C0079065

SYS_C0079066

SYS_C0079067

SYS_C0079068

SYS_C0079069

SYS_C0079070

SYS_C0079071

SYS_C0079072

SYS_C0079073

SYS_C0079074

SYS_C0079075

SYS_C0079076

SYS_C0079077

SYS_C0079078

SYS_C0079079

SYS_C0079080

SYS_C0079081

SYS_C0079082

SYS_C0079083

SYS_C0079084

CONSTRAINT_NAME

------------------------------

SYS_C0079085

SYS_C0079086

SYS_C0079087

SYS_C0079088

SYS_C0079089

SYS_C0079090

SYS_C0079091

SYS_C0079092

SYS_C0079093

SYS_C0079094

SYS_C0079095

SYS_C0079096

SYS_C0079097

SYS_C0079098

SYS_C0079099

SYS_C0079100

SYS_C0079101

SYS_C0079102

SYS_C0079103

SYS_C0079104

SYS_C0079105

CONSTRAINT_NAME

------------------------------

SYS_C0079106

SYS_C0079107

SYS_C0079108

SYS_C0079109

PK19

67 rows selected

SQL> create table temp_h1 as select * from crm_customer where rownum<10;

create table temp_h1 as select * from crm_customer where rownum<10

ORA-00955: 名称已由现有对象使用

SQL> drop table temp_h1;

Table dropped

SQL> create table temp_h1 as select * from crm_customer where rownum<10;

Table created

SQL> select constraint_name from dba_constraints where table_name ='TEMP_H1';

CONSTRAINT_NAME

------------------------------

SYS_C0093440

SYS_C0093441

SYS_C0093442

SYS_C0093443

SYS_C0093444

SYS_C0093445

SYS_C0093446

SYS_C0093447

SYS_C0093448

SYS_C0093449

SYS_C0093450

SYS_C0093451

SYS_C0093452

SYS_C0093453

SYS_C0093454

SYS_C0093455

SYS_C0093456

SYS_C0093457

SYS_C0093458

SYS_C0093459

CONSTRAINT_NAME

------------------------------

SYS_C0093460

SYS_C0093461

SYS_C0093462

SYS_C0093463

SYS_C0093464

SYS_C0093465

SYS_C0093466

SYS_C0093467

SYS_C0093468

SYS_C0093469

SYS_C0093470

SYS_C0093471

SYS_C0093472

SYS_C0093473

SYS_C0093474

SYS_C0093475

SYS_C0093476

SYS_C0093477

SYS_C0093478

SYS_C0093479

SYS_C0093480

CONSTRAINT_NAME

------------------------------

SYS_C0093481

SYS_C0093482

SYS_C0093483

SYS_C0093484

SYS_C0093485

SYS_C0093486

SYS_C0093487

SYS_C0093488

SYS_C0093489

SYS_C0093490

SYS_C0093491

52 rows selected

SQL>
 
 
A答:也许你有好几个TEMP_H1, 在不同的SCHEMA里.  试试

select owner, table_name from dba_tables where table_name ='TEMP_H1';

就会一目了然

B答:这个是不可能的,用ctas从组表必然要对表加相应的约束等。。。。

C答:那些都是not null约束,其他的约束和trigger是带不过来了,严格说来not null也是约束的一种,只不过教材上把它排除在外了吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: