最近需要清理一张大表,要求不能影响性能。在MySQL里边我们可以通过借助coreutils以及硬链接的方式来最小化I/O,Oracle也可以通过分批次回收空间来最小化I/O,到底如何,下面我们拭目以待。

一、TRUNCATE TABLE 语法

TRUNCATE TABLE [schema_name.]table_name

[ PRESERVE MATERIALIZED VIEW LOG | PURGE MATERIALIZED VIEW LOG ]

[ DROP STORAGE | REUSE STORAGE ] ;

--下面仅列出reuse storage的说明部分

REUSE STORAGE

Specify REUSE STORAGE to retain the space from the deleted rows allocated to the table. Storage values are not reset to the values when the table was created. This space can subsequently be used only by new data in the table resulting from insert or update operations. This clause leaves storage parameters at their current settings.

This setting is useful as an alternative to deleting all rows of a very large table—when the number of rows is very large, the table entails many thousands of extents, and when data is to be reinserted in the future. TRUNCATE TABLE with REUSE STORAGE performs several orders of magnitude faster than deleting all rows, but has the following drawbacks:

•You cannot roll back a TRUNCATE TABLE statement.

•All cursors are invalidated.

•You cannot flash back to the state of the table before the truncate operation.

This clause is not valid for temporary tables. A session becomes unbound from the temporary table when the table is truncated, so the storage is automatically dropped.

If you have specified more than one free list for the object you are truncating, then the REUSE STORAGE clause also removes any mapping of free lists to instances and resets the high-water mark to the beginning of the first extent.

二、演示truncate table .. reuse storage(11g)

SQL> select * from v$version where rownum=1;

BANNER

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

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

SQL> create table tb_reuse as select * from dba_objects;

Table created.

SQL> / --多次执行

37200896 rows created.

SQL> create table tb_noreuse as select * from tb_reuse;

Table created.

SQL> select count(*) from tb_reuse;

COUNT(*)

----------

37200896

SQL> select count(*) from tb_noreuse;

COUNT(*)

----------

37200896

SQL> select segment_name,bytes/1024/1024 from dba_segments where segment_name in('TB_REUSE','TB_NOREUSE');

SEGMENT_NAME BYTES/1024/1024

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

TB_REUSE 4165 --占用空间接近4GB

TB_NOREUSE 4172

SQL> truncate table tb_noreuse; --直接truncate,速度很快

Table truncated.

Elapsed: 00:00:00.25

SQL> select segment_name,bytes/1024/1024 from dba_segments where segment_name in('TB_REUSE','TB_NOREUSE');

SEGMENT_NAME BYTES/1024/1024

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

TB_REUSE 4165

TB_NOREUSE .0625 -- 空间已回收

Elapsed: 00:00:00.03

SQL> truncate table tb_reuse reuse storage; --使用reuse storage方式,并无太多性能提升

Table truncated.

Elapsed: 00:00:00.07

SQL> alter table tb_reuse deallocate unused keep 2048; --这里漏掉了指定m,缺省为byte

Table altered.

Elapsed: 00:00:00.36

SQL> select segment_name,bytes/1024/1024 from dba_segments where segment_name in('TB_REUSE','TB_NOREUSE');

SEGMENT_NAME BYTES/1024/1024

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

TB_REUSE .0625

TB_NOREUSE .0625

Elapsed: 00:00:00.03

三、演示truncate table .. reuse storage(12g)

SQL> select * from v$version where rownum=1;

BANNER CON_ID

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

Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production 0

SQL> create table tb_12_use as select * from dba_objects;

Table created.

SQL> insert into tb_12_use select * from tb_12_use;

90903 rows created.

SQL> /

11635584 rows created.

SQL> create table tb_12_nouse as select * from tb_12_use;

Table created.

SQL> select segment_name,bytes/1024/1024 from dba_segments where segment_name in('TB_12_USE','TB_12_NOUSE');

SEGMENT_NAME BYTES/1024/1024

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

TB_12_NOUSE 3074 --使用空间为3GB

TB_12_USE 3072

SQL> select 'Leshami' As author,'http://blog.csdn.net/leshami' as Blog from dual;

AUTHOR BLOG

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

Leshami http://blog.csdn.net/leshami

SQL> set timing on;

SQL> truncate table TB_12_NOUSE; --使用常规方式truncate

Table truncated.

Elapsed: 00:00:01.73

SQL> truncate table TB_12_USE reuse storage; --使用reuse storage方式,并无太多性能提升

Table truncated.

Elapsed: 00:00:01.10

SQL> alter table TB_12_USE deallocate unused keep 2048m;

Table altered.

Elapsed: 00:00:00.25

SQL> alter table TB_12_USE deallocate unused keep 1m;

Table altered.

Elapsed: 00:00:00.14

SQL> select segment_name,bytes/1024/1024 from dba_segments where segment_name in('TB_12_USE','TB_12_NOUSE');

SEGMENT_NAME BYTES/1024/1024

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

TB_12_NOUSE .0625

TB_12_USE 1.0625

Elapsed: 00:00:00.03

-- 由于前面的测试在非归档模式,因此重启切换到归档模式后再次测试

SQL> archive log list;

Database log mode Archive Mode

Automatic archival Enabled

Archive destination USE_DB_RECOVERY_FILE_DEST

Oldest online log sequence 396

Next log sequence to archive 398

Current log sequence 398

SQL> select count(*) from tb_12_use;

COUNT(*)

----------

23273472

SQL> select count(*) from tb_12_nouse;

COUNT(*)

----------

23273472

SQL> truncate table TB_12_NOUSE;

Table truncated.

Elapsed: 00:00:02.07

SQL> truncate table TB_12_USE reuse storage; --归档后使用reuse storage方式,同样无太多性能提升

--因为truncat属于DDL,本身并不会产生太大arch

Table truncated.

Elapsed: 00:00:00.76

四、小结

a、通过上述测试,当使用reuse storage与普通方式并无明显差异

b、truncate table 是ddl操作,无法回滚

c、尽管无明显性能差异,生产环境大表情况,还是建议使用reuse storage结合deallocate方式

oracle reuse storage,Oracle大表清算truncate . reuse storage相关推荐

  1. Oracle大表清理truncate .. reuse storage

    Oracle大表清理truncate .. reuse storage deallocate_unused_clause Purpose Use the deallocate_unused_claus ...

  2. oracle中的reuse,Oracle大表清理truncate .. reuse storage

    前言:上篇<WIN32界面开发之三:DUI雏形开发(一)>讲解了界面加载框架的创建,但我们的这些控件并没有起到控件的作用,现在还无法响应我们的点击事件和其它事 最近需要清理一张大表,要求不 ...

  3. MySQL快速清空大表数据(truncate table table_name;)

    MySQL快速清空大表数据 项目初次上线,进行性能测试造的数据量巨大,都是些不可用数据,但又有一些是必须保留的,很多时候需要进行系统性的清理数据或者是,将有用的数据筛选出来之后再插入到表中!保留表结构 ...

  4. oracle删除表不等待,oracle故障处理之删除大表空间hang住

    背景 数据库分区表数据越来越大,需要对过期话的数据进行迁移,以及大的分区表需要进行数据的清理和删除,达到释放磁盘空间的目的. 问题说明 环境:linux 6.X 数据库:oracle 11.2.0.4 ...

  5. oracle加大内存对大表,在ORACLE里如果遇到特别大的表,可以使用分区的表来改变其应用程序的性能...

    在ORACLE里如果遇到特别大的表,可以使用分区的表来改变其应用程序的性能. 以system身份登陆数据库,查看 v$option视图,如果其中Partition为TRUE,则支持分区功能:否则不支持 ...

  6. oracle 字段重命名大表,Oracle表字段的增、刪、改、表的重命名及主鍵的增、刪、改...

    一.表字段的增刪改: 添加字段的語法:alter table tablename add (column datatype [default value][null/not null],-.); 修改 ...

  7. MySQL大表drop/truncate操作流程

    1.创建新表 create table t1_new like t1;2.重命名表 rename table t1 to t1_deleted,t1_new to t1;3.创建硬链接 cd /dat ...

  8. ORACLE RAC集群大范围delete大表与insertupdate同时执行导致活动会话数飙升

    2018年6月7日 21:30左右,客户一oracle rac 11.2.0.3集群数据库活动会话数短时间内飙升,经过分析发现,客户应用21:26:00 首先发起对一张12G,6700万条数据的大表进 ...

  9. oracle必备文件,oracle初学者必备基础

    oracle 基础 ............................................[@more@] 51.如何将小表放入keep池中? alter table xxx sto ...

最新文章

  1. Python的零基础超详细讲解(第一天)-Python简介以及下载
  2. ASP.NET MVC 2 模型验证
  3. SQL中的in与not in、exists与not exists的区别以及性能分析
  4. NYOJ 453 小珂的烦恼 模拟
  5. 消除文法中一切左递归算法
  6. kali无限登录_Kali Linux没有无线网卡?玩个锤纸~
  7. 设计模式(10)-----模板方法模式
  8. [前端漫谈_4] 从 薛定谔的猫 聊到 Event loop
  9. 可以发外链的网站_可以发外链的地方有哪些?-top推
  10. 【算法】汉诺塔 移动
  11. 开源字符处理类库:CharString类 拆分自自己研发的web服务器中的类库
  12. /etc/init.d/functions详解
  13. 小学生把自己学校的网站搞了!
  14. vue实现下载pdf文件
  15. teamviewer远程控制工具
  16. win10一根网线连接linux,win10系统使用一根网线连接两台电脑的操作方法
  17. fafa什么意思_fafafafafa 什么意思
  18. 西班牙国家德比次回合时间确定 中国球迷需熬夜
  19. 2020最残酷一幕终于到来:最怕大势将至,你还浑然不知
  20. 西门子PLC1200学习之比较指令

热门文章

  1. 200亿买下推特后,马斯克宣布推特已死
  2. 畅谈企业数字化之道——2018全球企业服务大会隆重召开
  3. whmcs系统云服务器购买,whmcs主机服务器
  4. Cocos2d-x3.0游戏实例之《别救我》第十篇(完结)——用Json配置各类型怪物数据
  5. 如何使用JAVA代码将WORD转成PDF.
  6. 03-JavaWeb开发【最详细的CookieSession分析】
  7. LWN: Linux 5.3合入窗口进展 part 1
  8. 玩物下载被发现利用群晖NAS占用大量带宽和硬盘帮助爱奇艺做缓存加速, 这年头流氓真多,防不胜防!!!
  9. 【JavaScript】js简易实现检测系统字体是否存在
  10. 乐橙平台大华监控Android端实时预览播放