您的位置:首页 > 数据库

快速删除数据库中表内容很多的记录

2008-12-18 09:55 225 查看
use [brunei-ecustomsdev]gotruncate Table t_ic_audit_log_datagotruncate Table t_ic_audit_log_actionsgotruncate Table t_ic_audit_loggo

The TRUNCATE TABLE statement is a fast, nonlogged method of deleting all rows
in a table. TRUNCATE TABLE is functionally the same to the DELETE statement
without a WHERE clause. However, TRUNCATE TABLE is faster and uses fewer system
and transaction log resources.

Compared to the DELETE statement, TRUNCATE TABLE has the following
advantages:

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.

As with DELETE, the definition of a table emptied using TRUNCATE TABLE
remains in the database, together with its indexes and other associated objects.



Truncating Large Tables

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. For more information, see Dropping and Rebuilding Large
Objects.



Examples

The following example removes all data from the
JobCandidate
table.
SELECT
statements are included
before and after the
TRUNCATE TABLE
statement to compare
results.


Copy
Code
USE AdventureWorks;
GO
SELECT COUNT(*) AS BeforeTruncateCount
FROM HumanResources.JobCandidate;
GO
TRUNCATE TABLE HumanResources.JobCandidate;
GO
SELECT COUNT(*) AS AfterTruncateCount
FROM HumanResources.JobCandidate;
GO
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: