1、建立初始化数据演示数据表中的高水位的变化
(1)、建立用户和表
SYS@odb>create user test8 identified by test8;
SYS@odb>grant connect,resource to test8;
SYS@odb>conn test8/test8
TEST8@odb>create table gaoshuiwei as select * from all_objects;
TEST8@odb>select TABLE_NAME,NUM_ROWS,BLOCKS from user_tables where table_name='GAOSHUIWEI';
TABLE_NAME NUM_ROWS BLOCKS
---------------------------------------- ----------
GAOSHUIWEI 68309 500
TEST8@odb>delete from gaoshuiwei;
TEST8@odb>commit;
TEST8@odb>select count(*) from gaoshuiwei;
COUNT(*)
----------
0
TEST8@odb>select TABLE_NAME,BLOCKS from user_tables wheretable_name='GAOSHUIWEI';
TABLE_NAME NUM_ROWS BLOCKS
---------------------------------------- ----------
GAOSHUIWEI 68309 500
可以看到在删除表中的数据后,在user_tables里面该表还是有68309行和500个快大约8M左右;
(2)、在次插入数据
TEST8@ odb>insert intogaoshuiwei select * from all_objects;
TEST8@ odb>commit;
TEST8@ odb>select count(*) fromgaoshuiwei;
COUNT(*)
----------
68309
TEST8@ odb>selectTABLE_NAME,BLOCKS from user_tables where table_name='GAOSHUIWEI';
TABLE_NAME NUM_ROWS BLOCKS
---------------------------------------- ----------
GAOSHUIWEI 68309500
TEST8@ odb>delete fromgaoshuiwei;
68309 rows deleted.
TEST8@ odb>commit;
Commit complete.
TEST8@ odb>selectTABLE_NAME,BLOCKS from user_tables where table_name='GAOSHUIWEI';
TABLE_NAME NUM_ROWS BLOCKS
---------------------------------------- ----------
GAOSHUIWEI 68309 500
看到信息没什么变化,需要搜集统计信息;
TEST8@odb>
execdbms_stats.gather_table_stats('TEST8','GAOSHUIWEI',cascade=>true);
PL/sql procedure successfullycompleted.
TEST8@ odb>selectTABLE_NAME,BLOCKS from user_tables where table_name='GAOSHUIWEI';
TABLE_NAME NUM_ROWS BLOCKS
---------------------------------------- ----------
GAOSHUIWEI 0 500
TEST8@ odb>selectsegment_name,segment_type,bytes from user_segments wheresegment_name='GAOSHUIWEI'; //查看段的信息
SEGMENT_NAME SEGMENT_TYPE BYTES
-------------------------------------- ----------
GAOSHUIWEI TABLE 8388608
块500 大约8M左右, 生成执行计划:
TEST8@odb>explain plan for select count(*) from gaoshuiwei;
Explained.
TEST8@odb>select * from table(dbms_xplan.display);
该表是0行数据 ,全表扫描,cost 189 ;原因是因为的该表来回的插入删除,高水位线一直没有降下来,还会扫描500个块,全部扫描一遍返回结果,如果发现表的数据不大,但是查询很慢,可能和高水位有关:
可采用
TEST8@ odb>alter table gaoshuiwei enablerow movement; //启用行移动
TEST8@ odb>alter table gaoshuiwei shrinkspace compact; //压缩表的空间,这时还没发生变化
TEST8@ odb>alter table gaoshuiwei shrinkspace; //降低表的高水位线这时变化就比较明显了
对索引也可以设置同样的操作
alter indexPUB_LOG_PK shrink space compact;
原文链接:https://www.f2er.com/oracle/212992.html