SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Deleting large number of rows from a table & a heap in a VLDB


Deleting large number of rows from a table & a heap in a VLDB

Author
Message
Nakul Vachhrajani
Nakul Vachhrajani
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3236 Visits: 2149
Hello!

Thank-you all for your interest, and valuable feedback.

Some of the points mentioned in the feedback are really great. I will research them and update the article as necessary.

However, for some suggestions (filtered indexes, using partitioned tables, etc) are all great in hindsight. As mentioned, this is based upon a live example, and the schema was in place and no changes were allowed (at least in the case of the heap). That being said, I agree that if a purging solution is being designed from scratch, yes, these are some of the features that absolutely should be used.

Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
UMG Developer
UMG Developer
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3942 Visits: 2204
Thanks for the article, and the discussion, there is a lot of good information here.
Read It
Read It
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 100
I liked the article. I'm a bit confused by your numbers though. You said make note of the original data and log file sizes and then show the before and after file sizes after the delete was performed. There's a significant difference in the original file sizes to the before and after file sizes. I'm not sure what's happening there. Can you give me a little more explanation on those?
Nakul Vachhrajani
Nakul Vachhrajani
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3236 Visits: 2149
Read It (3/29/2011)
I liked the article. I'm a bit confused by your numbers though. You said make note of the original data and log file sizes and then show the before and after file sizes after the delete was performed. There's a significant difference in the original file sizes to the before and after file sizes. I'm not sure what's happening there. Can you give me a little more explanation on those?


Hello!

Good to know that you liked reading my article. I can definitely help you out in understanding the difference in the file sizes. Please find the explaination below:

Case #1 - Deleting Random Data from a table
Initially, we generated our test data, and noted the file sizes. The data and log files came out to 2694MB and 1705MB respectively.
Next, we generated the lookup table, and then executed the purge. The "Before" and "After" values are with respect to the Purge operation, and hence contain the space occupied by the lookup table.

The above also applies to Case #2 - Deleting data from a heap (non-clustered table).

The basic point I was trying to make is that the file sizes remain constant during the purge operation, and hence I have taken the file size measurements accordingly.

Do let me know if you still have any doubts, and I will be more than happy to help you out.

Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Read It
Read It
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 100
I thought that might be the case, but didn't want to assume that. Thank you for the help.
APP_SQL
APP_SQL
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 11
First of all, great article and very insightful so thanks for taking the time to write it. I do have one question about the approach you explained for deleting large quantities of data from tables with clustered index -

Assuming I understood correctly, you are saying that on a daily basis (or some periodicity), move the data you would like deleted into a look-up table. Then, once a week, join the look-up table to the table that data needs to be deleted from and perform delete. And subsequently truncate the lookup table or something. So my question is - in your implementation of this, have you encountered any blocks on that big table that the data needs to be deleted from during the join? What if that big table is frequently being used by other processes? I was planning to try this out and I am somewhat of a novice so I thought I'd check before breaking anything Smile

Also, out of curiosity, I noticed that the look-up table that you created for deleting data from a clustered-index table itself had no clustered index, simply a non-clustered index on the column "UnixDateToDelete". Was this done for a specific reason?

Thanks again for the article!
Nakul Vachhrajani
Nakul Vachhrajani
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3236 Visits: 2149
APP_SQL (4/3/2011)
First of all, great article and very insightful so thanks for taking the time to write it. I do have one question about the approach you explained for deleting large quantities of data from tables with clustered index -

Assuming I understood correctly, you are saying that on a daily basis (or some periodicity), move the data you would like deleted into a look-up table. Then, once a week, join the look-up table to the table that data needs to be deleted from and perform delete. And subsequently truncate the lookup table or something. So my question is - in your implementation of this, have you encountered any blocks on that big table that the data needs to be deleted from during the join? What if that big table is frequently being used by other processes? I was planning to try this out and I am somewhat of a novice so I thought I'd check before breaking anything Smile

Also, out of curiosity, I noticed that the look-up table that you created for deleting data from a clustered-index table itself had no clustered index, simply a non-clustered index on the column "UnixDateToDelete". Was this done for a specific reason?

Thanks again for the article!


Hello, APP_SQL!

Thank-you for your feedback, and I am happy that you liked reading my article.

As far as the concept goes, yes, you have understood correctly. Now, about your questions:

Q1. Whether or not I have encountered any blocks on the tables during the periodic purge cycles?
A1. Our is an on-premise system, and hence, we execute the purge during the weekly IT maintenance window provided to us by the customer (it's a configurable SQL job that does the purge). The window typically varies from 2 to 4 hours, and hence we have to be in and out of the system in about an hour for IT to do the rest of their maintenace. Because they happen during the maintenance window, all interfaces are down and hence, we have not had any blocking issues.
There have been cases where we had to execute the purge online, and even then we did not face any major blocking issues.

As an alternative, you may want to partition your table, and set the lock escalation to AUTO. What this will do is ask SQL Server to escalate locks to the partition, and not to the entire table.

Q2. Why did I use non-clustered index on the lookup table as opposed to a clustered index?
A2. No specific reason as such. Generally speaking, you can use a clustered index on the lookup table as well - no harm at all (in fact, your deletes may be even faster). In our case, we did not want to enforce any constraints or establish any relationships with the lookup table, and hence you will see that no PK-FK has been used. If your design is such that you can allow for a relationship to exist, please go ahead and use the clustered index by creating the "UnixDateToDelete" as Primary Key.

I hope that I was able to answer your questions satisfactorily. If there is anything else I can help you out with, do let me know.

Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
APP_SQL
APP_SQL
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 11
Thanks Nakul! You have answered all my questions. Thanks for the response and once again, thanks for the article.
Stanley Chan
Stanley Chan
SSC-Enthusiastic
SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)

Group: General Forum Members
Points: 106 Visits: 301
A great article and constructive discussion!
I do learn a lot of brilliant methods to delete very large table, but not just simply to make a where clause and wait for it to complete.
Marios Philippopoulos
Marios Philippopoulos
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12186 Visits: 3766
I really liked the lookup-table technique for deleting data from a clustered index.
Will definitely suggest that to our dev teams going forward.
Thank you for the article!

__________________________________________________________________________________
SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables
Persisting SQL Server Index-Usage Statistics with MERGE
Turbocharge Your Database Maintenance With Service Broker: Part 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search