My guess is that you don't have a covering index for the id column of your table.

That's easy to fix: create an index on the table (preferably an unique index) that has 'id' as one of the keys.

The thing you need to worry about is the danger of putting the entire delete in a single tran is that you may fill up your transaction log. 20,000 rows isn't that big and neither is 200k row tables. Let's cover that:

Say you have a query that easily fills up the transaction log of the Sybase ASE server: 

delete from rep_queues_archive where sample_date < dateadd(wk, -1, getdate())

How would you break up the transaction so it doesn't fill up the log?  There are several ways to do it, but I'll just cover two of them:

  1. Break up the transaction based on the actual data.
    1. delete from rep_queues_archive where sample_date < dateadd(wk, -52, getdate()
    2. delete from rep_queues_archive where sample_date < dateadd(wk, -51, getdate())
    3. etc.
  2. Break up the transaction based on the number of rows affected.
declare @rows_affected int
declare @dbName varchar(50)
select @dbName = db_name()
set rowcount 1000

select @rows_affected = 1

while @rows_affected > 0
begin
    delete from rep_queues_archive where sample_date < dateadd(wk, -1, getdate())
    select @rows_affected = @@rowcount

    if @@error = 0
    begin
        commit tran
        dump tran @dbName with truncate only
   end
    else
        rollback tran
end

set rowcount 0

Of course, if you are performing incremental backups, you will want to modify the dump tran @dbName with truncate only line to read something like dump tran @dbName to "/sybdumps/mydb_tran.dmp". Modify to suit your naming standard.

HTH,

Jason L. Froebe

Help find a cure for breast cancer! Net proceeds benefit the Susan G. Komen Breast Cancer Foundation and the National Philanthropic Trust. Help by donating - I'm walking 60 miles in 3 days in August 2007. (The day I return from TechWave is the first day of the Walk).

Blog, Tech Blog


In reply to Re: Perl DBI (sybase) rapidly delete large number of records by jfroebe
in thread Perl DBI (sybase) rapidly delete large number of records by simm42

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.