上一篇文章中提到(),当我们用delete删除数据时,因为无法降低高水位,对表做全表扫描时会带来数据库性能问题,而truncate可以大大改善这一状况,下面来看测试
SQL> conn zlm/zlm
Connected.
--创建表之前先记录下剩余表空间大小
SQL> select * from dba_free_space where tablespace_name='ZLM';
TABLESPACE_NAME FILE_ID BLOCK_ID BYTES BLOCKS RELATIVE_FNO
------------------------------ ---------- ---------- ---------- ---------- ------------
ZLM 6 128 51380224 6272 6
--创建一个100W行的大表
SQL> create table test1(int number);
Table created.
SQL> analyze table test1 compute statistics;
Table analyzed.
SQL> select table_name,num_rows,blocks,empty_blocks,avg_row_len,last_analyzed from dba_tables where table_name like 'TEST%';
TABLE_NAME NUM_ROWS BLOCKS EMPTY_BLOCKS AVG_ROW_LEN LAST_ANALYZED
------------------------------ ---------- ---------- ------------ ----------- ------------------
TEST1 0 0 0 0 27-SEP-14
注意,创建完表结构而未插入数据时,表的高水位是0,并没有为该表分配任何数据块
SQL> declare
2 i number := 0;
3 begin
4 for i in 1 .. 1000000 loop
5 insert into test1 values (i);
6 end loop;
7 commit;
8 end;
9 /
PL/SQL procedure successfully completed.
SQL> select count(*) from test1;
COUNT(*)
----------
1000000
--查看高水位状况
SQL> select header_file,header_block,bytes,blocks,extents from dba_segments where segment_name like 'TEST%';
HEADER_FILE HEADER_BLOCK BYTES BLOCKS EXTENTS
----------- ------------ ---------- ---------- ----------
6 130 13631488 1664 28
SQL> select block_id,extent_id,bytes,blocks from dba_extents where segment_name like 'TEST%';
BLOCK_ID EXTENT_ID BYTES BLOCKS
---------- ---------- ---------- ----------
128 0 65536 8
136 1 65536 8
144 2 65536 8
152 3 65536 8
160 4 65536 8
168 5 65536 8
176 6 65536 8
184 7 65536 8
192 8 65536 8
200 9 65536 8
208 10 65536 8
216 11 65536 8
224 12 65536 8
232 13 65536 8
240 14 65536 8
248 15 65536 8
256 16 1048576 128
384 17 1048576 128
512 18 1048576 128
640 19 1048576 128
768 20 1048576 128
896 21 1048576 128
1024 22 1048576 128
1152 23 1048576 128
1280 24 1048576 128
1408 25 1048576 128
1536 26 1048576 128
1664 27 1048576 128
SQL> select * from dba_free_space where tablespace_name='ZLM';
TABLESPACE_NAME FILE_ID BLOCK_ID BYTES BLOCKS RELATIVE_FNO
------------------------------ ---------- ---------- ---------- ---------- ------------