Solved

Truncate 300GB table size

Posted on 2011-02-26
12
586 Views
Last Modified: 2012-05-11
How should I plan to truncate our table with 300GB of data, this table unfortunately have heavy insert  24X7X365 days, what will happen during the truncate with insert statement ?
0
Comment
Question by:motioneye
  • 3
  • 2
  • 2
  • +5
12 Comments
 
LVL 11

Accepted Solution

by:
JoeNuvo earned 63 total points
ID: 34990400
base on documents,
said truncate large table still fast.
since the Physical "delete" will perform in background

http://msdn.microsoft.com/en-us/library/ms177570(v=SQL.90).aspx
http://msdn.microsoft.com/en-us/library/ms177495(v=SQL.90).aspx
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34990410
The query
"TRUNCATE TABLE YourTableName"
would delete all records
+
resets IDENTITY column

Raj
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 34990583
i would like to add one point that TRUNCATE is not allowed for a table referenced by a Foreign Key constraint.
0
 
LVL 15

Assisted Solution

by:Aaron Shilo
Aaron Shilo earned 63 total points
ID: 34990984
hi

truncate command is a fast executing command becouse you only udate the the system tables that the table you truncated is empty and all extent information regarding that table is delete from system tables. (this is why the command is fast no actual action is performd on the table it self).

you will bearly feel a small lag in inserts till the system tables are udates .

0
 
LVL 18

Assisted Solution

by:sventhan
sventhan earned 125 total points
ID: 34991277
<< what will happen during the truncate with insert statement

If you running truncate on a LIVE DB, isn't that you going to loose few data? Is that OK to loose the DATA? I'm talking about the insert happening while you running the truncate.
0
 
LVL 18

Assisted Solution

by:sventhan
sventhan earned 125 total points
ID: 34991283
OK. I see the lock going on and you're safe I guess.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 125 total points
ID: 34991312
To summarize:
* you need to make sure you have no foreign key references on the table
* the truncate itself is executed very fast, among others because no logging takes place
So you just need to try. You can do that anytime, but of course you are loosing all data stored in that table.

If you have foreign keys, you only can perform a very costly delete operation, splitted in slices of e.g. thousands of records; or rename the table, and immediately create a new empty copy of it to be filled again.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 62 total points
ID: 34994399
>>executed very fast, among others because no logging takes place<<
If you are referring to the Transaction Log, you may want to double check that.
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 125 total points
ID: 34995124
"No logging" = "almost no logging", then. The removed pages are not logged, as they would with DELETE.
0
 
LVL 3

Assisted Solution

by:KetGuru
KetGuru earned 62 total points
ID: 34996530

Truncate table will not lock a table and you are mentioning that this table is in use all the time, you want to truncate it. so assumption that this table is log table and have no constraint/dependency with other tables. Should not be an issue. size does not make difference with truncate command.
0
 
LVL 18

Expert Comment

by:sventhan
ID: 34999019
< Truncate table will not lock a table

http://msdn.microsoft.com/en-us/library/ms177570.aspx

From the link

ewer locks are typically used.

When the DELETE statement is executed using a row lock, each row in the table is locked for deletion. TRUNCATE TABLE always locks the table and page but not each row.

@ KetGuru

>>> Is MSDN wrong?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35038996
>>"No logging" = "almost no logging", then. The removed pages are not logged, as they would with DELETE.
...
Truncate table will not lock a table <<

How about we state all the facts correctly as stated in SQL Server BOL:

Less transaction log space is used.
The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table data and records only the page deallocations in the transaction log.

Fewer locks are typically used.
When the DELETE statement is executed using a row lock, each row in the table is locked for deletion. TRUNCATE TABLE always locks the table and page but not each row.

Without exception, zero pages are left in the table.
After a DELETE statement is executed, the table can still contain empty pages. For example, empty pages in a heap cannot be deallocated without at least an exclusive (LCK_M_X) table lock. If the delete operation does not use a table lock, the table (heap) will contain many empty pages. For indexes, the delete operation can leave empty pages behind, although these pages will be deallocated quickly by a background cleanup process.

...

Microsoft SQL Server 2005 introduces the ability to drop or truncate tables that have more than 128 extents without holding simultaneous locks on all the extents required for the drop.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now