Deleting 10 million rows from table

  • A lot of unwanted rows got entered into a table due to a faulty script. This caused the database to grow by 6GB. I need to delete these rows. The table has a unique non-clustered index only, no keys. I tried deleting the rows in chunks.

    After deleting about a million rows, I noticed that though the row count has decreased, the table size is growing. I am confused. I stopped deleting the rows till I can figure out why the table size is not decreasing. Can someone please explain to me what is going on. Thanks.

  • vsuresh (1/30/2015)


    A lot of unwanted rows got entered into a table due to a faulty script. This caused the database to grow by 6GB. I need to delete these rows. The table has a unique non-clustered index only, no keys. I tried deleting the rows in chunks.

    After deleting about a million rows, I noticed that though the row count has decreased, the table size is growing. I am confused. I stopped deleting the rows till I can figure out why the table size is not decreasing. Can someone please explain to me what is going on. Thanks.

    If you are saying the table is a heap, i.e. no clustered index, then you are observing expected behaviour:

    http://sqlmag.com/blog/curious-case-empty-heap-table

    https://technet.microsoft.com/en-us/library/ms189245(v=sql.105).aspx

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Thank you for replying. Do you know why the table size would increase though? If the pages are not getting deallocated because it is a heap, at least wouldn't it remain the same. I would just like to understand better, hence the question. Thanks again.

  • Aren't people also adding rows at the same time?

    --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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I did this during off hours when no one else is accessing that table, hence I'm puzzled.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply