Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6548445
  • 博文数量: 1005
  • 博客积分: 8199
  • 博客等级: 中将
  • 技术积分: 13071
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-25 20:19
个人简介

脚踏实地、勇往直前!

文章分类

全部博文(1005)

文章存档

2020年(2)

2019年(93)

2018年(208)

2017年(81)

2016年(49)

2015年(50)

2014年(170)

2013年(52)

2012年(177)

2011年(93)

2010年(30)

分类: Oracle

2011-09-06 16:35:13

-- 使用shrink收缩表空间,shrink与move不同, move过程中会导致索引失效,shrink不会使索引失效.
-- 创建表
  1. Create Table hxl.tb_hxl_list
  2.  (
  3.    Id Number,
  4.    provcode Number
  5.  )
  6.  Partition By List(provcode)
  7.  (
  8.    Partition p_l1 Values(0),
  9.    Partition p_l2 Values(1),
  10.    Partition p_l3 Values(2),
  11.    Partition p_l4 Values(3)
  12.  );
 
-- 初始化数据
  1. Declare
  2. Begin
  3.   For i In 1 .. 100000 Loop
  4.      Insert Into hxl.tb_hxl_list Values(i,round(dbms_random.value(0,3)));
  5.   End Loop;
  6.   Commit;
  7.   End;
  8. /
-- 创建索引
  1. Create Index hxl.IDX_TB_HXL_LIST_N1 On hxl.tb_hxl_list(Id) Global;
 
-- 查询目前表和索引的大小
  1. Column Segment_Name format a20
  2. Column Blocks format 9999999999
  3. Column Extents format 9999999999
  4. Column BYTES format 9999999999
  5. Select Segment_Name,Blocks, Extents, a.BYTES
  6.      From Dba_Segments a
  7.  Where Segment_Name In( 'TB_HXL_LIST','IDX_TB_HXL_LIST_N1')
  8.  Order By Segment_Name;
  9. SEGMENT_NAME BLOCKS EXTENTS BYTES
  10. -------------------- ----------- ----------- -----------

  11. IDX_TB_HXL_LIST_N1 384 18 3145728
  12. TB_HXL_LIST         72 9 589824
  13. TB_HXL_LIST         32 4 262144
  14. TB_HXL_LIST         40 5 327680
  15. TB_HXL_LIST         72 9 589824
-- 删除数据
  1. Delete From hxl.tb_hxl_list Where Rownum<=50000;
 
-- 查看目前表和索引占用空间大小,发现跟删除数据前没有变化
  1. Select Segment_Name,Blocks, Extents, a.BYTES
  2.      From Dba_Segments a
  3.  Where Segment_Name In( 'TB_HXL_LIST','IDX_TB_HXL_LIST_N1')
  4.  Order By Segment_Name;
  5. SEGMENT_NAME BLOCKS EXTENTS BYTES
  6. -------------------- ----------- ----------- -----------

  7. IDX_TB_HXL_LIST_N1 384 18 3145728
  8. TB_HXL_LIST         72 9 589824
  9. TB_HXL_LIST         32 4 262144
  10. TB_HXL_LIST         40 5 327680
  11. TB_HXL_LIST         72 9 589824
 
-- 启用行迁移
  1. ALTER TABLE TB_HXL_LIST ENABLE ROW MOVEMENT;
 
--使用compact选项收缩表
  1. ALTER TABLE TB_HXL_LIST SHRINK SPACE COMPACT;
 
--查看目前表和索引占用空间大小,发现使用compact选项不能收缩表空间
  1. Select Segment_Name,Blocks, Extents, a.BYTES
  2.      From Dba_Segments a
  3.  Where Segment_Name In( 'TB_HXL_LIST','IDX_TB_HXL_LIST_N1')
  4.  Order By Segment_Name;
  5. SEGMENT_NAME BLOCKS EXTENTS BYTES
  6. -------------------- ----------- ----------- -----------

  7. IDX_TB_HXL_LIST_N1 384 18 3145728
  8. TB_HXL_LIST         72 9 589824
  9. TB_HXL_LIST         32 4 262144
  10. TB_HXL_LIST         40 5 327680
  11. TB_HXL_LIST         72 9 589824
 
--使用 cascade选项收缩表 
  1. alter table TB_HXL_LIST shrink space cascade;
 
-- 表和索引占用空间已经收缩
  1. SQL> Select Segment_Name,Blocks, Extents, a.BYTES
  2.   2 From Dba_Segments a
  3.   3 Where Segment_Name In( 'TB_HXL_LIST','IDX_TB_HXL_LIST_N1')
  4.   4 Order By Segment_Name;
  5. SEGMENT_NAME BLOCKS EXTENTS BYTES
  6. -------------------- ----------- ----------- -----------

  7. IDX_TB_HXL_LIST_N1 160 17 1310720
  8. TB_HXL_LIST         72 9 589824
  9. TB_HXL_LIST          8 1 65536
  10. TB_HXL_LIST          40 5 327680
  11. TB_HXL_LIST          8 1 65536

使用shrink时有如下限制:

Restrictions on the shrink_clause
The shrink_clause is subject to the following restrictions:
* You cannot specify this clause for a cluster, a clustered table, or any object with a LONG column.
* Segment shrink is not supported for tables with function-based indexes or bitmap join indexes.
* This clause does not shrink mapping tables of index-organized tables, even if you specify CASCADE.
* You cannot specify this clause for a compressed table.
* You cannot shrink a table that is the master table of an ON COMMIT materialized view. Rowid materialized views must be rebuilt after the shrink operation.

 

 

阅读(3215) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~