測試oracle 的delete沒法釋放表空間

#################測試delete沒法釋放表空間,主要經過全盤掃描的磁盤I/O的開銷推斷

隨着數據的 insert,所使用段(Segment)的數據塊(data block)也不斷增長,這時候高水位(HWM)也隨着上升。當數據被刪除後(不管是 delete 仍是 truncate table)雖然被佔用的數據塊(data block)已經相應減小,可是高水位(HWM)並不會隨之降低。當高水位(HWM)下存在大量的空白數據塊(data block)時,若是發生全表掃描(Full Table Scan, FTS)就會形成不少額外的 IO。由於全表掃描(FTS)的時候讀取段(Segment)中的數據塊(data block)會一直讀取到高水位(HWM)才結束


SQL> create table t as select * from dba_objects;

Table created.

SQL> set autotrace on;
SQL> select count(*) from t;

  COUNT(*)
----------
     72653


Execution Plan
----------------------------------------------------------
Plan hash value: 2966233522

-------------------------------------------------------------------
| Id  | Operation       | Name | Rows  | Cost (%CPU)| Time      |
-------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |    1 |   290   (1)| 00:00:04 |
|   1 |  SORT AGGREGATE    |      |    1 |           |      |
|   2 |   TABLE ACCESS FULL| T      | 76948 |   290   (1)| 00:00:04 |
-------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement (level=2)


Statistics
----------------------------------------------------------
     28  recursive calls
      0  db block gets
       1113  consistent gets     ---磁盤I/O
       1035  physical reads
      0  redo size
    528  bytes sent via SQL*Net to client
    523  bytes received via SQL*Net from client
      2  SQL*Net roundtrips to/from client
      0  sorts (memory)
      0  sorts (disk)
      1  rows processed

SQL> set autotrace off;

SQL> delete from t;

SQL> select count(*) from t;

  COUNT(*)
----------
     0

SQL> commit;
SQL> set autotrace on;
SQL> select count(*) from t;

  COUNT(*)
----------
     0


Execution Plan
----------------------------------------------------------
Plan hash value: 2966233522

-------------------------------------------------------------------
| Id  | Operation       | Name | Rows  | Cost (%CPU)| Time      |
-------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |    1 |   289   (0)| 00:00:04 |
|   1 |  SORT AGGREGATE    |      |    1 |           |      |
|   2 |   TABLE ACCESS FULL| T      |    1 |   289   (0)| 00:00:04 |
-------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement (level=2)


Statistics
----------------------------------------------------------
      0  recursive calls
      0  db block gets
       1040  consistent gets   ---磁盤I/O沒有降低
      0  physical reads
      0  redo size
    525  bytes sent via SQL*Net to client
    523  bytes received via SQL*Net from client
      2  SQL*Net roundtrips to/from client
      0  sorts (memory)
      0  sorts (disk)
      1  rows processed

SQL> truncate table t;

SQL> select count(*) from t;

  COUNT(*)
----------
     0


Execution Plan
----------------------------------------------------------
Plan hash value: 2966233522

-------------------------------------------------------------------
| Id  | Operation       | Name | Rows  | Cost (%CPU)| Time      |
-------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |    1 |    2   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |      |    1 |           |      |
|   2 |   TABLE ACCESS FULL| T      |    1 |    2   (0)| 00:00:01 |
-------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement (level=2)


Statistics
----------------------------------------------------------
     20  recursive calls
      1  db block gets
     10  consistent gets          ---顯著降低
      0  physical reads
     96  redo size
    525  bytes sent via SQL*Net to client
    523  bytes received via SQL*Net from client
      2  SQL*Net roundtrips to/from client
      0  sorts (memory)
      0  sorts (disk)
      1  rows processed

總結:代表delete刪除並不能釋放表空間,雖然delete將不少記錄刪除,以空塊保留下來,oracle查詢時依然會去查詢以前分配的空塊。

1.delete產生rollback,若是刪除大數據量的錶速度會很慢,同時會佔用不少的rollback segments .truncate 是DDL操做,不產生rollback,速度快一些.
  
  Truncate table does not generate rollback information and redo records so it is much faster than delete.
  
  In default, it deallocates all space except the space allocated by MINEXTENTS unless you specify REUSE STORAGE clause.
  
2.delete不從tablespace中騰出空間,須要
  
  ALTER TABLESPACE AAA COALESCE(合併); 纔有空間
  
3.truncate 調整high water mark 而delete不.truncate以後,TABLE的HWM退回到 INITIAL和NEXT的位置(默認)
  
  delete 則不能夠。
  
4.truncate 只能對TABLE
  
  delete 能夠是table,view,synonym
5.、 truncate是DDL語言.
delete是DML語言

DDL語言是自動提交的.
命令完成就不可回滾.

oracle

相關文章
相關標籤/搜索