溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

shrink怎樣回收分區(qū)表碎片

發(fā)布時(shí)間:2021-11-12 15:42:05 來(lái)源:億速云 閱讀:113 作者:柒染 欄目:關(guān)系型數(shù)據(jù)庫(kù)

shrink怎樣回收分區(qū)表碎片,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

實(shí)驗(yàn)如下:

CREATE TABLE "SCOTT"."T4"
   (    "A" NUMBER,
        "B" NUMBER
   )
  PARTITION BY RANGE ("A")
(PARTITION "PART1"  VALUES LESS THAN (10),
PARTITION "PART2"  VALUES LESS THAN (20) ) ;

begin                                    
for v1 in 1..19
loop
insert into scott.t4 values(v1,dbms_random.value(1,100000));
commit;
end loop;
end;
/
INSERT INTO scott.T4 SELECT * FROM sT4;

SQL>   CREATE TABLE "SCOTT"."T4"
   (    "A" NUMBER,
        "B" NUMBER
  2    3    4     )
  5    PARTITION BY RANGE ("A")
  6  (PARTITION "PART1"  VALUES LESS THAN (10),
  7  PARTITION "PART2"  VALUES LESS THAN (20) ) ;

Table created.

SQL> begin                                    
  2  for v1 in 1..19
  3  loop
  4  insert into scott.t4 values(v1,dbms_random.value(1,100000));
  5  commit;
  6  end loop;
  7  end;
  8  /

PL/SQL procedure successfully completed.

SQL>

SQL> conn scott/tiger;
Connected.
SQL> INSERT INTO T4 SELECT * FROM T4;

19 rows created.

省略.........

SQL> INSERT INTO T4 SELECT * FROM T4;

77824 rows created.

SQL> INSERT INTO T4 SELECT * FROM T4;

155648 rows created.

SQL> INSERT INTO T4 SELECT * FROM T4;

311296 rows created.

SQL> commit;

Commit complete.

SQL> commit;

Commit complete.

SQL> select count(*) from t4;

  COUNT(*)
----------
    622592

SQL>

--sys用戶(hù)執(zhí)行查詢(xún)表大小及碎片
SQL> col SEGMENT_NAME for a15
SQL> select OWNER,SEGMENT_NAME,PARTITION_NAME,SEGMENT_TYPE,BYTES/1024/1024 size_m from dba_segments where SEGMENT_NAME='T4';

OWNER                          SEGMENT_NAME    PARTITION_NAME                 SEGMENT_TYPE           SIZE_M
------------------------------ --------------- ------------------------------ ------------------ ----------
SCOTT                          T4              PART1                          TABLE PARTITION            16
SCOTT                          T4              PART2                          TABLE PARTITION            16

SQL>
SQL> set lines 200
SQL> select table_name,
  2         num_rows,
  3         avg_row_len * num_rows / 1024 / 1024 actual_mb,
  4         blocks * 8 / 1024 high_water_mb,
  5         (blocks * 8 / 1024 - avg_row_len * num_rows / 1024 / 1024) need_recovery_mb
  6    from dba_tables
  7   where table_name = '&table_name' and owner='&owner';
Enter value for table_name: T4
Enter value for owner: SCOTT
old   7:  where table_name = '&table_name' and owner='&owner'
new   7:  where table_name = 'T4' and owner='SCOTT'

TABLE_NAME                       NUM_ROWS  ACTUAL_MB HIGH_WATER_MB NEED_RECOVERY_MB
------------------------------ ---------- ---------- ------------- ----------------
T4

SQL>

--分析表:
SQL> analyze table scott.T4 compute statistics;

Table analyzed.

SQL> select OWNER,SEGMENT_NAME,PARTITION_NAME,SEGMENT_TYPE,BYTES/1024/1024 size_m from dba_segments where SEGMENT_NAME='T4';

OWNER                          SEGMENT_NAME    PARTITION_NAME                 SEGMENT_TYPE           SIZE_M
------------------------------ --------------- ------------------------------ ------------------ ----------
SCOTT                          T4              PART1                          TABLE PARTITION            16
SCOTT                          T4              PART2                          TABLE PARTITION            16

SQL> select table_name,
  2         num_rows,
  3         avg_row_len * num_rows / 1024 / 1024 actual_mb,
  4         blocks * 8 / 1024 high_water_mb,
  5         (blocks * 8 / 1024 - avg_row_len * num_rows / 1024 / 1024) need_recovery_mb
  6    from dba_tables where table_name='T4';

TABLE_NAME                       NUM_ROWS  ACTUAL_MB HIGH_WATER_MB NEED_RECOVERY_MB
------------------------------ ---------- ---------- ------------- ----------------
T4                                 622592     16.625      31.46875         14.84375

