Shrink Oracle Table after Deletion

A few database tables are very big. An Oracle table still holds the disk space occupied by deleted records according to http://stackoverflow.com/questions/6540546/oracle-10g-table-size-after-deletion-of-multiple-rows. Re: shrink table after delete teaches the following commands to release the idle space from the table.
ALTER TABLE TABLE1 DEALLOCATE UNUSED;
ALTER TABLE TABLE1 ENABLE ROW MOVEMENT;
ALTER TABLE TABLE1 SHRINK SPACE;
ALTER TABLE TABLE1 MOVE;
1. Are the commands feasible?
2. Are they safe?
3. What will be the impacts of running the commands?
4. Is there any other workable safe approach shrinking a table?

Hi,
I advise using shrink table operation.
The tablespace which belong to your table must be in ASSM (Automatic segment space management) to use shrink.
Shrink is safe but you need to run two commands :
1)ALTER TABLE TABLE1 SHRINK SPACE COMPACT; (This is long operation which moves data, but can be done online)
2)ALTER TABLE TABLE1 SHRINK SPACE; (this is quick if you run SHRINK SPACE COMPACT before, it only shift the High Water Mark. Be carreful if you don't run SHRINK SPACE COMPACT before your table will be locked for a long time)
Another point, is that execution plans are calculed using the HWM so SHRINK the table (The 2nd command) will invalidate all the cursors in shared pool where the table is in. So execution plan need to be recalculated (often not a problem).
Regards,

Similar Messages

  • Shrink table after delete

    Hi,
    I want to remove some data from a Oracle table, too free up some space on hard drive.
    I use delete clause with where condition to delete data, but then after i would need some shrink clause to shrink up the table as i understand.
    I cant use truncate because it doesn't allow to use where clause. I need something like:
    delete from table 1 where condition1;
    shrink table1; --rfree up space                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    Simply deleting rows doesn't make blocks eligible for deallocation. Try this:
    truncate table mette reuse storage;
    CALL DBMS_STATS.GATHER_TABLE_STATS(USER,'mette');
    SELECT t.blocks used, s.blocks allocated
    FROM   user_tables t JOIN user_segments s ON s.segment_name = t.table_name
    WHERE  t.table_name = 'METTE';
       USED ALLOCATED
          0     20480
    alter table mette deallocate unused;
    CALL DBMS_STATS.GATHER_TABLE_STATS(USER,'mette');
    SELECT t.blocks used, s.blocks allocated
    FROM   user_tables t JOIN user_segments s ON s.segment_name = t.table_name
    WHERE  t.table_name = 'METTE';
       USED ALLOCATED
          0         8
    alter table mette allocate extent;
    -- ...same query...
       USED ALLOCATED
          0        16
    alter table mette deallocate unused;
    -- ...same query...
       USED ALLOCATED
          0         8

  • How to purge PERFSTAT.STATS$SQLTEXT table, after deleting snapshots

    I had an alarm on the free space of the PERFSTAT tablespace. In order to free up some space I tried to delete some old StatsPack snapshots, with the following query:
    DELETE from perfstat.stats$snapshot where snap_time < sysdate - 10 ;
    COMMIT;
    After running it the space usage on the tablespace was not significantly reduced. I checked again and I saw that the table PERFSTAT.STATS$SQLTEXT was very big, almost 8 Gb, but all the other tables were a lot smaller. I read somewhere that the sppurge.sql procedure, that comes with Oracle, could be used to purge the statspack data, but that it comes with the lines related to PERFSTAT.STATS$SQLTEXT commented, because it is a big query and it can take a lot of undo space. I tried to run the followiung query, which I found in the forum, by Don Burleson, but it failed, because of running out of undo:
    DELETE /*+ index_ffs(st)*/
    FROM perfstat.stats$sqltext st
    WHERE (hash_value, text_subset) NOT IN (
    SELECT /*+ hash_aj full(ss) no_expand*/ hash_value, text_subset
    FROM perfstat.stats$sql_summary ss
    WHERE snap_id NOT IN (SELECT DISTINCT snap_id FROM perfstat.stats$snapshot)
    COMMIT;
    Is there any way to know if the PERFSTAT.STATS$SQLTEXT table has records that could be purged, and an easier way, that don't use as much undo, to purge it?
    My oracle version is:
    Oracle9i Enterprise Edition Release 9.2.0.7.0 - 64bit Production
    PL/SQL Release 9.2.0.7.0 - Production
    CORE 9.2.0.7.0 Production
    TNS for Solaris: Version 9.2.0.7.0 - Production
    NLSRTL Version 9.2.0.7.0 - Production
    I. Neva
    Oracle DBA

    Is there any way to know if the PERFSTAT.STATS$SQLTEXT table has records that could be purged, yes, just transform Delete to Select
    SELECT /*+ index_ffs(st)*/ count(*)
    FROM perfstat.stats$sqltext st
    WHERE (hash_value, text_subset) NOT IN (
    SELECT /*+ hash_aj full(ss) no_expand*/ hash_value, text_subset
    FROM perfstat.stats$sql_summary ss
    WHERE snap_id NOT IN (SELECT DISTINCT snap_id FROM perfstat.stats$snapshot)
    and an easier way, that don't use as much undo, to purge it?yes. do it smaller chunks
    BEGIN
    LOOP
    DELETE /*+ index_ffs(st)*/
    FROM perfstat.stats$sqltext st
    WHERE (hash_value, text_subset) NOT IN (
    SELECT /*+ hash_aj full(ss) no_expand*/ hash_value, text_subset
    FROM perfstat.stats$sql_summary ss
    WHERE snap_id NOT IN (SELECT DISTINCT snap_id FROM perfstat.stats$snapshot)
    ) AND ROWNUM<=10000;
    EXIT WHEN SQL%ROWCOUNT = 0;
    COMMIT;
    END LOOP;
    COMMIT;
    END;
    /

  • Restoring the data from table after deletion

    Hi,
    If  I delete the data from the database (using delete command) , is there anyway to restore that data. I know it looks bit weird but I'm checking whether there is any technique in abap by which we can restore the data.
    Your help would be appreciated.
    Thanks,
    Kranthi.

    Hi Kranthi,
               When you delete lines of a database table using the DELETE command, the process is only complete after a database commit. Before the commit, any database change can be reset using a database rollback.To be able to perform a database rollback, a database system must generate a copy of each database object before making a change. These copies are kept in a rollback log until the end of an LUW. An overflow of the rollback log always results in the termination of the transaction which caused it. For example, attempting to remind a large number of defaulting customers in the same transaction could trigger such an error. You can only solve this problem by dividing the set of database updates into several smaller units ( LUWs) for the database system. You use this statement if you cannot be certain that all the database changes have been executed correctly.
    For your information :
    An LUW begins each time you start a transaction,when the database changes of the previous LUW have been confirmed (database commit) or when the database changes of the previous LUW have been cancelled (database rollback)
    An LUW ends when the database changes have been confirmed (database commit) or when the database changes have been canceled (database rollback). Within an LUW, database changes are not made until after a database commit. Prior to this, any database change can be canceled by a database rollback.
    Cheers
    Nishanth

  • Sending email after deleting the records in a table

    Hi
    I am deleting the records in a table. After deleting the records,
    i want to send an email to another person. I am planning to follow this steps.
    1. Create a trigger on the table(AFTER DELETE ON table FOR EACH ROW)
    to copy the deleted records to temporary table.
    2. Read the temporary table and send an email.
    Is there any other way we can do with out creating temporary table ?.
    Govind

    I don't know what you plan to use to send the mail but here's a solution that would work.
    -- Create a send mail procedure
    create or replace procedure send_mail (
    sender      IN VARCHAR2,
    recipient   IN VARCHAR2,
    message     IN VARCHAR2)
    IS
      mailhost VARCHAR2(30) := 'localhost';
      mail_conn utl_smtp.connection;
    BEGIN
    mail_conn :=  utl_smtp.open_connection(mailhost, 25);
      utl_smtp.helo(mail_conn, mailhost);
      utl_smtp.mail(mail_conn, sender);
      utl_smtp.rcpt(mail_conn, recipient);
      utl_smtp.data(mail_conn, message);
      utl_smtp.quit(mail_conn);
    END;
    /-- Create the trigger to email deleted rows
    create or replace trigger email_del_rows
    after delete on <table>
    for each row
    declare
    msg varchar2(2000);
    begin
    msg := 'COL1  COL2  COMPANY NAME  DATE'||chr(10);
    msg := msg||:old.col1||'    '||:old.col2||'    '||:old.company_name||'       '||:old_date|| chr(10);
    msg := msg||'END OF FILE';
    send_mail('SENDER','[email protected]',msg);
    end;
    /You can make it look pretty but you get the basic idea.

  • How to know how much I can shrink the table

    How can I know how much I can shrink the table after firing the delete statement?

    Hi,
    alter table table_name shrink space;
    For example:
    create table HWM_TEST(
      ID      NUMBER,
      DB_USERVARCHAR2(12),
      TARIH   DATE
      STORAGE(
              MAXEXTENTS       UNLIMITED
    insert into HWM_TEST
      select level   id,
              user    db_user,
              sysdate tarih
       from dual
      connect by level<100001;
      commit;
      100000
    rows created.
      Commit complete.
      insert into HWM_TEST
      select * from HWM_TEST;
      insert into HWM_TEST
      select * from HWM_TEST;
      insert into HWM_TEST
      select * from HWM_TEST;
      insert into HWM_TEST
      select * from HWM_TEST;
      insert into HWM_TEST
      select * fromHWM_TEST;
      commit;
      100000
    rows created.
      200000
    rows created.
      400000
    rows created.
      800000
    rows created.
      1600000 rows created.
      Commit complete.
    select count(*) from hwm_test;
       COUNT(*)
    3200000
       1 row selected.
      Elapsed: 00:00:02.26
    delete from HWM_TEST;
      commit;
      COUNT(*)
    0
      1 row selected.
      Elapsed: 00:00:02.42
      alter table HWM_TEST enable row movement;
      alter table HWM_TEST shrink space;
      set timing on
      select count(*) from hwm_test;
    COUNT(*)
    0
       1 row selected.
      Elapsed: 00:00:00.01
    Thank you

  • BOM table for deleted item

    Hi All,
    In which table shall i get the deleted item from Bill of material. I checked in STPO but in this table after deleting the item it will not appear.
    Regards,
    RBS

    Hi  RBS,
    Please go through the Tables  below Pertaining to BOM
    STPO BOM Item Details
    STPU BOM Sub Items (designators)
    STKO BOM Header Details
    MAST BOM Group to Material
    STZU BOM History Records
    STAS BOM Item Selection
    STPF BOM Explosion Structure
    PLMZ Allocation of bill of material items to operations
    Regards
    Varma

  • Table space not reduce after delete in oracle 11G

    Hi Team,
    I have a DB 11.1.0.7 on unix.
    I have execute delete tables on tablespace, but this not reduce.
    Thanks

    935299 wrote:
    What segment space management type is defined for the tablespace in question?
    MANUAL
    Then you should check out the documentation some more.
    But even if you shrink the table segement what is that going to do for the data file size?
    I don't undertand you.
    ThanksYour thread is titled "Table space not reduce after delete in oracle 11G" which implies to me that you are interested in reducing the size of a tablespace (which really means reducing the size of the underlying datafile(s)).
    So, if you shrink the size of the sys.aud$ table, will that cause the datafile(s) to become smaller? Will it accomplish your goal? What else, if anything, needs to happen?

  • Table space not reduce after delete in oracle 10g

    Hi..
    Based on my system, i have found that my oracle table space did not reduce after the deletion query. Why ?.. Could somebody help me. As your info, I am using oracle 10g.
    Thank you,
    Baharin

    After Delete the table space will not be set free. high water mark will not be reset. to regain the space you need to recognize the objects from which you deleted the data. This can be done in many ways.
    1) Move the objects.
    Alter table temp move --> optionally tablespace clause can be used. After this you need to rebuild table indexes.
    2) With 10g table can be shrinked or reorganize to free the space.
    alter table mytable enable row movement;
    alter table mytable shrink space;
    3) Export/Import
    export the objects and drop and recreate with import.

  • TABLESPACE cleanup not happening after table data delete

    Hi all
    I have a partitioned table with 4 partitions all residing in a single tablespace. I populated the table with sample data and then deleted all with a simple delete statement followed by a commit. But the tablespace is still showing data from the 4 partitions under the bytes column in query below:
    select segment_name, partition_name, tablespace_name, bytes
    from dba_segments
    where tablespace_name='tb_name'
    Am I missing something here? Do I need to run some other command after delete and commit to fully flush out the data?
    FYI, my system specs are:
    Windows 2008 Server Standard (32-bit)
    ORACLE 11.1.0.7.0

    HI,
    LMT is Locally Managed Tablespace.
    +Moreover, when you say the blocks will not be available immediately when do you reckon they will be available? How do I go about automating this to free up space as soon as data is deleted?+
    You cannot automate this job, this is already automated by Oracle.
    +FYI, both pct_free and pct_used come up as empty values for my table.+
    reason for a NULL-value for PCT_USED is that the tablespace where the table resides uses automatic segments space management (ASSM). With ASSM there is no need for PCT_USED only for PCT_FREE. Oracle manages blocks automatically.
    You dont worry about releasing the space, Oracle will take care of it automatically.
    Go through the below link
    [http://download.oracle.com/docs/cd/B19306_01/server.102/b14231/tspaces.htm#ADMIN10065]
    [http://download.oracle.com/docs/cd/B19306_01/server.102/b14231/schema.htm#sthref2100]
    [http://www.dba-oracle.com/art_builder_assm.htm]
    This is an extract form Oracle docs
    Understanding Reclaimable Unused Space
    Over time, updates and deletes on objects within a tablespace can create pockets of empty space that individually are not large enough to be reused for new data. This type of empty space is referred to as fragmented free space.
    Objects with fragmented free space can result in much wasted space, and can impact database performance. The preferred way to defragment and reclaim this space is to perform an online segment shrink. This process consolidates fragmented free space below the high water mark and compacts the segment. After compaction, the high water mark is moved, resulting in new free space above the high water mark. That space above the high water mark is then deallocated. The segment remains available for queries and DML during most of the operation, and no extra disk space need be allocated.
    You use the Segment Advisor to identify segments that would benefit from online segment shrink. Only segments in locally managed tablespaces with automatic segment space management (ASSM) are eligible. Other restrictions on segment type exist. For more information, see "Shrinking Database Segments Online".
    If a table with reclaimable space is not eligible for online segment shrink, or if you want to make changes to logical or physical attributes of the table while reclaiming space, you can use online table redefinition as an alternative to segment shrink. Online redefinition is also referred to as reorganization. Unlike online segment shrink, it requires extra disk space to be allocated. See "Redefining Tables Online" for more information.Regards,
    Vijayaraghavan K

  • Insert /delete data from SAP Z table to Oracle table and opposite

    Hi,
    Can u help me write this FM from the SAP side?
    So, I have two tables ZTABLE in SAP and Oracle table ORAC.
    Let's put three columns in each of them, for example
    TEL1
    TEL2
    ADRESS
    NAME
    where TEL field is primary from ZTABLE to ORAC...
    (in FM there shoud be abap code for writing data in ZTABLE after we press some pushbutton made in sap screen painter..)
    for example, when we write new record in ZTABLE
    00
    112233
    Street 4
    Name1
    this data shoud be inserted in Oracle table ORAC.
    when we write new record in Oracle table for example
    01
    445566
    New Street
    Name2
    this data shoud be inserted in ZTABLE.
    Field TEL1 can be only of two values 01 or 02, other combination is not valid...
    I must have all data from Oracle table ORAC in ZTABLE and opposite.
    It should be the same scenario for DELETE...
    And this communication should be online between sap and table in oracle database...
    Can u help me from sap side? and give idea how to configure on oracle side??
    Thanks a lot,
    Nihad

    I dont know if we can directly connect to a oracle database ( wait for the answers from others on this )
    but in XI we have the JDBC adaptor to insert and retrieve data.
    so for the outbound from SAP the flow can be something like this (with XI in landscape):
    1) You have a screen to maintain a new entry / delete an entry
    2) On save , this record gets saved or deleted from the Ztable in SAP
    3)) In the same screen you can call a proxy class-method (generated using SPROXY transaction ) to send the record to XI.
    4) XI to format it and insert into the oracle table
    Mathews

  • How to find out when data was deleted from table in oracle and Who deleted that

    HI Experts,
    Help me for below query:
    how to find out when data was deleted from table in oracle and Who deleted that ?
    I did that to fidn out some data from dba_tab_modifications, but I m not sure that what timestamp shows, wether it shows for update,insert or delete time ?
    SQL> select TABLE_OWNER,TABLE_NAME,INSERTS,UPDATES,DELETES,TIMESTAMP,DROP_SEGMENTS,TRUNCATED from dba_tab_modifications where TABLE_NAME='F9001';
    TABLE_OWNER                    TABLE_NAME                        INSERTS    UPDATES    DELETES     TIMESTAMP         DROP_SEGMENTS TRU
    PRODCTL                        F9001                                                     1683         46       2171            11-12-13 18:23:39             0                   NO
    Audit is enable in my enviroment?
    customer is facing the issue and data missing in the table and I told him that yes there is a delete at 11-12-13 18:23:39 in table after seeing the DELETS column and timestamp in dba_tab_modifications, but not sure I am right or not
    SQL> show parameter audit
    NAME                                 TYPE        VALUE
    audit_file_dest                      string      /oracle/admin/pbowe/adump
    audit_sys_operations                 boolean     TRUE
    audit_syslog_level                   string
    audit_trail                          string      DB, EXTENDED
    please help
    Thanks
    Sam

    LOGMiner --> Using LogMiner to Analyze Redo Log Files
    AUDIT --> Configuring and Administering Auditing

  • Space reusage after deletion in compressed table

    Hi,
    Some sources tell, that free space after DELETE in compressed table is not reused.
    For example, this http://www.trivadis.com/uploads/tx_cabagdownloadarea/table_compression2_0411EN.pdf
    Is it true?
    Unfortunatly I cannot reproduce it.

    Unfortunatly the question is still open.
    In Oracle 9i space, freed after DELETE in compressed block, was not reused in subsequent inserts.
    Isn't it?
    I saw many evidences from other people. One link I gave above.
    But in Oracle 10g I see another figures. After delete rows in compressed blocks, and subsequent insert into that block, block defragmented!
    Please, if who know any documentation about change in this behavior, please post links.
    p.s.
    in 10g:
    1. CTAS compress. Block is full.
    2. after, deleted every 4 from 5 rows.
    avsp=0x3b
    tosp=0x99e
    0x24:pri[0]     offs=0xeb0
    0x26:pri[1]     offs=0xea8 -- deleted
    0x28:pri[2]     offs=0xea0 -- deleted
    0x2a:pri[3]     offs=0xe98 -- deleted
    0x2c:pri[4]     offs=0xe90 -- deleted
    0x2e:pri[5]     offs=0xe88 -- live
    0x30:pri[6]     offs=0xe80 -- deleted
    0x32:pri[7]     offs=0xe78 -- deleted
    0x34:pri[8]     offs=0xe70 -- deleted
    0x36:pri[9]     offs=0xe68 -- deleted
    0x38:pri[10]     offs=0xe60 -- live
    0x3a:pri[11]     offs=0xe58 -- deleted
    0x3c:pri[12]     offs=0xe50 -- deleted
    0x3e:pri[13]     offs=0xe48 -- deleted
    0x40:pri[14]     offs=0xe40 -- deleted
    0x42:pri[15]     offs=0xe38  -- live
    0x44:pri[16]     offs=0xe30 -- deleted
    0x46:pri[17]     offs=0xe28 -- deleted
    0x48:pri[18]     offs=0xe20 -- deleted
    0x4a:pri[19]     offs=0xe18 -- deleted
    0x4c:pri[20]     offs=0xe10 -- live
    ...3. insert into table t select from ... where rownum < 1000;
    Inserted rows were inserted in a several blocks. Total number of not empty blocks was not changed. Chains did not occure.
    Block above looks as follow:
    avsp=0x7d
    tosp=0x7d
    0x24:pri[0]     offs=0xeb0
    0x26:pri[1]     offs=0x776 - new
    0x28:pri[2]     offs=0x84b - new
    0x2a:pri[3]     offs=0x920 - new
    0x2c:pri[4]     offs=0x9f5 - new
    0x2e:pri[5]     offs=0xea8 - old
    0x30:pri[6]     offs=0xaca - new
    0x32:pri[7]     offs=0xb9f - new
    0x34:pri[8]     offs=0x34d - new
    0x36:pri[9]     offs=0x422 - new
    0x38:pri[10]     offs=0xea0 - old
    0x3a:pri[11]     offs=0x4f7 - new
    0x3c:pri[12]     offs=0x5cc - new
    0x3e:pri[13]     offs=0x6a1 - new
    0x40:pri[14]     sfll=16  
    0x42:pri[15]     offs=0xe98 - old
    0x44:pri[16]     sfll=17
    0x46:pri[17]     sfll=18
    0x48:pri[18]     sfll=19
    0x4a:pri[19]     sfll=21
    0x4c:pri[20]     offs=0xe90 -- old
    0x4e:pri[21]     sfll=22
    0x50:pri[22]     sfll=23
    0x52:pri[23]     sfll=24
    0x54:pri[24]     sfll=26As we see, that old rows were defragmented, and repacked, and moved to the bottom of block.
    New rows (inserted after compressing of table) fill remaining space.
    So, deleted space was reused.

  • Table grows to 6 GB with 6k records only after Delete ORA-01653:

    Hello,
    I have a Table that i delete data from using
    DELETE FROM DJ_20255_OUTPUT a where trunc(a.LOADED_DATE) <trunc(sysdate -7);
    COMMIT;
    the issue i have is when i want to repopulate the table i get the Error ORA-01653: unable to extend table.
    The table grows to over 6gb but if i truncate that table and in both cases there is no data left in the table after either action the table will only grow to 0.8 Mb once populated.
    so with truncate table size is 0.8MB and if i use delete from.... the table grows to 6GB
    The repopulation of the table uses mutiple insert statments commiting after each one.
    is this a bug or is there an action i should perform onece I have deleted the data?
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi

    Is this an Index Organized table ? (select IOT_TYPE from user_tables where table_name = 'DJ_20255_OUTPUT' ;)
    Are you saying that you use this sequence :
    DELETE .... in one single call to delete all the rows
    INSERT ... in multiple calls with COMMIT after each row
    Hemant K Chitale

  • Table size not reducing after delete

    The table size in dba_segments is not reducing after we delete the data from the table. How can i regain the space after deleting the data from a table.
    Regards,
    Natesh

    I think when you do DELETE it removes the data but
    it's not releasing any used space and it's still
    marked as used space. I think reorganizing would help
    to compress and pack all block and relase any unused
    space in blocks. Why do you think that? Deleting data will create space that can be reused by subsequent insert/ update operations. It is not going to release space back to the tablespace to make it available for inserts into other tables in the tablespace, but that's not generally an issue unless you are permanently decreasing the size of a table, which is pretty rare.
    Would you also please explain about different about
    LOB and LONG ? or point me to any link which explain
    baout it.From the Oracle Concepts manual's section on the LONG data type
    "Note:
    Do not create tables with LONG columns. Use LOB columns (CLOB, NCLOB) instead. LONG columns are supported only for backward compatibility.
    Oracle also recommends that you convert existing LONG columns to LOB columns. LOB columns are subject to far fewer restrictions than LONG columns. Further, LOB functionality is enhanced in every release, whereas LONG functionality has been static for several releases."
    LONG was a very badly implemented solution to storing large amounts of data. LOBs are a much, much better designed solution-- you should always be using LOBs.
    Justin

Maybe you are looking for