快速删除数据库中表内容很多的记录
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.
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.
The following example removes all data from the
before and after the
results.
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 truncatetables 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 theJobCandidatetable.
SELECTstatements are included
before and after the
TRUNCATE TABLEstatement 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 |
相关文章推荐
- 快速删除数据库中的所有表记录
- SQL SERVER快速删除数据库表里面的记录,truncate与delete的对比 【转】
- 快速删除数据库和备份记录
- linux命令---vi编辑器快速定位行数、删除当前行、和删除当前行后面的全部内容
- web 框架 Django 学习记录 (二)把数据库中表内容展示在页面
- 数据库日志太大如何快速删除
- 删除数据库中的重复数据记录、查找重复记录、查找不重复记录
- 多用户共享表时,删除数据库记录时最容易忽略的安全逻辑条件
- 删除数据库重复记录
- MySQL实现快速删除所有表而不删除数据库的方法
- 利用android sqlite3 查看创建的数据库记录内容
- 如何快速删除Access指定字段的重复记录
- Vim快速选中、删除、复制引号或括号中的内容
- MySQL 数据库中删除重复记录的方法总结
- 解决sqlite 删除记录后数据库文件大小不变
- 初学mysql(四)-数据库之数据库表内容的插入删除修改操作
- 用VisualC#来修改和删除数据库记录
- 快速删除数据库中所有表中的数据
- 删除数据库表中的重复冗余记录,以及随机取N条数据的方法
- JAVA-数据库之删除记录