首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >高效删除顶部?

高效删除顶部?
EN

Stack Overflow用户
提问于 2014-02-07 01:11:00
回答 3查看 211关注 0票数 0

从数据库中删除1000个或10000个数据块中的行是更高效、最终更快?我不得不从许多表中删除大约300万行。我首先在100K行的块中执行删除,但性能看起来并不好。我改成了10000,而且移除速度似乎更快了。想知道更小的,比如每条DELETE语句1K是不是更好。

有什么想法?

我像这样删除:

代码语言:javascript
复制
DELETE TOP(10000)
FROM TABLE
WHERE Date < '1/1/2012'
EN

回答 3

Stack Overflow用户

发布于 2014-02-07 01:28:12

是,不是,这取决于由于锁定而导致的表的使用。我会尝试以较慢的速度删除记录。所以与操作员的问题相反。

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2014-02-07 01:36:18

是的,是这样的。不过,这完全取决于您的服务器。我的意思是,上一次我这样做的时候,我使用这个批准以6400万的增量删除了一些东西(在一个表上,当时大约有140亿行,其中80%最终被删除了)。我每隔10秒左右就会被删除一次。

这真的取决于你的硬件。更多的粒度是更多的工作,但这意味着更少地等待tx日志,以用于表上的其他操作。你必须尝试并找到你觉得舒服的地方-没有最终的答案,因为它完全取决于桌子和硬件的使用情况。

票数 1
EN

Stack Overflow用户

发布于 2014-02-07 01:57:29

我们使用表分区在不到一秒的时间内删除了500万行,但这只是从一个表中删除的。这需要一些前期的工作,但最终是最好的方法。这对你来说可能不是最好的方式。

来自我们关于分区的文档:

假设您想要向表中添加500万行,但不希望在执行此操作时锁定该表。我在排序系统中遇到了这样的情况,我无法在不停止系统接受订单的情况下插入行。坏的!如果要添加不与当前数据重叠的行,则分区是一种方法。

注意事项:

  • 数据不能与当前数据重叠。您必须根据值对数据进行分区。新数据不能与当前分区的数据交织在一起。如果要删除数据,则必须删除整个分区。您将不会有WHERE子句。
  • 如果您在生产数据库上执行此操作,并且希望限制对表的锁定,请使用“ONLINE= on”创建索引。

步骤概述:

用于添加记录的

代码语言:javascript
复制
- 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.

用于删除记录的

代码语言:javascript
复制
- 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.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21609498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档