December 4, 2007 at 7:47 pm
I'm a little worried about what will happen when several sources are using it at once while the deletes are running
Good to be worried about these types of things... try it once, manually, with a small number of rows and see. One lock, in and out, really fast. Waits for others to release all locks.
But won't kill you if you don't use it...
--Jeff Moden
Change is inevitable... Change for the better is not.
December 5, 2007 at 12:25 am
Depending on the % of the data you're deleting from your database you may find that it's actually faster to select the data you do want to keep into new tables, drop the old tables, rename new tables and then recreate your constraints...
Joe
December 5, 2007 at 8:10 am
Yeah....that is part of my battery. I'm looking at my production system and seeing what the ratio of preserved records vs deleted records are and if I can get away with using truncate table then all the better.
Thanks for all the great feedback!
December 6, 2007 at 7:40 am
You said that if each client had a staging table you could use truncates. What is preventing you from doing this? If the application requires the staging table to be a specific name, you could look at renaming the individual tables to the require table when you want to use it.
Steve
December 6, 2007 at 8:14 am
... and sometimes its just better to SELECT ... INTO ... the stuff you want to keep and then rebuild indices and other stuff afterwards on that smaller dataset. Just a thought...
December 12, 2007 at 9:15 am
Does anyone know what will happen if I do a TRUNCATE on a table at the same time I am bulk inserting rows into it? I have to truncate a few tables to clean up all the staging data and the method I am using is, I load all data into a table variable (minus the data I want to clean), I trunc the table and then reinsert. This works well even with larger amounts of data. But now I am wondering under high concurrency if I am going to accidentally trunc the incoming data streams.
What is the easiest way to lock down the table for the duration of the trunc procedure? I thought maybe wrapping it in a transaction but am not sure....
so if I have the following code, how do I lock down myTable for duration of the procedure?
declare @tbltable
(
myCols
)
-- trunc this table
begin try
insert into @tbl(myCols )
select myCols from myTbl with (readpast) where customerid = 1
truncate table myTbl
set identity_insert myTbl on
insert into myTbl (myCols )
select myCols from @tbl
set identity_insert myTbl off
end try
begin catch
select @Message = @Message + ERROR_MESSAGE()
raiserror(@Message, 16, 1)
end catch
December 12, 2007 at 10:07 am
the TRUNCATE TABLE operation technically locks the entire table first, so if you're doing a single insert operation, it shouldn't interfere. On the other hand could be ugly if you're batching the inserts.
Try this one. I'm sure someone will chime in on transaction isolation level, but I'm thinking you don't want any reads on this....
begin try
set transaction isolation level repeatable read
BEGIN TRANSACTION
insert into @tbl(myCols )
select myCols from myTbl with (readpast) where customerid = 1
truncate table myTbl
set identity_insert myTbl on
insert into myTbl (myCols )
select myCols from @tbl
set identity_insert myTbl off
commit transaction
end try
begin catch
select @Message = @Message + ERROR_MESSAGE()
IF (XACT_STATE()) = -1
BEGIN
-- N'The transaction is in an uncommittable state.' +
-- 'Rolling back transaction.'
ROLLBACK TRANSACTION;
END;
-- Test whether the transaction is committable.
IF (XACT_STATE()) = 1
BEGIN
-- N'The transaction is committable.' +
-- 'Committing transaction.'
COMMIT TRANSACTION;
END;
raiserror(@Message, 16, 1)
end catch
drop table @tbl
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Viewing 7 posts - 16 through 21 (of 21 total)
You must be logged in to reply to this topic. Login to reply