Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

deleting huge records From Table Expand / Collapse
Author
Message
Posted Sunday, November 21, 2010 11:22 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 10:39 AM
Points: 1,194, Visits: 787
i have a table with more than a 30 million Records in it.

because of soem performance related issues we need to delete some unwanted records (around 15 millon)from it and we have identified the filter conditions to remove the same. regarding this i need help on the following.


1) What are all the stpes that i need to take care when we are deleting such huge records from the database

2) How to estimate the time that is going to take when deleting such a huge records.



thanks in advance.
Post #1024149
Posted Sunday, November 21, 2010 11:32 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, October 24, 2013 1:55 AM
Points: 178, Visits: 465
If you have foreign keys, constraints etc it will take a longer time to delete records from sql table.

Also it will be better I guess to disable indexes (delete indexes) and then after deletion of unwanted rows recreate the indexes again. This will save server to update the indexes after every DML operation on the table.



Eralper
SQL Server and T-SQL Tutorials and Articles
Microsoft Certification and Certification Exams
Post #1024151
Posted Sunday, November 21, 2010 11:58 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 10:39 AM
Points: 1,194, Visits: 787
Eralper (11/21/2010)
If you have foreign keys, constraints etc it will take a longer time to delete records from sql table.

Also it will be better I guess to disable indexes (delete indexes) and then after deletion of unwanted rows recreate the indexes again. This will save server to update the indexes after every DML operation on the table.



Thanks for ur Quick Reply, The table does not have any relationship with other tables.
as u said there is an index on the table, so i need to delete the index and recreate it back once the delete operation is done.

what about log file? will it becomes huge in deleting these records?
Post #1024154
Posted Monday, November 22, 2010 12:06 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, October 24, 2013 1:55 AM
Points: 178, Visits: 465
Yes, that is an important topic.
If you could truncate than there will be no problem. But since you want to keep some of the records in the table, then DELETE operation will be done. So log file will have entries for each delete command.

You can also alter the recovery model of your database to keep the log file size smaller.
You can change it to simple recovery model if it is set to full recovery model now.



Eralper
SQL Server and T-SQL Tutorials and Articles
Microsoft Certification and Certification Exams
Post #1024156
Posted Monday, November 22, 2010 12:12 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, February 3, 2011 3:00 AM
Points: 164, Visits: 656

Hi,
Once you delete the 15 million records from that table. then you cannot reuse the unused space and also the db size will not be decrease.If you want to decrease the db size. then you have to shrink the data file. So that only it will recover the unused space.

Thanks
Balaji.G
Post #1024157
Posted Monday, November 22, 2010 12:13 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, October 24, 2013 1:55 AM
Points: 178, Visits: 465
You can also backup transaction log and reduce the size by deleting after 100000 rows for example, then continue with an other 100000 delete, etc.

Eralper
SQL Server and T-SQL Tutorials and Articles
Microsoft Certification and Certification Exams
Post #1024159
Posted Monday, November 22, 2010 12:49 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:14 PM
Points: 35,215, Visits: 31,665
You can get around some of the logging issues by using a SELECT/INTO to copy only the rows you want to keep and then renaming the table(s). Read up in Books Online on how to make SELECT/INTO minimally log.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1024170
Posted Monday, November 22, 2010 12:56 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, October 24, 2013 1:55 AM
Points: 178, Visits: 465
Thanks Jeff,
I checked the BOL and I found :

The amount of logging for SELECT...INTO depends on the recovery model in effect for the database. Under the simple recovery model or bulk-logged recovery model, bulk operations are minimally logged. With minimal logging, using the SELECT… INTO statement can be more efficient than creating a table and then populating the table with an INSERT statement.

So I think populating a new table and then truncating the other might be more efficient when log size is considered.


Eralper
SQL Server and T-SQL Tutorials and Articles
Microsoft Certification and Certification Exams
Post #1024171
Posted Monday, November 22, 2010 1:26 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 10:39 AM
Points: 1,194, Visits: 787
Thanks to Jeff and eralper on your valuable Inputs.
Post #1024183
Posted Monday, November 22, 2010 6:10 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:14 PM
Points: 35,215, Visits: 31,665
Eralper (11/22/2010)
Thanks Jeff,
I checked the BOL and I found :

The amount of logging for SELECT...INTO depends on the recovery model in effect for the database. Under the simple recovery model or bulk-logged recovery model, bulk operations are minimally logged. With minimal logging, using the SELECT… INTO statement can be more efficient than creating a table and then populating the table with an INSERT statement.

So I think populating a new table and then truncating the other might be more efficient when log size is considered.


I agree. Just to be clear for folks that may think of doing it, don't ever shift from FULL recovery to SIMPLE recovery for the sake of archiving data because it will break the log chain for backups. It's ok to shift from FULL recovery to the BULK-LOGGED recovery, though.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1024269
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse