简体   繁体   English

SQL Server脚本,用于从数据库中删除文件

[英]SQL Server Script for deleting files from DB

I need to delete all rows of a table containing a bunch of files. 我需要删除包含一堆文件的表的所有行。 Doing a simple DELETE FROM will more or less lock up the computer because of the sheer number of rows and size of files. 由于行数众多和文件大小很大,因此执行简单的DELETE FROM或多或少会锁定计算机。 I'm looking to create a SQL script that will accomplish this task without locking up my computer, can anyone point me in the right direction for this? 我正在寻找一个可以完成此任务而又不会锁定计算机的SQL脚本,有人可以为此指出正确的方向吗?

Thank you in advance. 先感谢您。

Did you try TRUNCATE - this will delete everything. 您是否尝试过TRUNCATE这将删除所有内容。

TRUNCATE TABLE yourTable

from MSDN MSDN

TRUNCATE TABLE is similar to the DELETE statement with no WHERE clause; TRUNCATE TABLE与没有WHERE子句的DELETE语句相似; however, TRUNCATE TABLE is faster and uses fewer system and transaction log resources. 但是,TRUNCATE TABLE更快,并且使用更少的系统和事务日志资源。

Compared to the DELETE statement, TRUNCATE TABLE has the following advantages: 与DELETE语句相比,TRUNCATE TABLE具有以下优点:

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. DELETE语句一次删除一行,并在事务日志中为每个删除的行记录一个条目。 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. TRUNCATE TABLE通过取消分配用于存储表数据的数据页面来删除数据,并仅在事务日志中记录页面的解除分配。

Fewer locks are typically used. 通常使用较少的锁。

When the DELETE statement is executed using a row lock, each row in the table is locked for deletion. 使用行锁执行DELETE语句时,表中的每一行都被锁定以删除。 TRUNCATE TABLE always locks the table and page but not each row. TRUNCATE TABLE始终锁定表和页面,但不锁定每一行。

Without exception, zero pages are left in the table. 毫无例外,表中保留了零页。

After a DELETE statement is executed, the table can still contain empty pages. 执行DELETE语句后,该表仍可以包含空页。 For example, empty pages in a heap cannot be deallocated without at least an exclusive (LCK_M_X) table lock. 例如,如果没有至少排他(LCK_M_X)表锁,则无法释放堆中的空页。 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. 对于索引,删除操作可以留下空白页,尽管这些页将通过后台清理过程快速释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM