Pattern: Job to delete many records

Pattern for efficient (i.e. use transaction for multiple deletes and use doDelete), stable (i.e. don’t make one huge transaction but commit and restart transaction periodically) and verbose (count down the number of remaining records) job to delete many records in a table.

static void deleteJob(Args _args)
{
    #define.TABLE(myTable)         //set
    #define.CONDITION(true)        //set
    #define.TRANSACTIONSIZE(100)   //set
    #TABLE #TABLE;
    int i = 1;
    ;
    while (i>0)
    {
        i=0;
        info(strfmt("%1",(select count(RecId) from #TABLE where #CONDITION).RecId));
        infolog.viewUpdate();
        ttsbegin;
        while select forupdate #TABLE where #CONDITION
        {
            #TABLE .dodelete();
            i++;
            if(i>=#TRANSACTIONSIZE)
                break;
        }
        ttscommit;
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s