Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Truncate with preserve.

Author  Topic 

wernerdejong
Starting Member

7 Posts

Posted - 2006-07-04 : 10:09:38
Hi all,

Just wondering if there is something like truncate table with preserve space.

Situation.

I daily load a number of flat files into a staging area. Before I load these files, the target tables must be emptied. Truncate table is the fastest way to do this since I do not want the transaction log to be full. (Second, the database is in simple mode) However, when I use the delete * from table command, the space reserved in the filesystem / database files and thus remains allocated to the table. (Which is exactly what I want!) Using truncate does not allocate this space but simply removes a header record (I assume) Thus when I load data to the table the space will be claimed over and over again while I already know the table is around 3gb, 400mb, 5mb etc. in size.

Thanks in advance for any suggestions.

nr
SQLTeam MVY

12543 Posts

Posted - 2006-07-04 : 10:19:44
Not sure what you are asking here.
I think both delete and truncate will deallocate the extents from the table.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

wernerdejong
Starting Member

7 Posts

Posted - 2006-07-04 : 10:26:46
Hi Nr,

Thanx for your reaction. I realise now that I forgot to post I'm using sql 2005. When looking at the report generated by SQL server about space used in a database. (hit f7 when selecting a database in management studio, then on the green button report) you can compare the two statements. Truncate will trash the reserved space and delete from will keep the reserved space / extends in the datafile.

I know from ppl that Oracle can truncate with preserve space and the report gave me some hope.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-07-04 : 12:24:42
Sorry - you're right.
Don't think there is a way to stop truncate dallocating the extents.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-07-05 : 05:14:39
I'd be interested to know if inserts are faster after a delete (keep extends) than a truncate (throw away extends)

(And is the data you are re-inserting similar, index-wise, to what was there before?)

Thanks

Kristen
Go to Top of Page

wernerdejong
Starting Member

7 Posts

Posted - 2006-07-05 : 09:07:28
Hi Kristen,

Per request. ;) I've tested both in SSIS with a dataset of 84.5mb = 143.828 rows
I used the oledb object with fastload on, ignore nulls
When using the truncate table option the entire package took
Compareable run 1: 27.110 seconds
Compareable run 2: 28.188 seconds
Compareable run 3: 27.687 seconds


When using the delete from option the entire package took
Compareable run 1: 29.610 seconds
Compareable run 2: 29.437 seconds
Compareable run 3: 29.688 seconds

It looks like the truncate option is faster overall. However. The delete optionhas a slightly higher insert speed. :) For large datasets (gb's) this could be a big difference since the delete statement overhead will be "earned" back with the actual moving of data. But to test this.... no more time / urge for now. ;)
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-07-10 : 07:37:44
"For large datasets (gb's) this could be a big difference ..."

Although the logging "cost" will be pretty high too ...

Thanks for that, most interesting.

Kristen
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-07-10 : 08:45:47
I wouldn't have thought that the delete/insert would be faster even for large datasets but would win if the delete/truncate could be run at a time which wasn't important e.g. if you have a limitted time to populate the table but the delete can be done prior to the window.

Would probably have to delete in batches which would make it even slower.
If you were replacing a complete database (or filegroup) then maybe restore from a backup with the tables empty but allocated.
This way lies madness I feel.

I'm doing a truncate / ssis bulk insert on tables around 10 - 20 G so might get round to testing it sometime.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

wernerdejong
Starting Member

7 Posts

Posted - 2006-07-10 : 09:12:33
If you set the database recovery model to simple, the delete statement wouldn't log as much as when the database is in full or bulk logged mode. :)

Anyway, testing would make certain.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-07-11 : 07:19:39
"wouldn't log as much ..."

... provided that you delete in batches :-(

Kristen
Go to Top of Page
   

- Advertisement -