0% found this document useful (0 votes)
27 views

Reorganize Oracle Tables To Reduce Disk IO

Uploaded by

Hari Haran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Reorganize Oracle Tables To Reduce Disk IO

Uploaded by

Hari Haran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Reorganize Oracle Tables to reduce disk I/O

Alireza Kamrani
06/24/2024

Reorganizing Oracle tables can be an e ective way to reduce disk I/O and improve database
performance. Here are some steps you can take to reorganize Oracle tables:
1. Analyze the tables: Use the Oracle Analyze command or the Automatic Workload Repository
(AWR) to collect statistics on the tables, including their size, row count, and distribution of
data. This will help you identify which tables are most in need of reorganization.
2. Determine the fragmentation level: Check the fragmentation level of the tables by running
queries that show the number of free blocks and the average row length. This will help you
identify which tables are most fragmented and require reorganization.
3. Choose a reorganization method: There are several methods for reorganizing Oracle tables,
including Export/Import, ALTER TABLE MOVE, and CTAS (Create Table As Select). Each
method has its own advantages and disadvantages, and the best method will depend on the
speci c needs and requirements of your database.
4. Create temporary tables: Before reorganizing a table, create temporary tables to store the
data. This will help ensure that no data is lost during the reorganization process.
5. Reorganize the tables: Use the chosen method to reorganize the tables, moving the data to a
new tablespace or segment, or creating a new table with the same data.
6. Update indexes and constraints: After reorganizing the tables, you will need to update the
indexes and constraints to re ect the new table structure.
7. Test the performance: After reorganizing the tables, test the performance of the database to
ensure that the reorganization has reduced disk I/O and improved overall performance.

By following these steps, you can reorganize Oracle tables to reduce disk I/O and improve
database performance. However, it's important to plan and test the reorganization carefully to
ensure that it meets the needs of the database and does not introduce any new performance or
stability issues.

Also yos can use DBMS_REDEFINITION package that have some good options to restructure
your table, such as partitioning of a nonpartition table, reoder columns, compression,change
storage parameters of table/index, shrink,...
so after REDEFINITION your table segmenets will be recreated and wasted space is minimal, this
operation is Online.

For review, I demonstrate a example to show working on REDEFINITION.

How to Shrink a Secure le LOB Using Online Rede nition (DBMS_REDEFINITION)

create user test identi ed by test;


grant dba to test;
alter user test default tablespace users;
connect test/test;

-- CREATE THE TEST TABLES

CREATE TABLE test ( ID NUMBER, PHOTO BLOB)


LOB (PHOTO) STORE AS SECUREFILE ;
fi
fi
fi
fl
ff
fi
CREATE TABLE test_b le ( B_FILE BFILE) ;

CREATE or REPLACE DIRECTORY test as '/home/oracle/dir1';

-- MODIFY THE LOB TO NOT USE RETENTION OR PCTVERSION (ie remove consistent read
copies)

ALTER TABLE TEST MODIFY LOB (COL2) (PCTVERSION 0);

-- INSERT THE BFILE LOCATOR FOR THE PHOTO

insert into test_b le values ( b lename('TEST','1.jpg'));

commit;

-- INSERT 50 COPIES OF THE PHOTO INTO THE TEST TABLE

declare
tmp_blob blob default EMPTY_BLOB();
tmp_b le b le:=null;
dest_o set integer:=1;
src_o set integer:=1;
begin
select b_ le into tmp_b le from test_b le;
DBMS_LOB.OPEN (tmp_b le, DBMS_LOB.FILE_READONLY);
dbms_lob.createtemporary(tmp_blob, TRUE);

DBMS_LOB.LOADBLOBFROMFILE(tmp_blob,tmp_b le,DBMS_LOB.LOBMAXSIZE,dest_o set,sr


c_o set);
for i in 1..50 loop
insert into test values(i,tmp_blob);
commit;
end loop;
DBMS_LOB.CLOSE(tmp_b le);
end;
/

-- EXAMINE THE STORAGE USED BY THE PROCESS

column segment_name format a30


set pagesiz 1000

select segment_name, sum(bytes) BYTES, count(*) EXTENTS


from user_extents
group by segment_name;
SEGMENT_NAME BYTES EXTENTS
-------------------------- ---------- ----------
TEST 65536 1
TEST_BFILE 65536 1
SYS_IL000006602$$ 65536 1
SYS_LOB0000066$$ 58851328 57

--NOTE the SECUREFILE LOB HAS 57 EXTENTS AFTER THE INSERT

-- DELETE 1/2 OF THE ROWS IN THE TEST TABLE TO FREE UP SPACE

delete from test where (id/2) = trunc(id/2);

COMMIT;
ff
ff
fi
ff
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
ff
-- DEMONSTRATE THAT ALTER TABLE ... SHRINK SPACE CASCADE ... DOES NOT WORK
WITH SECUREFILE LOBS

alter table test enable row movement;

alter table test shrink space cascade;

select segment_name, sum(bytes) BYTES, count(*) EXTENTS


from user_extents
group by segment_name;

SEGMENT_NAME BYTES EXTENTS


-------------------------- ---------- ----------
TEST 65536 1
TEST_BFILE 65536 1
SYS_IL0000066057C$$ 65536 1
SYS_LOB0000066$$ 58851328 57

NOTE: There was no change in storage

-- *** REDEFINE THE TABLE USING DBMS_REDEFINITION ***

-- DETERMINE IF THE TABLE CAN BE REDEFINED ONLINE

SET SERVEROUTPUT ON

BEGIN
DBMS_REDEFINITION.CAN_REDEF_TABLE('TEST','TEST',
DBMS_REDEFINITION.CONS_USE_ROWID);
END;
/

PL/SQL procedure successfully completed.

-- CREATE THE INTERIM TABLE

CREATE TABLE INTERIM AS SELECT * FROM TEST WHERE 1=2;

-- START THE REDEFINITION

BEGIN
DBMS_REDEFINITION.START_REDEF_TABLE(
uname => 'TEST',
orig_table => 'TEST',
int_table => 'INTERIM',
options_ ag => DBMS_REDEFINITION.CONS_USE_ROWID);
END;
/

-- FINISH THE REDEFINITION

exec DBMS_REDEFINITION.FINISH_REDEF_TABLE('TEST','TEST','INTERIM');

-- EXAMINE THE STORAGE AFTER THE REDEFINITION

select segment_name, sum(bytes) BYTES, count(*) EXTENTS


from user_extents
group by segment_name
ORDER BY 1;
fl
SEGMENT_NAME BYTES EXTENTS
------------------------------ ---------- ---
INTERIM 65536 1
SYS_IL0000066057C00002$$ 65536 1
SYS_IL0000066061C00002$$ 65536 1
SYS_LOB0000066$$ 58851328 57
SYS_LOB0000066$$ 27262976 41
TEST 65536 1
TEST_BFILE 65536 1

NOTE: The new SECUREFILE LOB has 41 extents whereas the original has 57 extents ... thus
the segment has been shrunk.

For more information, see:


https://docs.oracle.com/en/database/oracle/oracle-database/19/arpls/
DBMS_REDEFINITION.html

Regards,

Alireza Kamrani.
06/24/2024

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy