Syed Saad Ali

Oracle ACE Pro

Oracle Solution Architect

Oracle E-Business Suite

Oracle Cloud Infrastructure

Oracle Fusion Middleware

Oracle Database Administration

Oracle Weblogic Administration

Syed Saad Ali

Oracle ACE Pro

Oracle Solution Architect

Oracle E-Business Suite

Oracle Cloud Infrastructure

Oracle Fusion Middleware

Oracle Database Administration

Oracle Weblogic Administration

Installation/Configurations12c

April 10, 2023 Tablespace Level Fragmentation
Installation/Configurations12c

Tablespace Level Fragmentation in Oracle Database

Introduction

In this article, I will discuss tablespace level fragmentation, which causes slowness and wastage of space. Fragmentation is a common issue in Oracle databases due to excessive dml operations like insert followed by an update and delete operations.

Brief Explanation:

In Oracle, tablespace level fragmentation refers to space fragmentation within a tablespace, leading to inefficient storage utilization and potential performance issues. It occurs when free space within the tablespace is fragmented into smaller, non-contiguous extents.

Tablespace level fragmentation can occur due to various reasons, such as:

Data Insertions, Updates, and Deletions: Frequent data modifications can result in fragmented free space within the tablespace as objects grow and shrink.

Improper Storage Allocation: Inadequate or incorrect storage allocation settings can lead to fragmentation, especially if the initial extent sizes are too small or if automatic segment space management is not utilized.

Uneven Object Growth: When objects within the tablespace grow at different rates, it can lead to uneven distribution of free space and fragmentation.

Inefficient Reorganization or Maintenance: Lack of regular tablespace maintenance activities, such as the reorganization or shrinking of objects, can contribute to fragmentation over time.

The presence of tablespace level fragmentation can negatively impact database performance, leading to slower query execution, increased I/O operations, and inefficient use of storage resources.

Migrating tables and indexes to new tablespaces can indeed be an efficient and effective way to defragment and shrink a large tablespace in Oracle. This approach involves moving the objects from the fragmented tablespace to a new, properly-sized tablespace, thereby consolidating free space and optimizing storage utilization. Here’s an overview of the process:

Step 1. Log in with the dba account and create new tablespaces for the database user.

Sample SQL:
create tablespace XX_ERP2 datafile ‘/path/to/XX_ERP2.dbf’ size 256m autoextend on next 128m maxsize unlimited;

Step 2. Log in with the Database owner’s username/password

Step 3. Run the script below to generate a table migration script for migrating tables to a new tablespace

spool /home/oracle/movTbl.sql
select ‘alter table ‘ || SEGMENT_NAME || ‘ move tablespace XX_ERP2;’
FROM dba_Segments a,
dba_data_files b
WHERE b.file_id=a.relative_fno
and a.tablespace_name=’XX_ERP’ and segment_type=’TABLE’
order by FILE_NAME,segment_name;
spool off;

Step 4. Run the script below to generate an Index migration script for migrating the Index to a new tablespace

spool /home/oracle/movIndex.sql
 SELECT ‘alter index ‘ || SEGMENT_NAME || ‘ rebuild tablespace XX_ERP2;’
FROM dba_Segments a, dba_data_files b
WHERE b.file_id = a.relative_fno
AND a.tablespace_name = ‘XX_ERP’
AND segment_type = ‘INDEX’
ORDER BY FILE_NAME, segment_name;
spool off;

Step 5. Run the script below to generate the LOB Segments script for migrating the LOB Segments to a new tablespace if available.

spool /home/oracle/movLobSeg.sql
select ‘ALTER TABLE ‘ || table_name || ‘ move lob(‘ || COLUMN_NAME || ‘) STORE AS (TABLESPACE XX_ERP2);’
from dba_tab_columns
where owner=’XX_ERP’ and data_type=’CLOB’;
spool off;

Step 6. Check if anything is missing in the Original Tablespace to be Migrated

set lines 300
col owner format A26
col segment_name format A26
col segment_type format A26
col tablespace_name format A26
col relative_fno format 99999
col file_name format A50
SELECT owner, segment_name, segment_type,a.tablespace_name, a.relative_fno, b.file_name
FROM dba_Segments a,
dba_data_files b
WHERE b.file_id=a.relative_fno
and a.tablespace_name=’XX_ERP’
order by FILE_NAME,segment_name;

Step 7. Do not forget to change the default tablespace of the user to the Newly Created One

ALTER USER default tablespace XX_ERP2;

Step 8. Change the Old Tablespace Offline once all the Objects are transferred to the newly created Tablespace with the Scripts Generated.

alter tablespace XX_ERP offline;

 

December 1, 2017 Useful Commands For Unix Operating System
APPS DBAInstallation/Configurations10gInstallation/Configurations11gInstallation/Configurations12c

Useful Commands For Unix Operating System

 

1.Deleting files older than N number of days ?

find . -name ‘*.trc’ -mtime +[N in days]  -exec rm {} \;  
Command will delete files older then N days in that directory

 

2.Copying file keeping timestamp preserve

cp -ip file.txt file_txt_bkp

 

3.Listing files modified in last N days

find . -mtime -<ndays> -exec ls -lt {} \;

 

4.Sorting files based on Size of file ? 

ls -l | sort -nrk 5  | more
du -sm *|sort -nr|head -10 

 

5.Finding CPU & Memory detail 

->cat /proc/cpuinfo  (CPU)
->cat /proc/meminfo (Memory)
->topas

 

6.Finding if any service is listening on particular port or not ?

netstat -an | grep {port no}  

 

7.Finding Process ID (PID) associated with any port ?

lsof | grep {port no.}  
lsof should be installed and in path.Many times it will be installed eith root user.Make sure you have that permission

 

9.Finding a pattern in some file in a directory ?

grep pattern  file_name  ( find pattern in particular file )
grep -i pattern  file_name (find pattern ignoring the case)

 

10.Create symbolic link to a file ?

ln -s  pointing_to_original_file  symbolic_link_name

example
 ln -s /home/text.txt test.txt

 

11.History of command executed in UNIX 

fc -l 
fc -e – ls ( Would execute the last ls command.) 
History command will also do the same
history

 

12. Compressing a big file

zip -r new.zip new
or
compress file_name

 

13.Creating a tar file

tar -cvwf file.tar file.txt

 

14.Extracting the files from a tar file

tar -xvwf myfile.tar(System would unarchive (untar) the myfile.tar file into the current directory.)
tar -xvwzf myfile.tar.gz(System would unarchive (untar) the myfile.tar.gz file in the current directory.)

 

14.Extracting a gz file?

Use guzip command as follows:
gunzip file.gz
OR
gzip -d file.gz

 

15.Extract a tar.gz or .tgz file?

Files with extension tar.gz or .tgz are tar files compressed with gzip. On Unix system extract them with following command:
gunzip < file.tar.gz | tar xvf –
gunzip < file.tgz | tar xvf –

If you have GNU tar (Linux system) you can use the z option directly:
tar xvzf file.tar.gz
tar xvzf file.tgz

 

16.Using mailx command

echo “This is going to be body of the mail” |mailx -s “Subject:Testing” “false@gmail.com”

 

17.Checking the file system free and used space

df -gt 

 

 

November 1, 2017 Temporary Tablespace and Datafile Management in Oracle database
Installation/Configurations10gInstallation/Configurations11gInstallation/Configurations12cOracle Database Material

Temporary Tablespace and Datafile Management in Oracle database

1.  For Finding temp file and temp tablespace with size

SELECT file#, 
       status, 
       bytes / 1024 / 1024 “Size_MB”, 
       name 
FROM   v$tempfile; 

SELECT file_name, 
       tablespace_name, 
       bytes / 1024 / 1024 / 1024, 
       status 
FROM   dba_temp_files; 

2. Add tempfile to existing temp tablespace

SQL>ALTER TABLESPACE temp ADD tempfile ‘/u02/apps/oracle/temp01_01.dbf’ SIZE 
2048m; 

3. Add new temp tablespace and make as default

SQL>CREATE TEMPORARY TABLESPACE temp2 tempfile ‘/u02/apps/oracle/temp01.dbf’ SIZE 2g autoextend ON;

SQL>ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp2; 

Note: use “reuse” if datafile physically exists.

4. Making old Temporary Tablespace Offline

SQL>ALTER DATABASE tempfile ‘/u02/apps/oradata/temp01.dbf’ OFFLINE; 

5. Drop Temporary Tablespace 

SQL>DROP TABLESPACE temp INCLUDING CONTENTS AND datafiles;

NOTE: Don’t drop immediatly, Check user is using TEMP tablespace by the Below Given Query, then you can drop

SELECT a.username, 
       a.osuser, 
       a.sid 
       ||‘,’ 
       ||a.serial#                  SID_SERIAL, 
       c.spid                       Process, 
       b.tablespace                 tablespace, 
       a.status, 
       SUM(b.extents) * 1024 * 1024 SPACE 
FROM   v$session a, 
       v$sort_usage b, 
       v$process c, 
       dba_tablespaces d 
WHERE  a.saddr = b.session_addr 
       AND a.paddr = c.addr 
       AND b.tablespace = d.tablespace_name 
GROUP  BY a.username, 
          a.osuser, 
          a.sid 
          ||‘,’ 
          ||a.serial#, 
          c.spid, 
          b.tablespace, 
          a.status; 

6. Shrink Temporary Tablespace

SQL>ALTER TABLESPACE temp shrink tempfile ‘/u02/apps/oradata/temp01.dbf’ keep 10g;

October 20, 2017 Change Date and Time Formats In Oracle Database?
Installation/Configurations10gInstallation/Configurations11gInstallation/Configurations12cOracle Database Material

Change Date and Time Formats In Oracle Database?

Option 1. If We want to change this format to DD/MONTH/YYYY format.

SQL> alter session set nls_date_format=’DD/MONTH/YYYY’;

Session altered.

SQL> select sysdate from dual;

SYSDATE

—————–

20/OCTOBER  /2017

 

Option 2. If we want get the both date and time

SQL> alter session set nls_date_format=’DD/MONTH/YYYY HH24:MI:SS’;

Session altered.

SQL> select sysdate from dual;

SYSDATE

————————–

20/OCTOBER  /2017 17:19:12 –It is 24 hours format.

 

SQL> alter session set nls_date_format=’DD/MONTH/YYYY HH:MI:SS’;

Session altered.

SQL> select sysdate from dual;

SYSDATE

————————–

20/OCTOBER  /2017 05:19:43  –It is 12 hours format.