SHRINK SPACE Command : Online Segment Shrink for Tables, LOBs and IOTs
ORACLE-BASE - ALTER TABLE ... SHRINK SPACE Command : Online Segment Shrink for Tables, LOBs and IOTs
https://oracle-base.com/articles/misc/alter-table-shrink-space-online
Shrink Space Examples
Here are some simple examples of the ALTER TABLE ... SHRINK SPACE
command.
-- Enable row movement.
ALTER TABLE scott.emp ENABLE ROW MOVEMENT; -- Recover space and amend the high water mark (HWM).
ALTER TABLE scott.emp SHRINK SPACE; -- Recover space, but don't amend the high water mark (HWM).
ALTER TABLE scott.emp SHRINK SPACE COMPACT; -- Recover space for the object and all dependant objects.
ALTER TABLE scott.emp SHRINK SPACE CASCADE;
The COMPACT
option allows the shrink operation to be broken into two stages. First the rows are moved using the COMPACT
option but the high water mark (HWM) is not adjusted so no parsed SQL statements are invalidated. The HWM can be adjusted at a later date by reissuing the statement without the COMPACT
option. At this point any dependent SQL statements will need to be re-parsed.
Other shrink commands of interest are displayed below.
-- Shrink a LOB segment (basicfile only).
ALTER TABLE table_name MODIFY LOB(lob_column) (SHRINK SPACE);
ALTER TABLE table_name MODIFY LOB(lob_column) (SHRINK SPACE CASCADE); -- Shrink an IOT overflow segment.
ALTER TABLE iot_name OVERFLOW SHRINK SPACE;
There is more detail about this functionality below.
Identify Large Segments
The DBA|ALL|USER_SEGMENTS
views can be used to identify large segments. The following example uses a top-n query to display the 20 largest segments.
SET LINESIZE 200
COLUMN owner FORMAT A30
COLUMN segment_name FORMAT A30
COLUMN tablespace_name FORMAT A30
COLUMN size_mb FORMAT 99999999.00 SELECT *
FROM (SELECT owner,
segment_name,
segment_type,
tablespace_name,
ROUND(bytes/1024/1024,2) size_mb
FROM dba_segments
ORDER BY 5 DESC)
WHERE ROWNUM <= 20;
You may see many of the larger segments being LOB segments. You can get more information about LOB segments specifically using the following top-n query.
SET LINESIZE 200
COLUMN owner FORMAT A30
COLUMN table_name FORMAT A30
COLUMN column_name FORMAT A30
COLUMN segment_name FORMAT A30
COLUMN tablespace_name FORMAT A30
COLUMN size_mb FORMAT 99999999.00 SELECT *
FROM (SELECT l.owner,
l.table_name,
l.column_name,
l.segment_name,
l.tablespace_name,
ROUND(s.bytes/1024/1024,2) size_mb
FROM dba_lobs l
JOIN dba_segments s ON s.owner = l.owner AND s.segment_name = l.segment_name
ORDER BY 6 DESC)
WHERE ROWNUM <= 20;
The following scripts are examples of these types of queries.
Row Movement
The ALTER TABLE ... SHRINK SPACE
command moves rows between existing blocks to compact the data, so before you attempt to shrink a table segment you need to enable row movement. You can check if row movement is already enabled by querying the ROW_MOVEMENT
column of the [DBA|ALL|USER]_TABLES
views.
SELECT row_movement
FROM user_tables
WHERE table_name = 'EMP'; ROW_MOVE
--------
DISABLED SQL>
Row movement is enabled with the following command.
ALTER TABLE emp ENABLE ROW MOVEMENT;
Repeating the previous query shows row movement is now enabled.
SELECT row_movement
FROM user_tables
WHERE table_name = 'EMP'; ROW_MOVE
--------
ENABLED SQL>
SecureFile LOBs
When using basicfile LOBs the shrink commands work as expected. To demonstrate this we need to create the following table containing a basicfile LOB column.
DROP TABLE lob_tab PURGE; CREATE TABLE lob_tab (
id NUMBER,
data CLOB
)
LOB(data) STORE AS BASICFILE (DISABLE STORAGE IN ROW); INSERT INTO lob_tab VALUES (1, 'ONE');
COMMIT;
We can see both shrink commands complete without errors.
SQL> ALTER TABLE lob_tab MODIFY LOB(data) (SHRINK SPACE); Table altered. SQL> ALTER TABLE lob_tab MODIFY LOB(data) (SHRINK SPACE CASCADE); Table altered. SQL>
Now recreate the table using a securefile LOB column.
DROP TABLE lob_tab PURGE; CREATE TABLE lob_tab (
id NUMBER,
data CLOB
)
LOB(data) STORE AS SECUREFILE (DISABLE STORAGE IN ROW); INSERT INTO lob_tab VALUES (1, 'ONE');
COMMIT;
Now the first command fails, but adding the CASCADE
option appears to make it work.
SQL> ALTER TABLE lob_tab MODIFY LOB(data) (SHRINK SPACE);
ALTER TABLE lob_tab MODIFY LOB(data) (SHRINK SPACE)
*
ERROR at line 1:
ORA-10635: Invalid segment or tablespace type SQL> ALTER TABLE lob_tab MODIFY LOB(data) (SHRINK SPACE CASCADE); Table altered. SQL>
Unfortunately, the second command doesn't work and the securefile LOB segment is not shrunk.
Instead, to shrink a securefile LOB segment you need to move it. In the following example the move is to the same tablespace.
ALTER TABLE lob_tab MOVE LOB(data) STORE AS (TABLESPACE users);
Comments and Restrictions
Here are some things to consider before performing shrink operations.
- Moving rows can cause problem with rowid based triggers.
- Rowid materialized views must be rebuilt after a shrink operation.
- The shrinking process is only available for objects in tablespaces with automatic segment-space management enabled.
- You can't combine the
SHRINK SPACE
clause with any otherALTER TABLE
clauses. - You can't shrink a cluster or a clustered table.
- You can't shrink any object with a
LONG
column. - You can't shrink tables with dependent function-based indexes, domain indexes, or bitmap join indexes.
- You can't shrink tables that are the master table of an
ON COMMIT
materialized view - Mapping tables of index-organized tables are not affected by a shrink.
- Shrinks can't be used for compressed tables, except those using Advanced Row Compression (ROW STORE COMPRESS ADVANCED).
- The shrink operation against a table doesn't cascade to the LOB segments. They need to handled separately.
- You can't shrink securefile LOB segments.
- Changing the arrangement of rows in a table can have a negative impact on performance in some circumstances. Test thoroughly before making any decisions.
- After any structural change, like a move, remember to check for unusuable indexes. You can use the unusuable_indexes.sql script to find them. If you have any, rebuild them.
For more information see:
Hope this helps. Regards Tim...
SHRINK SPACE Command : Online Segment Shrink for Tables, LOBs and IOTs的更多相关文章
- 【转载】alter table move 和 alter table shrink space的区别
move 和shrink 的共同点1.收缩段2.消除部分行迁移3.消除空间碎片4.使数据更紧密 shrink 语法: alter table TABLE_NAME shrink space [com ...
- [20190918]shrink space与ORA-08102错误.txt
[20190918]shrink space与ORA-08102错误.txt 1.环境:SCOTT@test01p> @ ver1PORT_STRING V ...
- Oracle中shrink space命令
shrink_clause: http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_3001.htm#i2192484 ...
- Oracle shrink space
一.开启表的行迁移 alter table table_name enable row movement; select 'alter table '||s.owner||'.'||s.table_n ...
- Analyzing 'enq: HW - contention' Wait Event (Doc ID 740075.1)
Analyzing 'enq: HW - contention' Wait Event (Doc ID 740075.1) In this Document Symptoms Cause ...
- Oracle 收缩表大小 Oracle Shrink Table --转载
从10g开始,oracle开始提供Shrink的命令,假如我们的表空间中支持自动段空间管理 (ASSM),就可以使用这个特性缩小段,即降低HWM.这里需要强调一点,10g的这个新特性,仅对ASSM表空 ...
- Oracle shrink table
shrink必须开启行迁移功能. alter table table_name enable row movement ; 在oracle中可以使用alter table table_name shr ...
- oralce move和shrink释放高水位
转自:https://blog.51cto.com/fengfeng688/1955137 move和shrink的共同点: 收缩段,消除部分行迁移,消除空间碎片,使数据更紧密 shrink用法: 语 ...
- OCP读书笔记(25) - 题库(ExamE)
401.Which of the following are correct about block media recovery? (Choose all that apply.)A. Physic ...
随机推荐
- Centos7自动式脚本搭建jumpserver
JumpServer脚本 这里需要安装阿里的yum源和epel源并解压: epel源地址https://mirrors.tuna.tsinghua.edu.cn/epel// 安装阿里互联网yum仓库 ...
- [forward] cmake, CMakeLists.txt梳理
cmake intro 原文请见 cmake使用总结(转)-工程主目录CMakeList文件编写 在 Linux 下进行开发很多人选择编写 makefile 文件进行项目环境搭建,而makefile ...
- 消除input框的默认样式
input, button, select, textarea { outline: none; -webkit-appearance: none; border-radius: 0; } outli ...
- C#学习笔记_10_设计模式&继承&多态
10_设计模式&继承&多态 设计模式 由前人总结的用来解决特定问题的解决方案 单例模式 在一个项目的不同模块中获取对象,获取到的是同一个对象 代码 继承 概念:如果多个类中具有相同的字 ...
- vue 底部bottomnav
<template> <div id="foot"> <div class="tabBar"> <div class= ...
- 清北学堂模拟赛d4t1 a
分析:大模拟,没什么好说的.我在考场上犯了一个超级低级的错误:while (scanf("%s",s + 1)),导致了死循环,血的教训啊,以后要记住了. /* 1.没有发生改变, ...
- hdu1595find the longest of the shortest 最短路
//给一个无向图,问删除一条边,使得从1到n的最短路最长 //问这个最长路 //这个删除的边必定在最短路上,假设不在.那么走这条最短路肯定比其它短 //枚举删除这条最短路的边,找其最长的即为答案 #i ...
- input子系统驱动学习之中的一个
刚開始学习linux这门课就被分配编写一个设备的input子系统驱动.这对我的确有点困难.只是实际的操作中发现困难远比我想象的要大的多.本以为依照老师课上的步骤就行非常快的完毕这项任务.后来发 ...
- android 通用菜单条实现(一)
一.前言介绍 直奔主题啦,非常多Android app都有菜单条.菜单条除了背景图片.图标的不同外,布局基本一致.大致能够分为三部分:菜单条的左側区域.菜单条中间区域.菜单条右側区域. 为了考虑代码的 ...
- 一个NHibernate的BUG
一.背景 我们如今做的项目,用NHibernate实现数据訪问层. 訪问数据时,有的数据库表是确定的:有明白的表名.字段名.这时候依照常规的方法处理就可以:建立数据库表到类的映射.使用HQL读写数据库 ...