sql - 删除和截断和表大小
问题描述
我尝试了以下查询以了解删除和截断后对表大小的影响。
select * from SALES_HISTORY;
Output:
Product Month Sales
sony 22-DEC-17 24000
sony 22-DEC-17 24000
sony 22-DEC-17 24000
sony 22-DEC-17 24000
sony 22-DEC-17 24000
sony 22-DEC-17 24000
Delete from sales_history:
commit;
select segment_name,segment_type,bytes/1024/1024 MB
from dba_segments
where segment_type='TABLE' and segment_name='SALES_HISTORY';
Output:
segment_name segment_type MB
SALES_HISTORY TABLE 0.0625
Truncate table SALES_HISTORY;
之后我再次尝试查找表的大小,但它看起来与下面相同,如果截断所有行,它不应该为零
Output:
segment_name segment_type MB
SALES_HISTORY TABLE 0.0625
我需要了解删除和截断后对表和水印大小的影响。
解决方案
有几个选项truncate
,以下是这些选项如何delete
影响存储和高水位线:
delete
留下分配给表的所有空间。高水位线保持不变truncate table ... reuse storage
留下分配给表的所有空间。这会将高水位线重置为第一个范围truncate table ... drop storage
(默认)释放表上方的所有空间minextents
。并因此重置高水位线truncate table ... drop all storage
释放表中的所有空间
您可以通过以下方式验证这一点dbms_space.unused_space
:
create table t as
select level id, lpad ( 'x', 1000, 'x' ) stuff
from dual
connect by level <= 1000;
create or replace procedure show_size as
out_total_blocks integer;
out_total_bytes integer;
out_unused_blocks integer;
out_unused_bytes integer;
out_last_used_extent_file_id integer;
out_last_used_extent_block_id integer;
out_last_used_block integer;
begin
dbms_space.unused_space(
segment_owner => user
,segment_name => 'T'
,segment_type => 'TABLE'
,total_blocks => out_total_blocks
,total_bytes => out_total_bytes
,unused_blocks => out_unused_blocks
,unused_bytes => out_unused_bytes
,last_used_extent_file_id => out_last_used_extent_file_id
,last_used_extent_block_id => out_last_used_extent_block_id
,last_used_block => out_last_used_block
);
dbms_output.put_line ( 'Total Blocks: ' || out_total_blocks || ' (blocks in the segment)');
dbms_output.put_line ( 'Total Bytes: ' || out_total_bytes || ' (segment size in bytes)');
dbms_output.put_line ( 'Unused Blocks: ' || out_unused_blocks || ' (empty blocks)');
dbms_output.put_line ( 'Unused Bytes: ' || out_unused_bytes || ' (size of empty space in bytes)');
dbms_output.put_line ( 'Last Used Block: ' || out_last_used_block || ' (high water mark)');
end;
/
exec show_size;
Total Blocks: 256 (blocks in the segment)
Total Bytes: 2097152 (segment size in bytes)
Unused Blocks: 101 (empty blocks)
Unused Bytes: 827392 (size of empty space in bytes)
Last Used Block: 27 (high water mark)
delete t;
commit;
exec show_size;
Total Blocks: 256 (blocks in the segment)
Total Bytes: 2097152 (segment size in bytes)
Unused Blocks: 101 (empty blocks)
Unused Bytes: 827392 (size of empty space in bytes)
Last Used Block: 27 (high water mark)
truncate table t
reuse storage;
exec show_size;
Total Blocks: 256 (blocks in the segment)
Total Bytes: 2097152 (segment size in bytes)
Unused Blocks: 253 (empty blocks)
Unused Bytes: 2072576 (size of empty space in bytes)
Last Used Block: 3 (high water mark)
truncate table t
drop storage;
exec show_size;
Total Blocks: 8 (blocks in the segment)
Total Bytes: 65536 (segment size in bytes)
Unused Blocks: 5 (empty blocks)
Unused Bytes: 40960 (size of empty space in bytes)
Last Used Block: 3 (high water mark)
truncate table t
drop all storage;
exec show_size;
Total Blocks: 0 (blocks in the segment)
Total Bytes: 0 (segment size in bytes)
Unused Blocks: 0 (empty blocks)
Unused Bytes: 0 (size of empty space in bytes)
Last Used Block: 0 (high water mark)
推荐阅读
- json - 从Angular4中的[object object]获取值
- wpf - 如何在 WPF Datagrid 的另一个项目中包含一个子项目
- pandas - Pandas 在最后一行的几列中插入字典值
- android - 在保持焦点的同时禁用 EditText 输入
- c# - 使用 Prism、WPF 更改区域视图
- reactjs - React - 警告:函数作为 React 子级无效。如果您返回 Component 而不是
从渲染 - shiny - Problems with reactive object inside an eventReactive expression
- shell - 我正在尝试在我的 ubuntu 机器上从 Jenkins 服务器运行 shell 脚本
- go - 如何在地球上找到两条线(来自geojson的线串)之间的距离
- python - 如何知道 Django 中 FileField 中文件路径的长度?