Oracle CTAS

创建相同的表,包括数据,等同于table data的copy

Create Table tablename as select * from tablename

创建结构相同的表

Create TableCreate Table tablename as select * from tablename where 1=0

在需要一张备份表的情况下,有两种方法:

1. Truncate -> Insert into table select 

2. Drop -> Create table as select

简单分析下两种方法的优劣

1 create table tab_zy_test as 
2 select * from tbl_prerec_matchrest pm
3 where pm.dataowner = 987; 

耗时:14s

insert into tab_zy_test 
select * from tbl_prerec_matchrest pm
where pm.dataowner = 987;

耗时:1.6s

???

他妈的说好的比较慢呢?

梁敬彬,梁敬弘两位老师在<收获不止Oracle>这本书的P78~P79阐述了这样的观点

"insert into t select ..的方式是将数据先写到Data Buffer中,然后再刷到磁盘.而Create t as select ..的方式是跳过了数据缓存区,直接写入磁盘.前者需要先进内存,再到磁盘,后者直接进入磁盘,少了一个步骤"

但是

我他妈做实验的结果反而是相反的?

难道是数据量不够大 插入数据我记得只有30万条左右

需要测试上千万的数据

重新做了一个测试

700万的数据量

ctas 耗时 40s

insert into select 耗时 68s

1000万数据量

ctas耗时 5s

insert into select 耗时 16s

结论:

数据量越大,ctas比insert into select效率提升得越大。

 

注意:

在使用CTAS,并不会生成源表的Index,约束,主键,默认值等

 

posted @ 2018-03-29 18:59  Xeeyn  阅读(497)  评论(0编辑  收藏  举报