Oracle never fails to surprise me. And this time it was running truncate statements against my tables.
Oracle introduced dbms_space_admin.drop_empty_segments in 11.2. This enables us to drop unused space within a segment. In other words, this procedure will defragment a table. But why would it issue truncate against the tables that has data ?
Let's get started with a test case
SQL> create table T1 (A number) ;
Since DEFERRED_SEGMENT_CREATION is set to TRUE, segment for T1 is never created.
SQL> create table T2 (A number);
SQL> insert into T2 values (1);
SQL> commit;
My insert statement has created a segment because of the insert.
SQL> create table T3 (A number) ;
SQL> insert into T3 values (1);
SQL> commit;
SQL> delete from T3;
SQL> commit;
SQL> select table_name, segment_created from user_tables ;
T1 NO
T2 YES
T3 YES
T3 is the table that is fragmented. I inserted a row and then deleted it. So, I have some free space below the HWM. And drop_empty_segments procedure should take care of this.
Now lets run the procedure
SQL> execute dbms_space_admin.drop_empty_segments('SCOTT') ;
10046 trace of the above procedure shows that Oracle recursively ran the following SQLs
LOCK TABLE T2 IN EXCLUSIVE MODE NOWAIT
TRUNCATE TABLE T2
LOCK TABLE T3 IN EXCLUSIVE MODE NOWAIT
TRUNCATE TABLE T3
It locked tables T2 and T3 in exclusive mode. and then ran truncates against them.
But why was a truncate issued against T2 ? This table has data in it. And not even eligible for segment drop.
SQL> select table_name, segment_created from user_tables ;
T1 NO
T2 YES
T3 NO
As you can see, We have reclaimed the free space in T3 and segment_created has been set to NO.
But surprisingly, truncate was issued against T2 as well. I ran a select on T2, data wasn't actually truncated.
As per bug notes, this truncate is actually a no-op truncate. This is a special form of truncate. This truncate first checks if the segment has rows or not and then decides whether to truncate the segment.
But it still holds an exclusive lock on T2 which can cause major performance bottleneck. If T2 is a huge table with thousands of partitions, Oracle might take too long to probe each of these partitions to check if a row exists or not. And while this probe is on, any other session trying to insert a row into this table would wait for enq- TM contention.
Therefore, I'd never suggest running this procedure. There are other better ways to defrag a table.
No comments:
Post a Comment