Database 11g

Wednesday, June 15, 2016

How to Reorganize Table using Expdp/Impdp Utility


Hi guys, today i will show how to reorganize fragmented data and reclaim the unused space  using Data-Pump Utility.

 Assume that you received the call from particular Department Users.. For example we will take a "Hospital Pharmacy Department" The pharmacy users are not able to order the medicine from the  pharmacy table.


As a DBA following actions you have to take....


1. First take the table-name of Pharmacy module from the Apps Team.


2. Then check the health of a database like datafile size,table size,Index status etc ...


3. If every thing is fine, then contact your developer, if they did any modifications in the table. Specially Deletions and Updations.


Note:-  Because when rows are deleted or updated in the table Oracle will not Reset the High water mark Which leads to Fragmentation, unnecessary oracle scan all the empty blocks also when query is executed which will degrade the performance.

High Water Mark : Is  a pointer where oracle stops writing in the blocks and put a mark where it ends. OR  we can say its an Barrier between Used and Unused Blocks.

 Example :-
Assume that you have 5 lac rows in your table which has accommodated in 1000 blocks, when you are executing SELECT query its taking 20 minutes to give the output, and If you deleted 4 lac rows from this table. and run again same SELECT query it will take same 20 minutes to read again. Here oracle read 1000 blocks again This is because of HWM is not reset by oracle after deletion.



Reorganizing Steps:-

 So first we have to check how much data got fragmented in that particular Table.

SQL> set verify off
     column owner format a10
     column alcblks heading 'Allocated|Blocks' just c
     column usdblks heading 'Used|Blocks'      just c
     column hgwtr heading 'High|Water'         just c
     break on owner skip page
     select a.owner,a.table_name,b.blocks alcblks,a.blocks usdblks,(b.blocks-a.empty_blocks-1)
     hgwtr from dba_tables a,dba_segments b where a.table_name=b.segment_name
     and a.owner=b.owner and a.owner not in('SYS','SYSTEM') and a.blocks <> (b.blocks-a.empty_blocks-1)
     and a.owner like upper('&owner')||'%'and a.table_name like upper('&table_name')||'%'
     order by 1,2;
Enter value for owner: HMIS
Enter value for table_name: PMPMCSTK

 OWNER             TABLE_NAME        Allocated Blocks     Used Blocks     High Water
---------- ---------------------------------------------------------------------------------------------------
HMIS                     PMPMCSTK                    640                        382                  639

Here you can observe the value in High-Water column is 639 but your actual data is in Used Blocks i.e 382 when we reorganize it should be 382 in all these 3 columns.

>> Now we start Re-Organizing the table in production and Import it back on Test database using expdp/impdp

Production Server

D:\exp_table> expdp dumpfile=PMPMCSTK.DMP directory=exp_table tables=HMIS.PMPMCSTK

> Announce the Down time if you are doing on production because you to have drop the table and import it back.

Target Test Server 

C:\imp_table> impdp dumpfile=PMPMCSTK.DMP directory=imp_table tables=HMIS.PMPMCSTK


> Now table has re-organized check whether data is still fragmented....

SQL> set verify off
     column owner format a10
     column alcblks heading 'Allocated|Blocks' just c
     column usdblks heading 'Used|Blocks'      just c
     column hgwtr heading 'High|Water'         just c
     break on owner skip page
     select a.owner,a.table_name,b.blocks alcblks,a.blocks usdblks,(b.blocks-a.empty_blocks-1)
     hgwtr from dba_tables a,dba_segments b where a.table_name=b.segment_name
     and a.owner=b.owner and a.owner not in('SYS','SYSTEM') and a.blocks <> (b.blocks-a.empty_blocks-1)
     and a.owner like upper('&owner')||'%'and a.table_name like upper('&table_name')||'%'
     order by 1,2;
Enter value for owner: HMIS
Enter value for table_name: PMPMCSTK
                                         
OWNER         TABLE_NAME        Allocated Blocks     Used Blocks     High Water
---------- ------------------------------ ---------- ----------------------------------------------------
HMIS                PMPMCSTK                 384                           382                383



  • You can observe here how High Water Mark has reset and reclaim the space from 639 to 383 and 640 to 384.
  • Now gather table level stats to updating Optimizer for creating Fresh Execution plan as per newly reorganized table data..

   SQL> Begin DBMS_STATS.GATHER_TABLE_STATS (
             ownname =>'HMIS',
             tabname =>'PMPMCSTK',
             degree => 2,
             cascade => TRUE,
             METHOD_OPT => 'FOR COLUMNS SIZE AUTO',
             estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE);
             END;
              /


Advantages if we are using Expdp method for Reorganizing:-

1. Faster compared to other re-org options.
2. Extra space is not required if source table is dropped before import.
3. Easier to use when there are too many objects for reorganization.

Drawbacks:-

1. Table availability is affected. So, application downtime should be planned.
2. Any DML performed on the table between export completion and import start time will not be recorded.



Happy Learning...





















Thursday, June 9, 2016

How to drop data pump job in oracle11g



SQL> select * from dba_datapump_jobs;


OWNER       JOB_NAME                OPERATION     JOB_MODE   STATE               DEGREE ATACHED_SESSIONS DATAPUMP_SESSIONS
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SYSTEM    SYS_IMPORT_FULL_03   IMPORT          FULL          EXECUTING              1                         1                         3
SYSTEM    SYS_IMPORT_FULL_01   IMPORT          FULL          NOT RUNNING          0                          0                         0
SYSTEM    SYS_EXPORT_FULL_01   EXPORT         FULL          NOT RUNNING          0                          0                         0



SQL> DROP TABLE SYSTEM.SYS_IMPORT_FULL_01;

Table dropped.


SQL> DROP TABLE SYSTEM.SYS_EXPORT_FULL_01;

Table dropped.



Happy Learning....











Monday, June 6, 2016

How to find Top 10 Fragmented Tables in Oracle


SQL> select * from (select table_name,round((blocks * 8), 2) "size (kb)",round((num_rows *                        avg_row_len / 1024), 2) "actual_data (kb)", (round((blocks * 8), 2) - round((num_rows * avg_row_len / 1024), 2)) "wasted_space (kb)"  from dba_tables where (round((blocks * 8), 2) > round((num_rows * avg_row_len / 1024), 2)) order by 4 desc)WHERE ROWNUM <= 10;


TABLE_NAME                                                   size (kb)     actual_data (kb)      wasted_space (kb)
------------------------------ ---------- ---------------- ---------------------------------------------------------------
SHSHTDTA                                                        2316384       1871077.06         445306.94
TBL_HIJRI_CONTROLS                                  1905392        1552467.5           352924.5
IDL_UB1$                                                          248272           810.97                247461.03
AHTRNHST                                                       1073152        861498.85           211653.15
OTOCURNC                                                       948296         750972.9             197323.1
AHTRNHST_B4_OPENING_FINAL_BILL          595432         469561.71           125870.29
SHDESCPT                                                         124936            44.83                124891.17
OTORDERS                                                        248776         204020.26            44755.74
INVSRVITM                                                        188416         145291.38              43124.62
HISTGRM$                                                         34584           1128.22                  33455.78

10 rows selected.



Happy Learning....