November 18, 2008 at 9:13 am
Hello,
I have a table of 3.7mn records. I need to delete 37,000 records from it.
I have created a temp. lookup table (not # or ## table) which contains the primary key values for the records to be deleted.
But to delete 100 records it takes 1.2 minutes. I have nonclustered index on the primary key.
Is there anyway to delete the records quikly? Using inner join in place of subquery too did not make much of the difference.
My query is as given below:
Delete from
where
)
Please suggest.
Thanks in advance
Rohit
November 18, 2008 at 10:12 am
Try to truncate table
TRUNCATE TABLE name_of_your_table
couz the DELETE command is logged and every deleted action will recorded on log file and if you have million of records sure that you will wait for long time!
November 18, 2008 at 10:37 am
delete from
dbo.MainTable
from
dbo.MainTable mt
inner join dbo.DeleteTable dt -- table of primary keys to be deleted
on (mt.pk_id = dt.pk_id) -- what ever the primary keys are for the tables
This code is untested. I would test it in a test environment before proceeding to a production environment.
November 18, 2008 at 9:46 pm
Dugi (11/18/2008)
Try to truncate tableTRUNCATE TABLE name_of_your_table
couz the DELETE command is logged and every deleted action will recorded on log file and if you have million of records sure that you will wait for long time!
OH!!! NO, NO, NO! Truncate will delete ALL the rows from the table! DO NOT DO THIS FOR THIS PROBLEM. DEATH BY SQL!!!
--Jeff Moden
Change is inevitable... Change for the better is not.
November 18, 2008 at 9:59 pm
Rohit Chitre (11/18/2008)
Hello,I have a table of 3.7mn records. I need to delete 37,000 records from it.
I have created a temp. lookup table (not # or ## table) which contains the primary key values for the records to be deleted.
But to delete 100 records it takes 1.2 minutes. I have nonclustered index on the primary key.
Is there anyway to delete the records quikly? Using inner join in place of subquery too did not make much of the difference.
My query is as given below:
Delete from
where
)
Please suggest.
Thanks in advance
Rohit
d o you have to delete the records on the frequent basis or its a one time task. if it is a one time task. speed should not matters.
kshitij kumar
kshitij@krayknot.com
www.krayknot.com
November 18, 2008 at 10:02 pm
Hi Thanks for the reply. It is one time task. But I have to delete the records before users start using it i.e. before putting it on production.
Thanks & regards,
Rohit
November 18, 2008 at 10:04 pm
The query is running for more than 7 hours and still the task is not completed.
November 18, 2008 at 10:39 pm
Rohit Chitre (11/18/2008)
Hi Thanks for the reply. It is one time task. But I have to delete the records before users start using it i.e. before putting it on production.Thanks & regards,
Rohit
ok i got the point. gimme some time as i have to test the same at my own. just 10 min
kshitij kumar
kshitij@krayknot.com
www.krayknot.com
November 18, 2008 at 10:45 pm
Rohit Chitre (11/18/2008)
The query is running for more than 7 hours and still the task is not completed.
Yes , it takes time
first query processor tried to find out where your targeted data rows are, per the SQL profiler results, this causes huge disk-reads if no appropriate index can be used for large table.
secondly, sql server takes action to 'delete' your data rows, for heap and table with clustered index, the DELETE action is different.
I followed the three ways:
1. same as yours [takes time]
2. use while loop do delete 1000/5000 at a time.
while 1 =1
begin
set rowcount 1000
DELETE FROM MYTABLE WHERE MyDateField <= '2006/01/01'
if @@rowcount = 0 break
end
3.Create a new table, insert into it only the rows you want to keep.
truncate and drop the old table. Rename the new table to be your old one. and recreate all the required Primary Keys, Indexes, Foreign Key Constraints etc.
now you can choose wht u have to do. I will prefer the while loop as sql gets exclusive locks on 1000 rows only not the entire table...so that you can avoid blocking.
kshitij kumar
kshitij@krayknot.com
www.krayknot.com
November 18, 2008 at 10:47 pm
Hi
Delete the records in batches. Look up this site for more info.
"Keep Trying"
November 18, 2008 at 11:31 pm
If you want to delete only 37,000 rows, it should take only six seconds. Do you have any triggers on the table? Does anyone have an uncommitted transaction on the table causing non-deadlocking blocking?
--Jeff Moden
Change is inevitable... Change for the better is not.
November 19, 2008 at 12:17 am
Jeff Moden (11/18/2008)
Dugi (11/18/2008)
Try to truncate tableTRUNCATE TABLE name_of_your_table
couz the DELETE command is logged and every deleted action will recorded on log file and if you have million of records sure that you will wait for long time!
OH!!! NO, NO, NO! Truncate will delete ALL the rows from the table! DO NOT DO THIS FOR THIS PROBLEM. DEATH BY SQL!!!
wOOOps I didn't see the nr of records for deletion! Sorry for that! This is the reason that the suggestion must tested on the test environment to be sure if it works or not ... Sorry again !?
:w00t::w00t::w00t::w00t::w00t::w00t::w00t::w00t::w00t::w00t::w00t::w00t::w00t:
November 19, 2008 at 12:17 am
No we don't have any triggers on it. Also there are no locks. I tried it when no users were working. Still it takes a long time.
November 19, 2008 at 12:21 am
Do you have active constraints ... try to disable constraints and after that try deletion then enable constraints again !
November 19, 2008 at 12:46 am
I tried disbling the constraints. But it did not work
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply