Skip to main content

Welcome to DBA Master – Database Tips, Tricks, and Tutorials

Welcome to DBA Master ! This blog is dedicated to all things related to database administration , SQL optimization , and performance tuning . Whether you're a beginner or a seasoned DBA, you'll find practical guides, troubleshooting tips, and real-world tutorials to help you work smarter with data. What to Expect: SQL performance tuning tips Indexing strategies Backup and recovery best practices High availability and replication techniques Database creation, configuration, and setup Monitoring queries and scripts for proactive performance management Migration guides across different database platforms Security essentials and best practices Recommended tools for DBAs Real-world error fixes and how to solve them Stay tuned — exciting content is coming soon. Feel free to bookmark and share: www.dbamaster.com ! Thanks for visiting!

Oracle Defragmentation

reorganizes data in tables and indexes to remove fragmentation, reclaim space, and improve performance. Fragmentation from frequent inserts, updates, and deletes can increase I/O operations during queries.

create table test(id number,name varchar2(15));

begin
for i in 1..100000 loop
insert into test values(i,'siva');
end loop;
end;
/

commit

select sum(bytes)/1024/1024/1024 from dba_segments where segment_name='TEST';
select sum(bytes)/1024/1024/1024 from USER_segments where segment_name='TEST';

select table_name,avg_row_len,round(((blocks16/1024)),2)||'MB' "TOTAL_SIZE", round((num_rowsavg_row_len/1024/1024),2)||'Mb' "ACTUAL_SIZE",
round(((blocks16/1024)-(num_rowsavg_row_len/1024/1024)),2) ||'MB' "FRAGMENTED_SPACE",
(round(((blocks16/1024)-(num_rowsavg_row_len/1024/1024)),2)/round(((blocks16/1024)),2))100 "percentage"
from all_tables WHERE table_name='TEST';

select table_name,avg_row_len,round(((blocks16/1024)),2)||'MB' "TOTAL_SIZE", round((num_rowsavg_row_len/1024/1024),2)||'Mb' "ACTUAL_SIZE",
round(((blocks16/1024)-(num_rowsavg_row_len/1024/1024)),2) ||'MB' "FRAGMENTED_SPACE",
(round(((blocks16/1024)-(num_rowsavg_row_len/1024/1024)),2)/round(((blocks16/1024)),2))100 "percentage"
from user_tables where table_name='TEST';

analyze table test compute statistics;

set serverouput on;

DECLARE
unf NUMBER;
unfb NUMBER;
fs1 NUMBER;
fs1b NUMBER;
fs2 NUMBER;
fs2b NUMBER;
fs3 NUMBER;
fs3b NUMBER;
fs4 NUMBER;
fs4b NUMBER;
full NUMBER;
fullb NUMBER;
BEGIN
DBMS_SPACE.space_usage ('C##CHENNAI','TEST','TABLE',unf,unfb,fs1,fs1b,fs2,fs2b,fs3,fs3b,fs4,fs4b,full,fullb);
DBMS_OUTPUT.put_line ('Total number of blocks that are unformatted: ' || unf);
DBMS_OUTPUT.put_line ('Number of blocks that has at least 0 to 25% free space: ' || fs1);
DBMS_OUTPUT.put_line ('Number of blocks that has at least 25 to 50% free space: ' || fs2);
DBMS_OUTPUT.put_line ('Number of blocks that has at least 50 to 75% free space: ' || fs3);
DBMS_OUTPUT.put_line ('Number of blocks that has at least 75 to 100% free space: ' || fs4);
DBMS_OUTPUT.put_line ('Total number of blocks that are full in the segment: ' || full);
END;
/

EXEC dbms_stats.gather_table_stats(ownname => 'c##chennai', tabname => 'test', cascade => true, estimate_percent => 10,method_opt=>'for all indexed columns size 1', granularity => 'ALL', degree => 1);

Defragmentation Methods:

  1. move tablesapce
  2. ctas
  3. exp/imp
  4. shrink
  5. online redfinition

Shrink:

The SHRINK method in Oracle is used for de-fragmentation by compacting data within a segment to reclaim unused space and make storage more efficient. It works on tables and indexes in tablespaces managed with Automatic Segment Space Management (ASSM).

Benefits of Shrink:

  1. Reclaims unused space.
  2. Reduces fragmentation.
  3. Can be done online with minimal impact on DML operations.


alter table test enable row movement;
alter table test shrink space;
alter table test shrink space compact;
alter table test shrink space cascade;

Online table Redefinition:

DBMS_REDEFINITION is an Oracle package that allows for online table redefinition to reorganize and optimize database structures without significant downtime. This process can be used for defragmentation, schema changes, or moving tables to different tablespaces, all while the table remains available for DML operations.

Usage:

  1. Reducing fragmentation.
  2. Changing table structure (e.g., adding partitions).
  3. Migrating tables to new storage or tablespaces.


create table emp(id number,name varchar2(15),constraint id_pk primary key(id));

begin
for i in 1..100000 loop
insert into emp values(i,'siva');
end loop;
end;
/

begin
for i in 100001..10000000 loop
insert into emp values(i,'siva');
end loop;
end;
/

update emp set name='muthusiva' where id between 500000 and 700000;
delete from emp where id between 700001 and 900000;

analyze table emp compute statistics;

col table_name format a20
col total_size format a20
col actual_size format a20
col FRAGMENTED_SPACE format a20

select table_name,avg_row_len,round(((blocks16/1024)),2)||'MB' "TOTAL_SIZE", round((num_rowsavg_row_len/1024/1024),2)||'Mb' "ACTUAL_SIZE",
round(((blocks16/1024)-(num_rowsavg_row_len/1024/1024)),2) ||'MB' "FRAGMENTED_SPACE",
(round(((blocks16/1024)-(num_rowsavg_row_len/1024/1024)),2)/round(((blocks16/1024)),2))100 "percentage"
from user_tables where table_name='EMP';

grant execute on dbms_redefinition to chennai container=current;
GRANT EXECUTE ON DBMS_REDEFINITION TO chennai;
GRANT CREATE ANY TABLE TO chennai;
GRANT ALTER ANY TABLE TO chennai;
GRANT DROP ANY TABLE TO chennai;

conn sys / as sysdba

exec dbms_redefinition.can_redef_table('CHENNAI','EMP');

create table chennai.emp2 as select * from chennai.emp;

exec dbms_redefinition.start_redef_table('CHENNAI','EMP','EMP2′,'ID ID');

exec dbms_redefinition.sync_interim_table('CHENNAI','EMP','EMP2′);

alter table chennai.emp2 add (constraint id_pk2 primary key(id));

exec dbms_redefinition.finish_redef_table('CHENNAI','EMP','EMP2′);

drop table chennai.emp2;

Comments

Popular posts from this blog

Oracle Database 19C Performance Tunning - PART 1

Advantages: 1. Improved Query Performance •    Optimized SQL execution plans lead to faster query response times. •    Reduces unnecessary full table scans and improves indexing strategies. •    Parallel execution tuning speeds up large data processing tasks. 2. Better Resource Utilization •    Efficient use of CPU, memory, disk I/O, and network resources. •    Reduces contention on Redo Logs, Undo Tablespaces, and Buffer Cache. •    Helps in load balancing across multiple instances in RAC (Real Application Clusters). 3. Increased System Scalability •    Ensures that the database can handle a growing number of users and transactions. •    Proper tuning allows scaling without degrading performance. •    Optimized parallel processing ensures better performance on multi-core servers. 4. Lower Infrastructure Costs •    Reduces the need for add...

Oracle RMAN Backup And Restore

RMAN: (Oracle 8) RMAN (Recovery Manager) is a utility provided by Oracle Database to perform backup, restore, and recovery operations. It is a command line tool. Features of RMAN in Oracle 19c Comprehensive Backup Capabilities: Full and incremental backups. Block-level backups for efficient data storage. Archived redo log backups. Fast Recovery Area (FRA) integration for centralized backup storage. Efficient Recovery Options: Point-in-time recovery (PITR). Complete and incomplete recovery. Flashback database capabilities for quick undo of changes. Multitenant Database Support: RMAN fully supports container databases (CDBs) and pluggable databases (PDBs). Provides flexibility to back up and recover individual PDBs or entire CDBs. Automatic Space Management: Manages disk space in the FRA. Automatically deletes obsolete backups and archived logs. Data Deduplication and Compression: Backup optimization through block-level deduplication. Built-in compression algorithms to reduce storage req...

Oracle 19c Database Software Installation in OEL8

 Pre-requisites for OS level:            Set the static IP Address     Disable the Firewall (systemctl stop firewalld & systemctl disable firewalld)     set SELINUX=permissive on /etc/selinux/config  ##Need to restart the server use init 6 Oracle Installation Pre-requisites Methods     Automatic Setup     Manual Setup      Automatic requisites Setup: (avoid step 1 to step 5): dnf install -y oracle-database-preinstall-19c Install the dependencies: curl -o oracle-database-preinstall-19c-1.0-2.el8.x86_64.rpm https://yum.oracle.com/repo/OracleLinux/OL8/appstream/x86_64/getPackage/oracle-database-preinstall-19c-1.0-2.el8.x86_64.rpm dnf -y localinstall oracle-database-preinstall-19c-1.0-2.el8.x86_64.rpm Manual Setup: step 1: Add the karenl parameters and values vi /etc/sysctl.conf     fs.file-max = 6815744 kernel.sem = 250 32000 100 128 kernel....