【轉】淺談truncate的使用


delete 操作不會改變表的高水標記,因此如果我們對一個表插入1000萬條數據,然后再回滾(對insert操作做回滾相當於相應地做delete操作),會使表的高水標記增長得很高,這時雖然我們操作的表依然是一個空表,但是查詢它卻會讀驚人數量的內存塊,實驗如下:
ETL@RACTEST> create table test_table (a number);

Table created.

Elapsed: 00:00:00.01
ETL@RACTEST> set autotrace traceonly statistics;
ETL@RACTEST> select * from test_table;

no rows selected

Elapsed: 00:00:00.00

Statistics
----------------------------------------------------------
 

       24  recursive calls
          0  db block gets
          7  consistent gets
          0  physical reads
          0  redo size
        318  bytes sent via SQL*Net to client
        453  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          0  rows processed
可以看到查詢這個空表讀了7個內存塊。然后我們向表中插入1000萬條數據后再回滾:
insert into test_table select level from dual connect by level<=10000000;

10000000 rows created.

Elapsed: 00:00:58.38
ETL@RACTEST> rollback;

Rollback complete.

Elapsed: 00:00:01.15

ETL@RACTEST> set autotrace traceonly statistics;
ETL@RACTEST> select * from test_table;

no rows selected

Elapsed: 00:00:00.14

Statistics
----------------------------------------------------------
        283  recursive calls
          1  db block gets
      15463  consistent gets
          0  physical reads
        176  redo size
        318  bytes sent via SQL*Net to client
        453  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          4  sorts (memory)
          0  sorts (disk)
          0  rows processed
可以看到,同樣是讀一個空表,但是卻做了15463(consistent gets)+1(db block gets)個邏輯讀,可見delete操作不會修改表的高水標記。並且delete操作也很耗費時間,因此我們通常在想清空一個表的數據時用truncate來替代delete。truncate會以一種快速的方式清空表,只產生很少的 日志信息,並且會將高水標記清零。例如:
ETL@RACTEST> insert into test_table select level from dual connect by level<=10000000;

10000000 rows created.

Elapsed: 00:00:32.45
ETL@RACTEST> commit;

Commit complete.

Elapsed: 00:00:00.02
ETL@RACTEST> truncate table test_table;

Table truncated.

Elapsed: 00:00:29.52
ETL@RACTEST> set autotrace traceonly statistics;
ETL@RACTEST> select * from test_table;

no rows selected

Elapsed: 00:00:00.00

Statistics
----------------------------------------------------------
          1  recursive calls
          1  db block gets
          6  consistent gets
          0  physical reads
         96  redo size
        318  bytes sent via SQL*Net to client
        453  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          0  rows processed
truncate后再查詢只做了7個邏輯讀,也就是讀了7個內存塊。
但是truncate操作有一個限制,例如B表有一個外鍵參照A表的一個字段,那么truncate A表Oracle就會報錯,實驗如下:
create table ttt1 (a number,primary key(a));

Table created.

Elapsed: 00:00:00.02
ETL@RACTEST> insert into ttt1 select level from dual connect by level<=10;

10 rows created.

Elapsed: 00:00:00.02
ETL@RACTEST> commit;

Commit complete.

Elapsed: 00:00:00.00
ETL@RACTEST> select * from ttt1;

         A
----------
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10

10 rows selected.

Elapsed: 00:00:00.00
ETL@RACTEST> create table ttt2 (a number,constraint fk_ttt2_a foreign key(a) references ttt1(a));

Table created.
這時ttt2表的a字段參照了ttt1表的a字段,我如果用delete操作清空ttt1表的值是可以的,因為ttt2表沒有數據,刪除ttt1的數據不會違背外鍵參照完整性。
ETL@RACTEST> delete from ttt1;

10 rows deleted.
但是如果我們使用truncate,oracle則要報錯:
ETL@RACTEST> truncate table ttt1;
truncate table ttt1
               *
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys

因為truncate實現的是一種快速刪除,因此它無法檢驗參照完整性,即使刪除的數據不違反參照完整性,Oracle也不允許做truncate操作,也就是我們說的寧可錯殺一千,不可放過一個,呵呵。
我在培訓的時候有人說外鍵如果設置了級聯刪除就可以truncate,但實際上是不行的,實驗如下:
ETL@RACTEST> drop table ttt2 purge;

Table dropped.

Elapsed: 00:00:00.03
ETL@RACTEST> create table ttt2 (a number,constraint fk_ttt2_a foreign key(a) references ttt1(a) on delete cascade);

Table created.

Elapsed: 00:00:00.01
ETL@RACTEST> truncate table ttt1;
truncate table ttt1
               *
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys

Elapsed: 00:00:00.00
這是一種方式的級聯刪除,trucate仍然報錯,下面是另一種級聯:
ETL@RACTEST> drop table ttt2 purge;

Table dropped.

Elapsed: 00:00:00.01
ETL@RACTEST> create table ttt2 (a number,constraint fk_ttt2_a foreign key(a) references ttt1(a) on delete set null);

Table created.

Elapsed: 00:00:00.01
ETL@RACTEST> truncate table ttt1;
truncate table ttt1
               *
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
同樣不行,因此得出結論,只要有外鍵參照A表的字段,那么就不能對A表使用truncate操作!
 
轉自:http://blog.sina.com.cn/s/blog_6ff05a2c0100mira.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM