Jan 30, 2016

Table Fragmentation in Oracle Database - to get performance benifit

Some times In oracle schema you may found some tables which has huge difference in actual size (size from User_segments) and expected size from user_tables (Num_rows*avg_row_length (in bytes)). This all is due to fragmentation in the table or stats for table are not updated into user_tables.

We will discuss:
1) What is Table Fragmentation?
2) How to understand HWM (High Water Mark) in table?
3) What are the reasons to reorganization of table?
4) How to find most fragmented tables?
5) How to reset HWM / remove fragmentation?
6) How to get more performance benefits from most fragmented tables?
7) Demo

1) What is Table Fragmentation?

If a table is only subject to inserts, there will not be any fragmentation. Fragmentation comes with when we update/delete data in table. The space which gets freed up during non-insert DML operations is not immediately re-used (or sometimes, may not get  reuse ever at all). This leaves behind holes in table which results in table fragmentation.

To understand it more clearly, we need to be clear on how oracle manages space for tables.

When rows are not stored contiguously, or if rows are split onto more than one block, performance decreases because these rows require additional block accesses.

Note that table fragmentation is different from file fragmentation. When a lot of DML operations are applied on a table, the table will become fragmented because DML does not release free space from the table below the HWM.(High Water Mark).

2) How to understand HWM (High Water Mark) in table?

HWM is an indicator of USED BLOCKS in the database. Blocks below the high water mark (used blocks) have at least once contained data. This data might have been deleted. Since Oracle knows that blocks beyond the high water mark don't have data, it only reads blocks up to the high water mark when doing a full table scan.

DDL statement always resets the HWM.

3) What are the reasons to reorganization of table?

a) Slower response time (from that table)
b) High number of chained (actually migrated) rows. 
c) Table has grown many folds and the old space is not getting reused.

Note: Index based queries may not get that much benefited by reorg as compared to queries which does Full table 
scan.

4) How to find most fragmented tables?

In Oracle schema there are tables which has huge difference in actual size (size from User_segments) and expected size from user_tables (Num_rows*avg_row_length (in bytes)). This all is due to fragmentation in the table or stats for table are not updated into dba_tables.

5) What actions to be taken on most fragmented tables?

Steps to Check and Remove Table Fragmentation:- 

i)  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');

ii) Check Table size:

Now again check table size using and will find reduced size of the table.

select table_name,bytes/(1024*1024*1024) 
from dba_table 
where table_name='&table_name';  -- keep a track to match after fragmentation 

iii) Check for Fragmentation in table:

Below query will show the total size of table with fragmentation, expected without fragmentation and how much % of size we can reclaim after removing table fragmentation. Database Administrator has to provide table_name and schema_name as input to this query.

SQL> set pages 50000 lines 32767;
SQL> 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.
To find Top 10 fragmentation tables
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;
SQL> 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.

5) How to reset HWM / remove fragmentation?

We have three options to reorganize fragmented tables:

1. Alter table move (to another tablespace, or same tablespace) and rebuild indexes:- 
   (Depends upon the free space available in the tablespace)  
2. Export and import the table:- (difficult to implement in production environment)
3. Shrink command (from Oracle 10g onwards)
   (Shrink command is only applicable for tables which are tablespace with auto segment space management)

Here, I am following Options 1 and 3 option by keeping table availability in mind. 

Option: 1 

Alter table move (to another tablespace, or same tablespace) and rebuild indexes:-
Collect status of all the indexes on the table:-

We will record Index status at one place, So that we get back them after completion of this exercise,  

SQL> select index_name,status from dba_indexes 

where table_name like '&table_name';

Move table in to same or new tablespace:
---------------------------------------
In this step we will move fragmented table to same tablespace or from one tablespace to another tablespace to reclaim fragmented space. Find Current size of you table from dba_segments and check if same or any other tablespace has same free space available. So, that we can move this table to same or new tablespace.

Steps to Move table in to same tablespace:

-----------------------------------------
alter table <table_name> move;   ------> Move to same tablespace

OR

Steps to Move table in to new tablespace:
----------------------------------------
alter table <table_name> enable row movement;
alter table <table_name> move tablespace <new_tablespace_name>;

Now, get back table to old tablespaces using below command

alter table table_name move tablespace old_tablespace_name;

Now,Rebuild all indexes:
-----------------------
We need to rebuild all the indexes on the table because of move command all the index goes into unusable state.

SQL> select status,index_name from dba_indexes where table_name = '&table_name';

STATUS INDEX_NAME

-------- ------------------------------
UNUSABLE INDEX_NAME                            -------> Here, value in status field may be valid or unusable.

SQL> alter index <INDEX_NAME> rebuild online;  -------> Use this command for each index

Index altered.

SQL> select status,index_name from dba_indexes where table_name = '&table_name';


STATUS INDEX_NAME
-------- ------------------------------
VALID INDEX_NAME                               -------> Here, value in status field must be valid.

After completing these steps, table statistics must be gathered.

Option: 2 Export and import the table:-

Click here to read from my posts

Option: 3 Shrink command (from Oracle 10g onwards):-


Shrink command: 

Its a new 10g feature to shrink (reorg) the tables (almost) online which can be used with automatic segment space 
management.

This command is only applicable for tables which are tablespace with auto segment space management.

Before using this command, you should have row movement enabled.

SQL> alter table <table_name> enable row movement;
Table altered.

There are 2 ways of using this command.

a. Rearrange rows and reset the HWM:

Part 1: Rearrange (All DML's can happen during this time)

SQL> alter table <table_name> shrink space compact;
Table altered.

Part 2: Reset HWM (No DML can happen. but this is fairly quick, infact goes unnoticed.)

SQL> alter table <table_name> shrink space;
Table altered.

b. Directly reset the HWM:

(Both rearrange and restting HWM happens in one statement)
SQL> alter table <table_name> shrink space; 
Table altered.

Advantages over the conventional methods are:

1. Unlike "alter table move ..",indexes are not in UNUSABLE state.After shrink command,indexes are updated also.
2. Its an online operation, So you dont need downtime to do this reorg.
3. It doesnot require any extra space for the process to complete.

After completing these steps, table statistics must be gathered.

6) How to get more performance benefits from most fragmented tables?

After doing above steps, you must gather statistics to tell optimizer to create best execution plan for better performance during query execution. Here I have given some auto sampling method to gather stats. Most of cases I got performance benefits when I did auto sampling method.

Gather table stats:

SQL> exec dbms_stats.gather_table_stats('&owner_name','&table_name');
PL/SQL procedure successfully completed.
OR
SQL> exec dbms_stats.gather_table_stats('&owner_name', '&table_name', estimate_percent => dbms_stats.auto_sample_size);
OR 
SQL> exec dbms_stats.gather_table_stats(ownname=>'&owner_name',
tabname=>'&table_name',estimate_percent => 100, cascade=>true, method_opt=>'for all columns size AUTO'); 

–- For entire schema
SQL>  EXECUTE DBMS_STATS.GATHER_SCHEMA_STATS('&schema_name',DBMS_STATS.AUTO_SAMPLE_SIZE);

Check Table size:

Now again check table size using and will find reduced size of the table.

SQL> select table_name,bytes/(1024*1024*1024) from dba_table where table_name='&table_name';

Now match with your earlier track, You must have some benefits. Sure performance will improve.

7) Demonstration:

Here I ll show you one demo activity. But when you will do, you first complete in your pre-prod database and collect performance statistics before and after. Then based on benefit, you can plan for production.

Demo:
1) Take all invalid objects counts for whole database as well as applied schema

select count(1) from dba_objects where status='INVALID' -- 2386

select count(1) from dba_objects where status='INVALID' and owner='CUSTOMER' -- 0

2) Take Top list ( preferably 10) of tables for fragmenation

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;

Output:

TABLE_NAME                      size (kb) actual_data (kb) wasted_space (kb)

------------------------------ ---------- ---------------- -----------------
CUSTOMER_SERVICES_DTLS       12382432      10341757.49       2040674.51
PKG_ACTUAL_AVAILABLE           7291976       5736686.1         1555289.9
PROCESSED_TRAN                  1601072       367932.44         1233139.56
PROCESSED_CUURENCY              1314672       145479.1          1169192.9
ACTUAL_SERVICES_DTLS            7452568       6332113.25        1120454.75
SERVICEREQUESTDETAILS           3037840       1932758.36        1105081.64
PKG_RESULTREPORTDTLS            1436632       440030.4          996601.6
BATCH_TXN_SERIALITEM            2621128       1820127.37        801000.63
CUSTOMER_BILLDETAILS            233616        1451156.52        782459.48


10 rows selected

-- Find size
SQL> select segment_name, bytes/(1024*1024*1024) "Size GB" from
  2  dba_segments where segment_name='CUSTOMER_SERVICES_DTLS';
  

SEGMENT_NAME                           Size GB
-------------------------------------- ----------
CUSTOMER_SERVICES_DTLS             11.828125

SQL> 

3) Take one sample table. Here we ll take "CUSTOMER_SERVICES_DTLS". Find the owner.

SQL>
select owner,table_name,tablespace_name,num_rows,blocks
from dba_tables where table_name='CUSTOMER_SERVICES_DTLS';

output:


OWNER      TABLE_NAME                     TABLESPACE_NAME    NUM_ROWS     BLOCKS

---------------------------- ------------------------------ ---------- ----------
CUSTOMER      CUSTOMER_SERVICES_DTLS     CUSTOMER              74055662    1542825

4) Do below activities for safe purpose:

a) Take DDL


-- Create table
create table CUSTOMER.CUSTOMER_SERVICES_DTLS
(
 xxxxxx
) tablespace CUSTOMER;
--Create/Recreate indexes 
create index CUSTOMER.INDX_TXNID on CUSTOMER.CUSTOMER_SERVICES_DTLS (TXNID)
  tablespace CUSTOMER;
create index CUSTOMER.INDX_SYSTEMUPDATEDDATE on CUSTOMER.CUSTOMER_SERVICES_DTLS (SYSTEMUPDATEDDATE)
  tablespace CUSTOMER;

b) take logical backup using expdp:


expdp directory=data_pump dumpfile=CUSTOMER_SERVICES_DTLS.dmp logfile=CUSTOMER_SERVICES_DTLS.log tables=CUSTOMER.CUSTOMER_SERVICES_DTLS exclude=statistics

5) Verify all index status

SQL> select index_name,status
  2  from dba_indexes where table_name='CUSTOMER_SERVICES_DTLS';

INDEX_NAME                     STATUS
------------------------------ --------
INDX_TXNID             VALID
INDX_SYSTEMUPDATEDDATE         VALID

SQL> 

6) Now move the table:
SQL> connect / as sysdba
SQL> set timing on;
SQL> alter table CUSTOMER.CUSTOMER_SERVICES_DTLS move;

Table altered.


Elapsed: 00:11:12.18

SQL> 

(Note: Based of table size, more archivelogs will be generated. You must have sufficient space on required tablespace/ datafile, including TEMP tablespace)

7) Now again verify these:
a) No. of records
SQL> select count(rowid) from CUSTOMER.CUSTOMER_SERVICES_DTLS;

COUNT(ROWID)
------------
    74055662

SQL> 

b) Index statistics

SQL> select index_name,status

  2  from dba_indexes where table_name='CUSTOMER_SERVICES_DTLS';

INDEX_NAME                     STATUS
------------------------------ --------
INDX_TXNID             UNUSABLE
INDX_SYSTEMUPDATEDDATE         UNUSABLE

SQL> 

Here Indexes are "Unusable" status. So these must be rebuild.

8) Rebuild the Indexes

SQL> alter index CUSTOMER.INDX_TXNID rebuild online;

Index altered.

SQL> alter index CUSTOMER.INDX_SYSTEMUPDATEDDATE rebuild online;

Index altered.

SQL> 

Now check the index stats

SQL> select index_name,status from dba_indexes where table_name='CUSTOMER_SERVICES_DTLS';
INDEX_NAME                     STATUS
------------------------------ --------
INDX_TXNID             VALID
INDX_SYSTEMUPDATEDDATE         VALID

SQL> 


--> Now all are valid.


9) Now Chek no. of rows and blocks

SQL> select owner,table_name,tablespace_name,num_rows,blocks
from dba_tables where table_name='CUSTOMER_SERVICES_DTLS';

output:

OWNER      TABLE_NAME                     TABLESPACE_NAME    NUM_ROWS     BLOCKS

---------------------------- ------------------------------ ---------- ----------
CUSTOMER      CUSTOMER_SERVICES_DTLS     CUSTOMER              74055662    151033

--> See here no. of blocks reduced.


10) Now Gather table stats:

SQL> exec dbms_stats.gather_table_stats(ownname=>'CUSTOMER',tabname=>'CUSTOMER_SERVICES_DTLS',estimate_percent => 100, cascade=>true, method_opt=>'for all columns size AUTO'); 


11) Check Table size:

Now again check table size using and will find reduced size of the table.

SQL> 
SQL> select segment_name, bytes/(1024*1024*1024) "Size GB" from
  2  dba_segments where segment_name='CUSTOMER_SERVICES_DTLS';
  

SEGMENT_NAME                           Size GB
-------------------------------------- ----------
CUSTOMER_SERVICES_DTLS             10.02131

SQL> 


--> Here table size reduced also.

12) Now crosscheck all valid/ invalid object counts and match. You can release your database if you have taken downtime.

Issues may come during the above activity:

SQL> alter table CUSTOMER.CUSTOMER_SERVICES_DTLS move; 
alter table CUSTOMER.CUSTOMER_SERVICES_DTLS move
*
ERROR at line 1:
ORA-01652: unable to extend temp segment by 8192 in tablespace CUSTOMER


i.e., you don't have sufficient space on required tablespace and temp tablespace also.

So add more datafiles and tempfles if your datafiles and tempfile reached 32G.






90 comments:

  1. thanks that articles great..

    ReplyDelete
  2. Hi ,

    The script (Take Top list ( preferably 10) of tables for fragmenation), is not working for table/index partitions. So used dbms_space package for it.

    https://www.sakthidbtech.com/blog/view/oracle-find-fragmentation

    Thanks

    ReplyDelete
  3. Very useful...awsome...very much self explainatary !

    ReplyDelete
  4. is there any way to check and remover fragmentation of partition table and cluster table. Because move is not working for those tables.

    ReplyDelete
    Replies
    1. Use alter table TABLE_NAME move partition PARTITION_NAME [ONLINE, etc]

      Delete
  5. Nice post , It is the excellent information.

    ReplyDelete
  6. very good post .nice to understand fragmentation

    ReplyDelete
  7. Are you thinking to use Advantages of Oracle Database for your work or business? A reliable storage will store the data from a database, that's the main purpose of using DBMS. Using ACID test, Oracle ensures the top level reliability of your system. As well as, delivers high integrity of data storage.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Thanks for this information . I have many doubt about the same . But this blog clear all my doubt . Thanks once again .

    ReplyDelete
  10. Awesome Info. Thanks for sharing.

    ReplyDelete
  11. Thank you ever so for you article.Much thanks again. Want more.
    oracle sql plsql online course

    ReplyDelete
  12. Thanks for sharing such worthy content, this information is useful for knowledge seekers. Waiting for a more upcoming post like this.
    Elements Of Graphic Design
    Graphics Elements

    ReplyDelete
  13. Thank you for posting your article. Please share more of this because we want to know more about it. These Insights it is too important. Thank you

    Its time to get the best printer for best picture and quality photo. Every where many brands printer will be available but I recommended canon ij setup for your home and office. Please share our link and get the best deals now.

    ReplyDelete
  14. Thanks for sharing such worthy content, this information is useful for knowledge seekers. Dedicated server Hosting

    ReplyDelete
  15. Youtube.com/activate
    To get a youtube.com/enact code for the app, customers need to sign in to their Google account through the Gmail or Google+ account linked to their YouTube account. a record is modified, the customer is prompted to provide access to their YouTube gadget and app

    ReplyDelete
  16. This comment has been removed by a blog administrator.

    ReplyDelete
  17. This comment has been removed by a blog administrator.

    ReplyDelete
  18. This comment has been removed by a blog administrator.

    ReplyDelete
  19. This comment has been removed by a blog administrator.

    ReplyDelete
  20. This comment has been removed by a blog administrator.

    ReplyDelete
  21. This comment has been removed by a blog administrator.

    ReplyDelete
  22. We appreciate your publishing this content. We would like to know more about this, so please share more. These revelations are far too crucial. Many thanks.

    To further refresh, it's time to eat some chicken fillet burgers.

    ReplyDelete
  23. Find a provider that specializes in high-risk merchants like those that were identified above and looks for one of those. In most cases, you will be able to complete an application online; however, certain suppliers may require you to submit physical documentation in addition to your online application. Your chances of being approved are determined by a number of factors, some of which are your line of work, where you live, and your credit score.

    https://paylessmerchantsolutions.com/high-risk/

    ReplyDelete
  24. This comment has been removed by a blog administrator.

    ReplyDelete
  25. This comment has been removed by a blog administrator.

    ReplyDelete
  26. This comment has been removed by a blog administrator.

    ReplyDelete
  27. This comment has been removed by a blog administrator.

    ReplyDelete
  28. This comment has been removed by a blog administrator.

    ReplyDelete
  29. This comment has been removed by a blog administrator.

    ReplyDelete
  30. This comment has been removed by a blog administrator.

    ReplyDelete
  31. This comment has been removed by a blog administrator.

    ReplyDelete
  32. This comment has been removed by a blog administrator.

    ReplyDelete
  33. This comment has been removed by a blog administrator.

    ReplyDelete
  34. This comment has been removed by a blog administrator.

    ReplyDelete
  35. This comment has been removed by a blog administrator.

    ReplyDelete
  36. This comment has been removed by a blog administrator.

    ReplyDelete
  37. Such a nice blog . True balance is a loan app, if you are looking for Instant loan without documents
    Visit – True balance

    ReplyDelete
  38. Are you looking for the best Shopify development company in India If yes, you may hear about the RS organisation.

    ReplyDelete
  39. It's a wonderful Blog and Thank you Sharing, I find it very interesting and well thought out and put together. if you’re a looking for a loan app
    Visit- True Balance

    ReplyDelete
  40. Thanks for sharing , such an amazing post. True balance is a loan application, provides personal loan apps india. You can get a hassle free loan without giving your documents and without wasting your time. Must Visit - Truebalance

    ReplyDelete
  41. Your blog is insightful and enriching. Thank you for sharing valuable content. If you are looking for personal loan you can visit Stashfin.

    ReplyDelete
  42. Thank you for sharing this valuable blog. If you are looking for best app for insurance you can visit Stashfin

    ReplyDelete
  43. Very Interesting and valuable blog thanks for sharing. If you are looking for hair curler you can visit Mr. Barber

    ReplyDelete
  44. nice blog post . very informative content

    ReplyDelete
  45. Thank you for sharing this blog. I really like this blog.
    Spectro machine for Lead

    ReplyDelete
  46. Thanks for sharing this blog. I really like this

    ReplyDelete
  47. The smartphone application called LendingKart was created specifically for small and medium enterprises (SMEs). It works like a loan app.

    ReplyDelete
  48. The smartphone application called LendingKart was created specifically for small and medium enterprises (SMEs). It works like a Loan application.

    ReplyDelete
  49. Thanks for providing valuable info...for the Personal loan apps, the True Balance loan app for smartphones makes it simple to acquire Business loans.

    ReplyDelete
  50. Amazing blog for convenient financial services, including Online loan app, are offered via the little lending app True Balance.

    ReplyDelete

  51. Thanks for sharing this insightful post! If you are looking for Critical Illness Insurance you can visit Stashfin.

    ReplyDelete

  52. Thanks for sharing this insightful post! If you are looking for Pocket Insurance you can visit Stashfin.

    ReplyDelete
  53. Thanks for sharing this insightful post! If you are looking for
    Loan Protection Insurance
    you can visit Stashfin.

    ReplyDelete
  54. Thanks for sharing this post If you are looking for SAP Data Migration, TJC Group offers expert solutions for seamless data transfer and integration.

    ReplyDelete
  55. Very informative, Is Mutual Fund Taxable? Bajaj Finserv’s expertise would be helpful.

    ReplyDelete
  56. Experience the ease of financing with Niva Bupa’s Instant Personal Loan app! Perfect for any urgent needs, our app provides quick and hassle-free personal loans right at your fingertips. Get started today!

    ReplyDelete
  57. Hey, are you looking for a Personal Loan Apps India must download - True Balance

    ReplyDelete
  58. Hey, are you looking for a Home Renovation Loan must download - True Balance

    ReplyDelete
  59. Hey, are you looking for a Loan App Get Cash Instantly must download - True Balance

    ReplyDelete

  60. Thanks for sharing. A Personal Loan personal loan is a versatile financial product that allows individuals to borrow a lump sum for various needs, such as debt consolidation, home improvements, medical expenses, or unexpected emergencies.

    ReplyDelete
  61. True balance is a loan app get cash instantly. Its blend of modern technology, user-centric design, and financial flexibility make it a standout choice in the world of digital lending. Download the app today.

    ReplyDelete
  62. Thanks for sharing this useful information. Stashfin's Critical Illness Insurance provides financial support when you're diagnosed with a serious illness such as cancer, heart disease, or stroke. This insurance helps cover medical expenses, recovery costs, and living expenses during your treatment. With a lump sum payout, Stashfin ensures you can focus on recovery without the burden of financial stress.

    ReplyDelete
  63. Thanks for sharing such an amazing post. True Balance offers instant loans online
    , providing a seamless application process that allows you to access funds quickly and easily.

    ReplyDelete
  64. With True Balance's Instant cash loan service, borrowing is hassle-free. Say goodbye to waiting weeks or days for approvals. Their easy-to-use loan application facilitates the entire procedure, from applying to getting funds. This implies that you can use your phone to apply for a fast cash loan at any time, anyplace.

    ReplyDelete
  65. Pearl Water Technologies offers the best water softener for washing machine, ensuring your clothes stay fresh and clean. Highly recommend!

    ReplyDelete
  66. "Pearl Water Technologies' Washing Machine Softener
    is a game-changer! It keeps appliances running smoothly and improves laundry results. Highly recommend for every household."


    ReplyDelete
  67. Thanks for such informative content. To know about personal loan apply online visit True Balance.

    ReplyDelete

Translate >>