Home All Groups Group Topic Archive Search About

Disk full from delete

Author
15 Jun 2009 11:00 PM
tshad
I am deleting over a 1.5 million records which is running my log out of
space and it is set to Siimple Recovery.

So now I need to break up my delete statement into 3 statements.  Just
trying to find the best way.  I am deleting all the records and can't purge.

I planned on doing it this way:

DELETE CLIENT
SELECT TOP 500000 ClientID FROM Client

DELETE CLIENT
SELECT TOP 500000 ClientID FROM Client

DELETE CLIENT
SELECT ClientID FROM Client

This seems to work.

Just curious if there is there a better way?

Thanks,

Tom

Author
15 Jun 2009 11:07 PM
tshad
Turns out that won't work as this is really 2 statement a delete and a
select.

I need to do a Delete/Where to make this work.

Is there a way to say delete 500000 records?

Thanks,

Tom

Show quoteHide quote
"tshad" <t***@pdsa.com> wrote in message
news:OZmoE0g7JHA.1380@TK2MSFTNGP05.phx.gbl...
>I am deleting over a 1.5 million records which is running my log out of
>space and it is set to Siimple Recovery.
>
> So now I need to break up my delete statement into 3 statements.  Just
> trying to find the best way.  I am deleting all the records and can't
> purge.
>
> I planned on doing it this way:
>
> DELETE CLIENT
> SELECT TOP 500000 ClientID FROM Client
>
> DELETE CLIENT
> SELECT TOP 500000 ClientID FROM Client
>
> DELETE CLIENT
> SELECT ClientID FROM Client
>
> This seems to work.
>
> Just curious if there is there a better way?
>
> Thanks,
>
> Tom
>
Are all your drivers up to date? click for free checkup

Author
15 Jun 2009 11:25 PM
Michael Coles
Can you issue a TRUNCATE TABLE statement?

--

========
Michael Coles
"Pro T-SQL 2008 Programmer's Guide"
http://www.amazon.com/T-SQL-2008-Programmer-rsquo-Guide/dp/143021001X


Show quoteHide quote
"tshad" <t***@pdsa.com> wrote in message
news:%232VEe4g7JHA.6004@TK2MSFTNGP02.phx.gbl...
> Turns out that won't work as this is really 2 statement a delete and a
> select.
>
> I need to do a Delete/Where to make this work.
>
> Is there a way to say delete 500000 records?
>
> Thanks,
>
> Tom
>
> "tshad" <t***@pdsa.com> wrote in message
> news:OZmoE0g7JHA.1380@TK2MSFTNGP05.phx.gbl...
>>I am deleting over a 1.5 million records which is running my log out of
>>space and it is set to Siimple Recovery.
>>
>> So now I need to break up my delete statement into 3 statements.  Just
>> trying to find the best way.  I am deleting all the records and can't
>> purge.
>>
>> I planned on doing it this way:
>>
>> DELETE CLIENT
>> SELECT TOP 500000 ClientID FROM Client
>>
>> DELETE CLIENT
>> SELECT TOP 500000 ClientID FROM Client
>>
>> DELETE CLIENT
>> SELECT ClientID FROM Client
>>
>> This seems to work.
>>
>> Just curious if there is there a better way?
>>
>> Thanks,
>>
>> Tom
>>
>
>
Author
16 Jun 2009 12:09 AM
tshad
No.

I have about 15 foreign key refs on it.

What I did end up doing which works but only because I know where to do the
break.

DELETE CLIENT
Where ClientID < 500000

DELETE CLIENT
Where ClientID < 1000000

DELETE CLIENT

But I was curious as to whether there was a way to say delete the 1st 500000
records.

Thanks,

Tom

Show quoteHide quote
"Michael Coles" <admin.AT.geocodenet.DOT.com> wrote in message
news:ub2YuCh7JHA.1712@TK2MSFTNGP03.phx.gbl...
> Can you issue a TRUNCATE TABLE statement?
>
> --
>
> ========
> Michael Coles
> "Pro T-SQL 2008 Programmer's Guide"
> http://www.amazon.com/T-SQL-2008-Programmer-rsquo-Guide/dp/143021001X
>
>
> "tshad" <t***@pdsa.com> wrote in message
> news:%232VEe4g7JHA.6004@TK2MSFTNGP02.phx.gbl...
>> Turns out that won't work as this is really 2 statement a delete and a
>> select.
>>
>> I need to do a Delete/Where to make this work.
>>
>> Is there a way to say delete 500000 records?
>>
>> Thanks,
>>
>> Tom
>>
>> "tshad" <t***@pdsa.com> wrote in message
>> news:OZmoE0g7JHA.1380@TK2MSFTNGP05.phx.gbl...
>>>I am deleting over a 1.5 million records which is running my log out of
>>>space and it is set to Siimple Recovery.
>>>
>>> So now I need to break up my delete statement into 3 statements.  Just
>>> trying to find the best way.  I am deleting all the records and can't
>>> purge.
>>>
>>> I planned on doing it this way:
>>>
>>> DELETE CLIENT
>>> SELECT TOP 500000 ClientID FROM Client
>>>
>>> DELETE CLIENT
>>> SELECT TOP 500000 ClientID FROM Client
>>>
>>> DELETE CLIENT
>>> SELECT ClientID FROM Client
>>>
>>> This seems to work.
>>>
>>> Just curious if there is there a better way?
>>>
>>> Thanks,
>>>
>>> Tom
>>>
>>
>>
>
Author
18 Jun 2009 2:36 AM
Michael Coles
Can you drop the foreign keys, perform the truncate, and recreate the
foreign keys?

--

========
Michael Coles
"Pro T-SQL 2008 Programmer's Guide"
http://www.amazon.com/T-SQL-2008-Programmer-rsquo-Guide/dp/143021001X


Show quoteHide quote
"tshad" <t***@pdsa.com> wrote in message
news:uiit$ah7JHA.1764@TK2MSFTNGP06.phx.gbl...
> No.
>
> I have about 15 foreign key refs on it.
>
> What I did end up doing which works but only because I know where to do
> the break.
>
> DELETE CLIENT
> Where ClientID < 500000
>
> DELETE CLIENT
> Where ClientID < 1000000
>
> DELETE CLIENT
>
> But I was curious as to whether there was a way to say delete the 1st
> 500000 records.
>
> Thanks,
>
> Tom
>
> "Michael Coles" <admin.AT.geocodenet.DOT.com> wrote in message
> news:ub2YuCh7JHA.1712@TK2MSFTNGP03.phx.gbl...
>> Can you issue a TRUNCATE TABLE statement?
>>
>> --
>>
>> ========
>> Michael Coles
>> "Pro T-SQL 2008 Programmer's Guide"
>> http://www.amazon.com/T-SQL-2008-Programmer-rsquo-Guide/dp/143021001X
>>
>>
>> "tshad" <t***@pdsa.com> wrote in message
>> news:%232VEe4g7JHA.6004@TK2MSFTNGP02.phx.gbl...
>>> Turns out that won't work as this is really 2 statement a delete and a
>>> select.
>>>
>>> I need to do a Delete/Where to make this work.
>>>
>>> Is there a way to say delete 500000 records?
>>>
>>> Thanks,
>>>
>>> Tom
>>>
>>> "tshad" <t***@pdsa.com> wrote in message
>>> news:OZmoE0g7JHA.1380@TK2MSFTNGP05.phx.gbl...
>>>>I am deleting over a 1.5 million records which is running my log out of
>>>>space and it is set to Siimple Recovery.
>>>>
>>>> So now I need to break up my delete statement into 3 statements.  Just
>>>> trying to find the best way.  I am deleting all the records and can't
>>>> purge.
>>>>
>>>> I planned on doing it this way:
>>>>
>>>> DELETE CLIENT
>>>> SELECT TOP 500000 ClientID FROM Client
>>>>
>>>> DELETE CLIENT
>>>> SELECT TOP 500000 ClientID FROM Client
>>>>
>>>> DELETE CLIENT
>>>> SELECT ClientID FROM Client
>>>>
>>>> This seems to work.
>>>>
>>>> Just curious if there is there a better way?
>>>>
>>>> Thanks,
>>>>
>>>> Tom
>>>>
>>>
>>>
>>
>
>
Author
19 Jun 2009 6:00 PM
tshad
"Michael Coles" <admin.AT.geocodenet.DOT.com> wrote in message
news:09398995-01B5-4ABB-B175-98516414FD87@microsoft.com...
> Can you drop the foreign keys, perform the truncate, and recreate the
> foreign keys?

I can and actually do that for one of the tables.

The problem is that this is a script we will be using over the next 6 months
and foreign keys can change and the names of the foreign keys could change
if someone drops it and re-adds on the customer side.  So I just disable all
foreign keys (actually all constraints) before doing my inserts and then
enable them all.

I actually move the database to a test database first and run the scripts
and fix anything that may have changed and when it works clean I run it on
the actual database.

Thanks,

Tom
Show quoteHide quote
>
> --
>
> ========
> Michael Coles
> "Pro T-SQL 2008 Programmer's Guide"
> http://www.amazon.com/T-SQL-2008-Programmer-rsquo-Guide/dp/143021001X
>
>
> "tshad" <t***@pdsa.com> wrote in message
> news:uiit$ah7JHA.1764@TK2MSFTNGP06.phx.gbl...
>> No.
>>
>> I have about 15 foreign key refs on it.
>>
>> What I did end up doing which works but only because I know where to do
>> the break.
>>
>> DELETE CLIENT
>> Where ClientID < 500000
>>
>> DELETE CLIENT
>> Where ClientID < 1000000
>>
>> DELETE CLIENT
>>
>> But I was curious as to whether there was a way to say delete the 1st
>> 500000 records.
>>
>> Thanks,
>>
>> Tom
>>
>> "Michael Coles" <admin.AT.geocodenet.DOT.com> wrote in message
>> news:ub2YuCh7JHA.1712@TK2MSFTNGP03.phx.gbl...
>>> Can you issue a TRUNCATE TABLE statement?
>>>
>>> --
>>>
>>> ========
>>> Michael Coles
>>> "Pro T-SQL 2008 Programmer's Guide"
>>> http://www.amazon.com/T-SQL-2008-Programmer-rsquo-Guide/dp/143021001X
>>>
>>>
>>> "tshad" <t***@pdsa.com> wrote in message
>>> news:%232VEe4g7JHA.6004@TK2MSFTNGP02.phx.gbl...
>>>> Turns out that won't work as this is really 2 statement a delete and a
>>>> select.
>>>>
>>>> I need to do a Delete/Where to make this work.
>>>>
>>>> Is there a way to say delete 500000 records?
>>>>
>>>> Thanks,
>>>>
>>>> Tom
>>>>
>>>> "tshad" <t***@pdsa.com> wrote in message
>>>> news:OZmoE0g7JHA.1380@TK2MSFTNGP05.phx.gbl...
>>>>>I am deleting over a 1.5 million records which is running my log out of
>>>>>space and it is set to Siimple Recovery.
>>>>>
>>>>> So now I need to break up my delete statement into 3 statements.  Just
>>>>> trying to find the best way.  I am deleting all the records and can't
>>>>> purge.
>>>>>
>>>>> I planned on doing it this way:
>>>>>
>>>>> DELETE CLIENT
>>>>> SELECT TOP 500000 ClientID FROM Client
>>>>>
>>>>> DELETE CLIENT
>>>>> SELECT TOP 500000 ClientID FROM Client
>>>>>
>>>>> DELETE CLIENT
>>>>> SELECT ClientID FROM Client
>>>>>
>>>>> This seems to work.
>>>>>
>>>>> Just curious if there is there a better way?
>>>>>
>>>>> Thanks,
>>>>>
>>>>> Tom
>>>>>
>>>>
>>>>
>>>
>>
>>
>
Author
1 Jul 2009 12:18 AM
Michael Coles
Show quote Hide quote
"tshad" <t***@pdsa.com> wrote in message
news:uVFbTfQ8JHA.4820@TK2MSFTNGP04.phx.gbl...
>
> "Michael Coles" <admin.AT.geocodenet.DOT.com> wrote in message
> news:09398995-01B5-4ABB-B175-98516414FD87@microsoft.com...
>> Can you drop the foreign keys, perform the truncate, and recreate the
>> foreign keys?
>
> I can and actually do that for one of the tables.
>
> The problem is that this is a script we will be using over the next 6
> months and foreign keys can change and the names of the foreign keys could
> change if someone drops it and re-adds on the customer side.  So I just
> disable all foreign keys (actually all constraints) before doing my
> inserts and then enable them all.
>
> I actually move the database to a test database first and run the scripts
> and fix anything that may have changed and when it works clean I run it on
> the actual database.

If you're already dropping foreign keys and recreating them for one table,
why not do it for the other?  You could manually maintain separate scripts
that drop and recreate the foreign keys/constraints as the database changes
are applied.  Or you could use the catalog views to create dynamic SQL
statements to drop and recreate the constraints.  Either way you'd get
better performance and avoid the logging issues by truncating instead of
deleting.

--

========
Michael Coles
"Pro T-SQL 2008 Programmer's Guide"
http://www.amazon.com/T-SQL-2008-Programmer-rsquo-Guide/dp/143021001X
Author
16 Jun 2009 12:33 AM
Plamen Ratchev
Here is how you can delete on batches:

WHILE 1 = 1
BEGIN

   DELETE TOP (500000)
   FROM Client;

   IF @@ROWCOUNT < 500000 BREAK;

END

--
Plamen Ratchev
http://www.SQLStudio.com
Author
17 Jun 2009 4:46 PM
tshad
"Plamen Ratchev" <Pla***@SQLStudio.com> wrote in message
news:QdednRkintbCdavXnZ2dnUVZ_sSdnZ2d@speakeasy.net...
> Here is how you can delete on batches:
>
> WHILE 1 = 1
> BEGIN
>
>   DELETE TOP (500000)
>   FROM Client;
>
>   IF @@ROWCOUNT < 500000 BREAK;
>
> END
>
I like that one and it worked great.

Thanks,

Tom
Show quoteHide quote
> --
> Plamen Ratchev
> http://www.SQLStudio.com
Author
17 Jun 2009 3:28 PM
John Bell
Show quote Hide quote
"tshad" <t***@pdsa.com> wrote in message
news:OZmoE0g7JHA.1380@TK2MSFTNGP05.phx.gbl...
>I am deleting over a 1.5 million records which is running my log out of
>space and it is set to Siimple Recovery.
>
> So now I need to break up my delete statement into 3 statements.  Just
> trying to find the best way.  I am deleting all the records and can't
> purge.
>
> I planned on doing it this way:
>
> DELETE CLIENT
> SELECT TOP 500000 ClientID FROM Client
>
> DELETE CLIENT
> SELECT TOP 500000 ClientID FROM Client
>
> DELETE CLIENT
> SELECT ClientID FROM Client
>
> This seems to work.
>
> Just curious if there is there a better way?
>
> Thanks,
>
> Tom
Hi Tom

Look at the reply by Plamen although this syntax is not available on SQL
2000

I think the original syntax was supposed to be:

DELETE Client
FROM (SELECT TOP 500000 ClientID FROM Client ) AS t1
WHERE Client.au_id = t1.ClientID

You could also set ROWCOUNT

SET ROWCOUNT 500000;

DELETE  FROM Client;

WHILE @@ROWCOUNT>0
    DELETE  FROM Client;
SET ROWCOUNT 0;

John;
Author
17 Jun 2009 4:47 PM
tshad
Show quote Hide quote
"John Bell" <jbellnewspo***@hotmail.com> wrote in message
news:%230016A27JHA.4100@TK2MSFTNGP06.phx.gbl...
>
> "tshad" <t***@pdsa.com> wrote in message
> news:OZmoE0g7JHA.1380@TK2MSFTNGP05.phx.gbl...
>>I am deleting over a 1.5 million records which is running my log out of
>>space and it is set to Siimple Recovery.
>>
>> So now I need to break up my delete statement into 3 statements.  Just
>> trying to find the best way.  I am deleting all the records and can't
>> purge.
>>
>> I planned on doing it this way:
>>
>> DELETE CLIENT
>> SELECT TOP 500000 ClientID FROM Client
>>
>> DELETE CLIENT
>> SELECT TOP 500000 ClientID FROM Client
>>
>> DELETE CLIENT
>> SELECT ClientID FROM Client
>>
>> This seems to work.
>>
>> Just curious if there is there a better way?
>>
>> Thanks,
>>
>> Tom
> Hi Tom
>
> Look at the reply by Plamen although this syntax is not available on SQL
> 2000
>

