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 other ALTER 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的更多相关文章

  1. 【转载】alter table move 和 alter table shrink space的区别

    move 和shrink 的共同点1.收缩段2.消除部分行迁移3.消除空间碎片4.使数据更紧密 shrink 语法:  alter table TABLE_NAME shrink space [com ...

  2. [20190918]shrink space与ORA-08102错误.txt

    [20190918]shrink space与ORA-08102错误.txt 1.环境:SCOTT@test01p> @ ver1PORT_STRING                    V ...

  3. Oracle中shrink space命令

    shrink_clause:   http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_3001.htm#i2192484 ...

  4. Oracle shrink space

    一.开启表的行迁移 alter table table_name enable row movement; select 'alter table '||s.owner||'.'||s.table_n ...

  5. 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   ...

  6. Oracle 收缩表大小 Oracle Shrink Table --转载

    从10g开始,oracle开始提供Shrink的命令,假如我们的表空间中支持自动段空间管理 (ASSM),就可以使用这个特性缩小段,即降低HWM.这里需要强调一点,10g的这个新特性,仅对ASSM表空 ...

  7. Oracle shrink table

    shrink必须开启行迁移功能. alter table table_name enable row movement ; 在oracle中可以使用alter table table_name shr ...

  8. oralce move和shrink释放高水位

    转自:https://blog.51cto.com/fengfeng688/1955137 move和shrink的共同点: 收缩段,消除部分行迁移,消除空间碎片,使数据更紧密 shrink用法: 语 ...

  9. OCP读书笔记(25) - 题库(ExamE)

    401.Which of the following are correct about block media recovery? (Choose all that apply.)A. Physic ...

随机推荐

  1. Self-Attetion

    四.self-attention 1.是什么? attention机制通常用在encode与decode之间,但是self-attention则是输入序列与输出序列相同,寻找序列内部元素的关系即 K= ...

  2. Python学习【第7篇】:Python之常用模块2

    hashlib,configparser,logging模块 一.常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希 ...

  3. 【解题报告】 洛谷 P3492 [POI2009]TAB-Arrays

    [解题报告] 洛谷 P3492 [POI2009]TAB-Arrays 这题是我随机跳题的时候跳到的.写完这道题之后,顺便看了一下题解,发现只有一篇题解,所以就在这里顺便写一个解题报告了. 首先当然是 ...

  4. NLTK学习笔记(七):文本信息提取

    目录 实体识别:分块技术 分块语法的构建 树状图 IOB标记 开发和评估分块器 命名实体识别和信息提取 如何构建一个系统,用于从非结构化的文本中提取结构化的信息和数据?哪些方法使用这类行为?哪些语料库 ...

  5. Oracle学习总结(5)—— SQL语句经典案例

    --0.所有员工信息 SELECT * FROM emp --1.选择部门30的所有员工 SELECT * FROM emp WHERE deptno=20 --2.列出所有办事员(CLERK)的姓名 ...

  6. [置顶] Git学习总结(1)——Git使用详细教程

    一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...

  7. NOI2017爆零记[AFO]

    WC2017 12分……全省第一…… APIO2017 4分……全省第二…… 千言万语汇成两个表格 NOI2017 114分(笔试100+一试4+二试5+A类5)……全省第三…… 全场最菜…… day ...

  8. 清北学堂模拟赛d2t6 分糖果(candy)

    题目描述总共有n颗糖果,有3个小朋友分别叫做L,Y,K.每个小朋友想拿到至少k颗糖果,但这三个小朋友有一个共同的特点:对3反感.也就是说,如果某个小朋友拿到3颗,13颗,31颗,333颗这样数量的糖果 ...

  9. thymeleaf模板引擎基础使用(转)

    刚好项目上用到这个模板引擎,记录以下基础用法. thymeleaf介绍 简单说, Thymeleaf是一个跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP .相比其他的模板 ...

  10. MySQL的各种网络IO超时的用法和实现

    2016-04-06 赵伟 数据库开发者 客户端C API 在C API中调用mysql_options()来设置mysql_init() 所创建的连接对象的属性,使用这三个选项可以设置连接超时和读写 ...