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;
========================================================
分类: 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选项,否则并行操作将不得不由于串行化写入到联机重做日志文件而等待。
在使用这些高级选项之前,应该首先调查目标表的结构,确保已经避免前面提到的一些常见问题。
========================================================
今天鄙人有意翻出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....
有谁能解释一下,.
这难道是官方文件的手误???
| |
|
A答:也许你有好几个TEMP_H1, 在不同的SCHEMA里. 试试
select owner, table_name from dba_tables where table_name ='TEMP_H1';
就会一目了然
B答:这个是不可能的,用ctas从组表必然要对表加相应的约束等。。。。
C答:那些都是not null约束,其他的约束和trigger是带不过来了,严格说来not null也是约束的一种,只不过教材上把它排除在外了吧。 |