Which part is not availble in 2000 - the TOP statement?

Thanks,

Tom

Show quoteHide quote
> I think the original syntax was supposed to be:
>
> DELETE Client
> FROM (SELECT TOP 500000 ClientID FROM Client ) AS t1
> WHERE Client.au_id = t1.ClientID
>
> You could also set ROWCOUNT
>
> SET ROWCOUNT 500000;
>
> DELETE  FROM Client;
>
> WHILE @@ROWCOUNT>0
>    DELETE  FROM Client;
> SET ROWCOUNT 0;
>
> John;
Author
17 Jun 2009 4:53 PM
Aaron Bertrand [SQL Server MVP]
I think TOP (n) will break in 2000 (incorrect syntax), should be TOP n
(without the parens).


Show quoteHide quote
>> Look at the reply by Plamen although this syntax is not available on SQL
>> 2000
>>
>
> Which part is not availble in 2000 - the TOP statement?
Author
17 Jun 2009 8:07 PM
Plamen Ratchev
>
> Which part is not availble in 2000 - the TOP statement?
>

Correct, the DELETE statement in SQL Server 2000 does not support the
TOP clause.

--
Plamen Ratchev
http://www.SQLStudio.com
Author
17 Jun 2009 8:26 PM
Aaron Bertrand [SQL Server MVP]
Oh yeah, that too.  Amazing how soon we forget.  <shrug>


On 6/17/09 4:07 PM, in article
JMWdnSceWoxy0aTXnZ2dnUVZ_vWdn***@speakeasy.net, "Plamen Ratchev"
<Pla***@SQLStudio.com> wrote:

Show quoteHide quote
>>
>> Which part is not availble in 2000 - the TOP statement?
>>
>
> Correct, the DELETE statement in SQL Server 2000 does not support the
> TOP clause.
Author
18 Jun 2009 2:37 AM
Michael Coles
DELETE TOP is not available in 2000

--

========
Michael Coles
"Pro T-SQL 2008 Programmer's Guide"
http://www.amazon.com/T-SQL-2008-Programmer-rsquo-Guide/dp/143021001X


Show quoteHide quote
"tshad" <t***@pdsa.com> wrote in message
news:OQbt9s27JHA.5828@TK2MSFTNGP04.phx.gbl...
>
> "John Bell" <jbellnewspo***@hotmail.com> wrote in message
> news:%230016A27JHA.4100@TK2MSFTNGP06.phx.gbl...
>>
>> "tshad" <t***@pdsa.com> wrote in message
>> news:OZmoE0g7JHA.1380@TK2MSFTNGP05.phx.gbl...
>>>I am deleting over a 1.5 million records which is running my log out of
>>>space and it is set to Siimple Recovery.
>>>
>>> So now I need to break up my delete statement into 3 statements.  Just
>>> trying to find the best way.  I am deleting all the records and can't
>>> purge.
>>>
>>> I planned on doing it this way:
>>>
>>> DELETE CLIENT
>>> SELECT TOP 500000 ClientID FROM Client
>>>
>>> DELETE CLIENT
>>> SELECT TOP 500000 ClientID FROM Client
>>>
>>> DELETE CLIENT
>>> SELECT ClientID FROM Client
>>>
>>> This seems to work.
>>>
>>> Just curious if there is there a better way?
>>>
>>> Thanks,
>>>
>>> Tom
>> Hi Tom
>>
>> Look at the reply by Plamen although this syntax is not available on SQL
>> 2000
>>
>
> Which part is not availble in 2000 - the TOP statement?
>
> Thanks,
>
> Tom
>
>> I think the original syntax was supposed to be:
>>
>> DELETE Client
>> FROM (SELECT TOP 500000 ClientID FROM Client ) AS t1
>> WHERE Client.au_id = t1.ClientID
>>
>> You could also set ROWCOUNT
>>
>> SET ROWCOUNT 500000;
>>
>> DELETE  FROM Client;
>>
>> WHILE @@ROWCOUNT>0
>>    DELETE  FROM Client;
>> SET ROWCOUNT 0;
>>
>> John;
>
>

Bookmark and Share