How to remove 'wasted rows' after delete in Oracle SQL database

☆樱花仙子☆ 提交于 2021-02-08 07:20:18

问题


In Oracle sql database, a process in our system deleted (not truncated) approx 2 million rows from a table. This resulted in a huge number of 'wasted rows' causing the queries running on that table to take more than 9 hours which usually get over in 5 minutes. Upon checking, we found that the size of total number of actual rows was of around 2600MB whereas the overall table including 'wasted rows' had a size of 3700MB.

Please let me know what is the best way to delete rows and then get rid of 'wasted rows' so that we don't have to rebuild the table every time.


回答1:


Let's simulate your case with a test table created with some data

create table tst as 
select 
rownum id, lpad('x',1000,'y') pad
from dual 
connect by level <= 100000;

The table consist of 15K blocks

select blocks from user_segments
where segment_name = 'TST';

    BLOCKS
----------
     15360 

If we delete all rows, the table size remains the same

delete from tst;
commit;

select blocks from user_segments
where segment_name = 'TST';

    BLOCKS
----------
     15360 

After reorganising the table the table size goes down as the free space is removed.

alter table tst MOVE;  

select blocks from user_segments
where segment_name = 'TST';

    BLOCKS
----------
         8 

Note that this step requires a downtime of the application, no changes are allowed within the reorganisation.

Starting with Oracle 12.2 you can do this step ONLINE




回答2:


Free space should not affect your performance that much. Instead, I suspect your statistics may be out of date. Have you tried gathering the statistics, after the delete operation?



来源:https://stackoverflow.com/questions/58899461/how-to-remove-wasted-rows-after-delete-in-oracle-sql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!