简体   繁体   中英

SQL - Delete table with 372 million rows starting from first row

I have a table with 372 million rows, I want to delete old rows starting from the first ones without blocking the DB. How can I reach that?

The table have

id | memberid | type |      timeStamp          | message |
1     123        10    2014-03-26 13:17:02.000    text

UPDATE:

I deleted about 30 GB of Space in DB, but my DISC is ON 6gb space yet.. Any suggestion to get that free space?

Thank you in advance!

select 1;
while(@@ROWCOUNT > 0)
begin
   WAITFOR DELAY '00:00:10';
   delete top(10) from tab  where <your-condition>;

end

delete in chunks using above sql

You may want to consider another approach:

  1. Create a table based on the existing one
  2. Adjust the identity column in the empty table to start from the latest value from the old table (if there is any)
  3. Swap the two tables using sp_rename
  4. Copy the records in batches into the new table from the old table
  5. You can do whatever you want with the old table.

BACKUP your database before you start deleting records / play with tables.

the best performance is to query data by id, then:

delete from TABLENAME where id>XXXXX

is the lowest impact you can execute.

You can also divide the operation in suboperations limiting the number of deleted rows for each operation adding ROWCONT declatarion,

example if you want to delete only 5.000.000 of rows per call you can do this:

SET ROWCOUNT=5000000;
delete from TABLENAME where id>XXXXX;

here you can find a reference https://msdn.microsoft.com/it-it/library/ms188774%28v=sql.120%29.aspx?f=255&MSPPError=-2147217396

The answer to the best way to delete rows from an Oracle table is: It depends! In a perfect world where you can take the table offline for maintenance, a complete reorganization is always best because it does the delete and places the table back into a pristine state. We will address the tools for doing large scale deletes and the appropriate methods for each environment.

Factors and tools for massive deletes

The choice of the delete methods depends on many factors:

  1. Is the target table partitioned? Partitioning greatly improves delete performance. For example, it is common to have a large time-based table partition and deleting elderly rows from these table can be as simple as dropping the desired partition. See these notes on managing partitioned tables.

  2. Can you reorganize the table after the delete to remove fragmentation?

  3. What percentage of the table will be deleted? In cases where you are deleting more than 30-50% of the rows in a very large table it is faster to use CTAS to delete from a table than to do a vanilla delete and a reorganization of the table blocks and a rebuild of the constraints and indexes.

  4. Do you want to release the space consumed by the deleted rows? If you know that the empty space will be re-used by subsequent DML then you will want to leave the empty space within the table. Conversely, if you want to released the space back onto the tablespace then you will need to reorganize the table.

There are many tools that you can use to delete from large tables:

  1. dbms_metadata.get_ddl: This procedure wil punch-off the definitions of all table indexes and constraints.

  2. dbms_redefinition: This procedure will reorganize a table while it remains available for updating.

  3. Create Table as Select: You can use CTAS to copy a table while removing rows in bulk.

  4. Rename table: If you copy a table when deleting rows you can rename it back to its original name.

  5. COMMIT: In cases where a delete might run for many hours, even the largest UNDO log will not be able to hold the rollback information and it becomes necessary to do the delete in a PL/SQL loop, issuing a COMMIT every zillion-rows to free-up the undo logs. This approach will be re-startable automatically because the delete will pick-up where it left off as on your last commit checkpoint.

More information visit here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM