从数据库中删除1000个或10000个数据块中的行是更高效、最终更快?我不得不从许多表中删除大约300万行。我首先在100K行的块中执行删除,但性能看起来并不好。我改成了10000,而且移除速度似乎更快了。想知道更小的,比如每条DELETE语句1K是不是更好。
有什么想法?
我像这样删除:
DELETE TOP(10000)
FROM TABLE
WHERE Date < '1/1/2012'发布于 2014-02-07 01:28:12
是,不是,这取决于由于锁定而导致的表的使用。我会尝试以较慢的速度删除记录。所以与操作员的问题相反。
set rowcount 10000
while @@rowcount > 0
begin
waitfor delay '0:0:1'
delete
from table
where date < convert(datetime, '20120101', 112)
end
set rowcount 0发布于 2014-02-07 01:36:18
是的,是这样的。不过,这完全取决于您的服务器。我的意思是,上一次我这样做的时候,我使用这个批准以6400万的增量删除了一些东西(在一个表上,当时大约有140亿行,其中80%最终被删除了)。我每隔10秒左右就会被删除一次。
这真的取决于你的硬件。更多的粒度是更多的工作,但这意味着更少地等待tx日志,以用于表上的其他操作。你必须尝试并找到你觉得舒服的地方-没有最终的答案,因为它完全取决于桌子和硬件的使用情况。
发布于 2014-02-07 01:57:29
我们使用表分区在不到一秒的时间内删除了500万行,但这只是从一个表中删除的。这需要一些前期的工作,但最终是最好的方法。这对你来说可能不是最好的方式。
来自我们关于分区的文档:
假设您想要向表中添加500万行,但不希望在执行此操作时锁定该表。我在排序系统中遇到了这样的情况,我无法在不停止系统接受订单的情况下插入行。坏的!如果要添加不与当前数据重叠的行,则分区是一种方法。
注意事项:
步骤概述:
用于添加记录的
- Partition the table you want to add records to (leave a blank partition for the new data). Do not forget to partition all of your indexes.
- Create new table with the exact same structure (keys, data types, etc.).
- Add a constraint to the new table to limit that data so that it would fit into the blank partition in the old table.
- Insert new rows into new table.
- Add indexes to match old table.
- Swap the new table with the blank partition of the old table.
- Un-partition the old table if you wish.
用于删除记录的
- Partition the table into sets so that the data you want to delete is all on partitions by itself (this could be many different partitions).
- Create a new table with the same partitions.
- Swap the partitions with the data you want to delete to the new table.
- Un-partition the old table if you wish.
https://stackoverflow.com/questions/21609498
复制相似问题