SQL>

--刪除表數(shù)據(jù):
SQL> delete scott.t4 where rownum < 600000;
 
599999 rows deleted.

SQL>

--再查看表發(fā)小及碎片情況:
SQL> select OWNER,SEGMENT_NAME,PARTITION_NAME,SEGMENT_TYPE,BYTES/1024/1024 size_m from dba_segments where SEGMENT_NAME='T4';

OWNER                          SEGMENT_NAME    PARTITION_NAME                 SEGMENT_TYPE           SIZE_M
------------------------------ --------------- ------------------------------ ------------------ ----------
SCOTT                          T4              PART1                          TABLE PARTITION            16
SCOTT                          T4              PART2                          TABLE PARTITION            16

SQL>
SQL>
SQL>  select table_name,
  2           num_rows,
  3           avg_row_len * num_rows / 1024 / 1024 actual_mb,
  4           blocks * 8 / 1024 high_water_mb,
  5           (blocks * 8 / 1024 - avg_row_len * num_rows / 1024 / 1024) need_recovery_mb
  6      from dba_tables where table_name='T4';

TABLE_NAME                       NUM_ROWS  ACTUAL_MB HIGH_WATER_MB NEED_RECOVERY_MB
------------------------------ ---------- ---------- ------------- ----------------
T4                                 622592     16.625      31.46875         14.84375

SQL>


--再次分析一下表
SQL>  analyze table scott.T4 compute statistics;

Table analyzed.

SQL>
SQL> select OWNER,SEGMENT_NAME,PARTITION_NAME,SEGMENT_TYPE,BYTES/1024/1024 size_m from dba_segments where SEGMENT_NAME='T4';

OWNER                          SEGMENT_NAME    PARTITION_NAME                 SEGMENT_TYPE           SIZE_M
------------------------------ --------------- ------------------------------ ------------------ ----------
SCOTT                          T4              PART1                          TABLE PARTITION            16
SCOTT                          T4              PART2                          TABLE PARTITION            16

SQL>  select table_name,
  2           num_rows,
  3           avg_row_len * num_rows / 1024 / 1024 actual_mb,
  4           blocks * 8 / 1024 high_water_mb,
  5           (blocks * 8 / 1024 - avg_row_len * num_rows / 1024 / 1024) need_recovery_mb
  6      from dba_tables where table_name='T4';

TABLE_NAME                       NUM_ROWS  ACTUAL_MB HIGH_WATER_MB NEED_RECOVERY_MB
------------------------------ ---------- ---------- ------------- ----------------
T4                                  22593 .603298187      31.46875       30.8654518

SQL>

發(fā)現(xiàn)使勁的表大小已經(jīng)變?yōu)?.603298187MB了.

--使用dbms_stat包分析分區(qū)表,如下:
SQL> EXEC DBMS_STATS.GATHER_TABLE_STATS(OWNNAME=> 'SCOTT',TABNAME=> 'T4',METHOD_OPT=> 'for all indexed columns size auto',CASCADE=> TRUE,ESTIMATE_PERCENT=> 100);

PL/SQL procedure successfully completed.

SQL> select OWNER,SEGMENT_NAME,PARTITION_NAME,SEGMENT_TYPE,BYTES/1024/1024 size_m from dba_segments where SEGMENT_NAME='T4';

OWNER                          SEGMENT_NAME    PARTITION_NAME                 SEGMENT_TYPE           SIZE_M
------------------------------ --------------- ------------------------------ ------------------ ----------
SCOTT                          T4              PART1                          TABLE PARTITION            16
SCOTT                          T4              PART2                          TABLE PARTITION            16

SQL>  select table_name,
  2           num_rows,
  3           avg_row_len * num_rows / 1024 / 1024 actual_mb,
  4           blocks * 8 / 1024 high_water_mb,
  5           (blocks * 8 / 1024 - avg_row_len * num_rows / 1024 / 1024) need_recovery_mb
  6      from dba_tables where table_name='T4';

TABLE_NAME                       NUM_ROWS  ACTUAL_MB HIGH_WATER_MB NEED_RECOVERY_MB
------------------------------ ---------- ---------- ------------- ----------------
T4                                  22593 .538659096      31.46875       30.9300909

結(jié)果同上。

--查詢(xún)每個(gè)分區(qū)的碎片情況;
SQL> select t3.table_name, t3.partition_name,t3.num_rows * t3.AVG_ROW_LEN / 1024 / 1024 actual_mb,T3.blocks * 8 / 1024 high_water_mb,(blocks * 8 / 1024 - avg_row_len * num_rows / 1024 / 1024 ) need_recovery_mb,t3.empty_blocks,t3.last_analyzed from dba_tab_partitions t3 where t3.table_name ='T4';

TABLE_NAME                     PARTITION_NAME                  ACTUAL_MB HIGH_WATER_MB NEED_RECOVERY_MB EMPTY_BLOCKS LAST_ANALYZED
------------------------------ ------------------------------ ---------- ------------- ---------------- ------------ -------------------
T4                             PART1                                   0     15.734375        15.734375           34 2017-10-18 06:06:49
T4                             PART2                          .538659096     15.734375       15.1957159           34 2017-10-18 06:06:49

SQL>  



--進(jìn)行shrink表t4(整個(gè)分區(qū)shrink,當(dāng)然也可以針對(duì)子分區(qū)shrink,例如alter table PTABLE MODIFY PARTITION PTABLE_P2 shrink space)

SQL> alter table scott.t4 enable row movement;

Table altered.

SQL>
SQL> alter table scott.t4 shrink space cascade;

Table altered.

SQL> alter table scott.t4 disable row movement;

Table altered.


--查看表大小及碎片情況:
SQL> select OWNER,SEGMENT_NAME,PARTITION_NAME,SEGMENT_TYPE,BYTES/1024/1024 size_m from dba_segments where SEGMENT_NAME='T4';

OWNER                          SEGMENT_NAME    PARTITION_NAME                 SEGMENT_TYPE           SIZE_M
------------------------------ --------------- ------------------------------ ------------------ ----------
SCOTT                          T4              PART1                          TABLE PARTITION         .1875
SCOTT                          T4              PART2                          TABLE PARTITION         .8125

SQL>
SQL>  select table_name,
  2           num_rows,
  3           avg_row_len * num_rows / 1024 / 1024 actual_mb,
  4           blocks * 8 / 1024 high_water_mb,
  5           (blocks * 8 / 1024 - avg_row_len * num_rows / 1024 / 1024) need_recovery_mb
  6      from dba_tables where table_name='T4';

TABLE_NAME                       NUM_ROWS  ACTUAL_MB HIGH_WATER_MB NEED_RECOVERY_MB
------------------------------ ---------- ---------- ------------- ----------------
T4                                  22593 .538659096      31.46875       30.9300909

SQL>

--再次收集統(tǒng)計(jì)信息:
SQL> EXEC DBMS_STATS.GATHER_TABLE_STATS(OWNNAME=> 'SCOTT',TABNAME=> 'T4',METHOD_OPT=> 'for all indexed columns size auto',CASCADE=> TRUE,ESTIMATE_PERCENT=> 100);

PL/SQL procedure successfully completed.

SQL>
SQL> select OWNER,SEGMENT_NAME,PARTITION_NAME,SEGMENT_TYPE,BYTES/1024/1024 size_m from dba_segments where SEGMENT_NAME='T4';

OWNER                          SEGMENT_NAME    PARTITION_NAME                 SEGMENT_TYPE           SIZE_M
------------------------------ --------------- ------------------------------ ------------------ ----------
SCOTT                          T4              PART1                          TABLE PARTITION         .1875
SCOTT                          T4              PART2                          TABLE PARTITION         .8125

SQL>  select table_name,
  2           num_rows,
  3           avg_row_len * num_rows / 1024 / 1024 actual_mb,
  4           blocks * 8 / 1024 high_water_mb,
  5           (blocks * 8 / 1024 - avg_row_len * num_rows / 1024 / 1024) need_recovery_mb
  6      from dba_tables where table_name='T4';

TABLE_NAME                       NUM_ROWS  ACTUAL_MB HIGH_WATER_MB NEED_RECOVERY_MB
------------------------------ ---------- ---------- ------------- ----------------
T4                                  22593 .538659096      .6640625       .125403404

--同時(shí)查看各子分區(qū)情況:
SQL> select t3.table_name, t3.partition_name,t3.num_rows * t3.AVG_ROW_LEN / 1024 / 1024 actual_mb,T3.blocks * 8 / 1024 high_water_mb,(blocks * 8 / 1024 - avg_row_len * num_rows / 1024 / 1024 ) need_recovery_mb,t3.empty_blocks,t3.last_analyzed from dba_tab_partitions t3 where t3.table_name ='T4';

TABLE_NAME                     PARTITION_NAME                  ACTUAL_MB HIGH_WATER_MB NEED_RECOVERY_MB EMPTY_BLOCKS LAST_ANALYZED
------------------------------ ------------------------------ ---------- ------------- ---------------- ------------ -------------------
T4                             PART1                                   0      .0078125         .0078125           34 2017-10-18 06:15:37
T4                             PART2                          .538659096        .65625       .117590904           34 2017-10-18 06:15:37

關(guān)于shrink怎樣回收分區(qū)表碎片問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI