Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As others have mentioned, when you delete 42 million rows, the db has to log 42 million deletions against the database. Thus, the transaction log has to grow substantially. What you might try is to break up the delete into chunks. In the following query, I use the NTile ranking function to break up the rows into 100 buckets. If that is too slow, you can expand the number of buckets so that each delete is smaller. It will help tremendously if there is an index on <code>owner_cats.id_owner</code>, <code>owner_cats.id_cats</code> and <code>cats.id_cat</code> (which I assumed the primary key and numeric).</p> <pre><code>Declare @Cats Cursor Declare @CatId int --assuming an integer PK here Declare @Start int Declare @End int Declare @GroupCount int Set @GroupCount = 100 Set @Cats = Cursor Fast_Forward For With CatHerd As ( Select cats.id_cat , NTile(@GroupCount) Over ( Order By cats.id_cat ) As Grp From cats Join owner_cats On owner_cats.id_cat = cats.id_cat Where owner_cats.id_owner = 1 ) Select Grp, Min(id_cat) As MinCat, Max(id_cat) As MaxCat From CatHerd Group By Grp Open @Cats Fetch Next From @Cats Into @CatId, @Start, @End While @@Fetch_Status = 0 Begin Delete cats Where id_cat Between @Start And @End Fetch Next From @Cats Into @CatId, @Start, @End End Close @Cats Deallocate @Cats </code></pre> <p>The notable catch with the above approach is that it is not transactional. Thus, if it fails on the 40th chunk, you will have deleted 40% of the rows and the other 60% will still exist. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload