The rows are split into more than one block is called fragmentation. it decrease the block accesses and it slow the database performance. fragmentation are two type one is table fragmentation and second is index fragmentation.
TABLE FRAGMENTATION:
Each table have the blocks and it stored in the data files. The data will store in the block level. When the data split into more than one blocks is called table fragmentation. The table get more update and delete statement means table will get fragmentation.
When table getting full table scan in the that time oracle will read the data upto HWM (HIGH WATER MARK LEVEL). HWM is using to find used block and free block in table.
ROW CHAINING:
When inserting the row, in that time the block does not have any space means oracle allocate the next block for that inserting its called the row chaining.
ROW MIGRATION:
When updating the row in block the update not fit into the existing block it go to another block is called row migration. it based on pctfree. Because pctfree it low means the block get row migration.
FINDING THE FRAGMENTATION IN TABLE:
-
Create one table and add 2 column in that table.
-
Insert max 1000000 records in that table and commit it.
-
Now do some delete operation in that table and analyze the table using estimate or compute statistics or dbms_space.space_usage package.
-
Now check the info in dba_tables using query
(SELECT chain_cnt FROM user_tables WHERE table_name = ‘ROW_MIG_CHAIN_DEMO’;)
-
Its show the num_rows and chain_cnt, if u get any values in chain_cnt means fragmentation is accured.
-
Using dbms_space.space_usage
Steps to Check and Remove Table Fragmentation:-
1. Gather table stats:
To check exact difference in table actual size (dba_segments) and stats size (dba_tables). The difference between these value will report actual fragmentation to DBA. So, We have to have updated stats on the table stored in dba_tables. Check LAST_ANALYZED value for table in dba_tables. If this value is recent you can skip this step. Other wise i would suggest to gather table stats to get updated stats.
exec dbms_stats.gather_table_stats(‘&schema_name’,’&table_name’);
\
2. Check Table size:
select table_name,bytes/(1024*1024*1024) from dba_table where table_name=’&table_name’;
3. Check for Fragmentation in table:
SELECT chain_cnt FROM user_tables WHERE table_name = ‘ROW_MIG_CHAIN_DEMO’;
(or)
set pages 50000 lines 32767
select owner,table_name,round((blocks*8),2)||’kb’ “Fragmented size”, round((num_rows*avg_row_len/1024),2)||’kb’ “Actual size”, round((blocks*8),2)-round((num_rows*avg_row_len/1024),2)||’kb’,
((round((blocks*8),2)-round((num_rows*avg_row_len/1024),2))/round((blocks*8),2))*100 -10 “reclaimable space % ” from dba_tables where table_name =’&table_Name’ AND OWNER LIKE ‘&schema_name’
/Note: This query fetch data from dba_tables, so the accuracy of result depends on dba_table stats.
If you find reclaimable space % value more than 20% then we can expect fragmentation in the table. Suppose, DBA find 50% reclaimable space by above query, So he can proceed for removing fragmentation.
4. DEFRAGMENTATIONS (or) remove fragemenation
1. move table to another tablespace
2. export and import the table(exp/imp)
3. shrink command (fron oracle 10g)
(shrink command is only applicable for tables which are tablespace with auto segment space management)
4. CTAS method
5.online redefinition
option: 1 move table to another tablespace
alter table
enable row movement;
