473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to reduce table size using delete or truncate statement in 10G

How to reduce the table size for any table while using truncate or delete statements. In oracle 8i it was truncating the storage space but in 10g it is not ....

I have given truncate statement in a procedure to drop the storage of a table used... when its record count exceeds 500.

Expand|Select|Wrap|Line Numbers
  1. BEGIN
  2.  
  3. i:= 0;
  4.  
  5. FOR CC IN NLD LOOP
  6.  
  7. DELETE FROM CHN_BED5A_T4_FX ;
  8.  
  9. COMMIT;
  10.  
  11. DELETE FROM CHN_BED5B_T4_FX ;
  12.  
  13. COMMIT;
  14.  
  15.  
  16. if i=500
  17. then 
  18. EXECUTE IMMEDIATE('TRUNCATE table CHN_BED5A_T4_FX');
  19. EXECUTE IMMEDIATE('TRUNCATE table CHN_BED5B_T4_FX');
  20. i:= 0;
  21. end if;
  22.  
  23. commit;
  24.  

This is the part where i truncate..

Can anyone help me with truncating the storage space as well since i am not able to run my reports with this increasing table size.....
Dec 21 '09 #1
8 7659
OraMaster
135 100+
Hi
Please try to put correct code enclosed with code tags when you ask for help.
Also do you want to reduce the size of datafile by releasing unused extents or you want to keep not more than 500 records in your tables? Please be specific.
Best Regds,
Bhushan
Dec 21 '09 #2
amitpatel66
2,367 Expert 2GB
When ever you perform a TRUNCATE, the water mark is reset and the space occupied by the data in the table is released.

In case of delete, the data is deleted and moved to rollback segments, but the storage space is not released. The Water mark remains at same level.

For more detail, Check here
Dec 21 '09 #3
Hi ,

I am sorry for not pasting the complete code... here is the complete one...

Expand|Select|Wrap|Line Numbers
  1. SET SERVEROUTPUT ON;
  2.  
  3. DECLARE
  4.  
  5. i number;
  6.  
  7. CURSOR NLD IS
  8. SELECT A.ROWID AS TMP_ROWID,A.* FROM CHN_BILLREFDUMP3_T1_FX A WHERE A.STAT=0 AND A.STRM=1;
  9.  
  10.  
  11. BEGIN
  12.  
  13. i:= 0;
  14.  
  15. FOR CC IN NLD LOOP
  16.  
  17. DELETE FROM CHN_BED3A_T4_FX ;
  18.  
  19. COMMIT;
  20.  
  21. DELETE FROM CHN_BED3B_T4_FX ;
  22.  
  23. COMMIT;
  24.  
  25.  
  26. if i=500
  27. then 
  28. EXECUTE IMMEDIATE('TRUNCATE table CHN_BED3A_T4_FX');
  29. EXECUTE IMMEDIATE('TRUNCATE table CHN_BED3B_T4_FX');
  30. i:= 0;
  31. end if;
  32.  
  33. commit;
  34.  
  35.  
  36.  
  37. INSERT /*+ append */ INTO CHN_BED3A_T4_FX
  38. SELECT /*+ ordered*/ 0,
  39. A.JURISDICTION,A.RATE_DT,A.PRIMARY_UNITS,A.SECOND_UNITS,A.THIRD_UNITS,A.RATED_UNITS,
  40. A.AMOUNT,B.MSG_ID,B.MSG_ID2,B.MSG_ID_SERV,B.SPLIT_ROW_NUM,B.ACCOUNT_NO,B.SUBSCR_NO,
  41. B.SUBSCR_NO_RESETS,B.BILL_REF_NO AS INDEX_BILL_REF,B.BILL_REF_RESETS,B.BILLED_AMOUNT,
  42. B.DISCOUNT,B.TRANS_DT,B.TYPE_ID_USG
  43. FROM chn_cdr_data_CU3 A,chn_cdr_billed_CU3 B
  44. WHERE a.CDR_DATA_PARTITION_KEY=b.CDR_DATA_PARTITION_KEY and
  45. A.MSG_ID=B.MSG_ID AND A.MSG_ID2=B.MSG_ID2 AND  A.MSG_ID_SERV=B.MSG_ID_SERV AND B.BILL_REF_NO=CC.INDEX_BILL_REF AND B.BILLED_AMOUNT>0 AND A.SPLIT_ROW_NUM=B.SPLIT_ROW_NUM;
  46.  
  47. COMMIT;
  48.  
  49.  
  50. INSERT /*+ append */ INTO CHN_BED3B_T4_FX
  51. SELECT /*+ ordered*/ MSG_ID,MSG_ID2,MSG_ID_SERV,BILL_REF_NO,COUNT(*) FROM chn_cdr_billed_CU3 WHERE BILLED_AMOUNT>0 AND
  52. BILL_REF_NO=CC.INDEX_BILL_REF GROUP BY MSG_ID,MSG_ID2,MSG_ID_SERV,BILL_REF_NO;
  53.  
  54. COMMIT;
  55.  
  56. --DUMPING ALL THE CALLS WITH CALL SPLIT =1  EVEN IT IS AIRTIME OR PSTN
  57.  
  58. UPDATE CHN_BED3A_T4_FX A SET A.CNT=(SELECT NCNT FROM CHN_BED3B_T4_FX B WHERE  B.INDEX_BILL_REF=A.INDEX_BILL_REF AND NCNT=1
  59. AND A.MSG_ID=B.MSG_ID AND A.MSG_ID2=B.MSG_ID2 AND  B.MSG_ID_SERV=A.MSG_ID_SERV) ;
  60.  
  61. COMMIT;
  62.  
  63. UPDATE CHN_BED3A_T4_FX A SET A.CNT=(SELECT NCNT FROM CHN_BED3B_T4_FX B WHERE  B.INDEX_BILL_REF=A.INDEX_BILL_REF AND NCNT<>1
  64. AND A.MSG_ID=B.MSG_ID AND A.MSG_ID2=B.MSG_ID2 AND  B.MSG_ID_SERV=A.MSG_ID_SERV) WHERE
  65. ( CNT=0 OR CNT IS NULL) AND A.TYPE_ID_USG IN ( SELECT SUBTYPE_CODE FROM mis.CHTYPEMAST2 WHERE UPPER(ITEM_DESCRIPTION) LIKE '%AIR%'
  66. OR UPPER(ITEM_DESCRIPTION) LIKE '%HIERARCHY%');
  67.  
  68. COMMIT;
  69.  
  70. DELETE FROM CHN_BED3A_T4_FX WHERE CNT IS NULL;
  71.  
  72. INSERT /*+ append */ INTO CHN_BILLBKUP3BDUMP_T3_FX
  73. SELECT /*+ ordered*/ SUBSCR_NO,SUBSCR_NO_RESETS,TYPE_ID_USG,INDEX_BILL_REF,JURISDICTION,SUM(PRIMARY_UNITS) AS ACTDURN,
  74. SUM(RATED_UNITS) AS CHARGED_UNITS,COUNT(*) AS CALLCOUNT ,SUM(SECOND_UNITS) AS ROAMDURN
  75. FROM CHN_BED3A_T4_FX
  76. GROUP BY SUBSCR_NO,SUBSCR_NO_RESETS,TYPE_ID_USG,INDEX_BILL_REF,JURISDICTION;
  77.  
  78. COMMIT;
  79.  
  80. INSERT /*+ append */ INTO CHN_BILLBKUP3_T2_FX (INDEX_BILL_REF,SUBSCR_NO,SUBSCR_NO_RESETS,  SUBTYPE_CODE, RATE_TYPE, REVENUE_TYPE,
  81. ITEM_CODE,  ITEM_DESCRIPTION,AMOUNT,TAX,DISCOUNT, NET_AMT_IN_RUPEES)
  82. SELECT  /*+ ordered*/
  83. BID.BILL_REF_NO,BID.SUBSCR_NO,SUBSCR_NO_RESETS,BID.SUBTYPE_CODE,BID.RATE_TYPE,
  84. DECODE(BID.TYPE_CODE,2,'RC',3,'NRC',1,'PAY',7,'USG',4,'ADJ',6,'UC',TYPE_CODE,'OTH') AS REVENUE_TYPE,
  85. BID.SUBTYPE_CODE AS ITEM_CODE,
  86. DES.DESCRIPTION_TEXT AS ITEM_DESCRIPTION,
  87. SUM(BID.AMOUNT) /100     AS AMOUNT           ,
  88. SUM(BID.TAX )       /100         AS TAX   ,
  89. SUM(BID.DISCOUNT)    /100      AS DISCOUNT     ,
  90. SUM(BID.AMOUNT+BID.TAX+BID.DISCOUNT) /100 AS NET_AMT_IN_RUPEES
  91. FROM  chn_bill_invoice_detail_CU3 BID, chn_descriptions_CU3 DES
  92. WHERE BID.BILL_REF_NO = CC.INDEX_BILL_REF
  93. AND BID.subtype_code <>'-1050'
  94. AND BID.DESCRIPTION_CODE = DES.DESCRIPTION_CODE(+)
  95. AND BID.TYPE_CODE IN (2,3,4,7)
  96. GROUP BY BID.BILL_REF_NO,BID.SUBSCR_NO,SUBSCR_NO_RESETS,DECODE(BID.TYPE_CODE,2,'RC',3,'NRC',1,'PAY',7,'USG',4,'ADJ',6,'UC',TYPE_CODE,'OTH'),
  97. BID.SUBTYPE_CODE,DES.DESCRIPTION_TEXT,BID.SUBTYPE_CODE,BID.RATE_TYPE;
  98.  
  99. COMMIT;
  100.  
  101. UPDATE CHN_BILLREFDUMP3_T1_FX SET STAT= 1 WHERE ROWID = CC.TMP_ROWID;
  102.  
  103. COMMIT;
  104.  
  105. i:=i+1;
  106.  
  107. END LOOP;
  108.  
  109. DBMS_OUTPUT.PUT_LINE ('Done');
  110.  
  111. END;
  112. /
  113.  
  114.  
  115.  
  116.  
I truncate since the space given for user id is very limited and i have to run this query is 15 servers using DBlinks.Hence truncating the tablespace is the only way for me.

It was truncating my table size previously but now suddenly i have this problem.
Dec 22 '09 #4
amitpatel66
2,367 Expert 2GB
You might be facing the problem due to delete operation performed nearly 500 times. Make sure you re build your indexes so that the orphan leaf are removed from the Indexes for atleast each 100 delete operations.
Dec 22 '09 #5
Hmm.. thanks for the reply....

Actually the script is really fast and without giving Truncate command there is no other option to reduce the table size to default while creating it...

This was working until 2 days before.I heard there is an option as "Recycle bin ON" which stops the table size from getting reduced....
Dec 23 '09 #6
amitpatel66
2,367 Expert 2GB
That can be the reason. Just try setting RECYCLEBIN to OFF either at system or session level and run your procedure and check.
Dec 23 '09 #7
Yeah got it guys....... The trick is to create the table with initial storage space specified.

Ex .. Create table tmp storage(initial 50M)

so when truncating oracle returns the table space to be 50M .This applies in 10G.
Thanks for the support. :)
Dec 31 '09 #8
amitpatel66
2,367 Expert 2GB
You are Welcome :)
Do post back in case of any further issues

Moderator
Dec 31 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Gordon | last post by:
I have 2 tables t and t1. In this case, t1 is a copy of t. I want to delete rows from t1 based on criteria on the t table and a relationship between t ad t1 (in this case the id column). In the...
3
by: martin | last post by:
Hi, We have a heavily used production server and a table which logs every hit on a web site. This table has grown large over time and we want to clear it down as efficiently as possible. We would...
5
by: Fred | last post by:
Hi, my database size has grown out of control and I need help with the following issues. (I am very new to databases) I am storing financial tick data in one of the tables and after two months...
3
by: LineVoltageHalogen | last post by:
Greeting All, I have a stored proc that dynamically truncates all the tables in my databases. I use a cursor and some dynamic sql for this: ...... create cursor Loop through sysobjects and...
2
by: Hervé Piedvache | last post by:
Bug or problem with PostgreSQL ? version --------------------------------------------------------------- PostgreSQL 7.4.3 on i686-pc-linux-gnu, compiled by GCC 2.95.4 select * from my_table;...
5
by: pankaj_wolfhunter | last post by:
Greetings, I have an application that need to get all the user defined child tables first before their parents. I wrote a query, given in this newsgroup only, as below SELECT o.name FROM...
6
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called...
14
by: Sala | last post by:
Hi I want to truncate all data in database ... pls help me how i ll truncate?
3
by: Lennart | last post by:
Any thoughts on the following scenario anyone? During a performance test I discovered that the application asked one specific query extremely often. It turned out that this particular query...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.