SQL Server 中聚集索引、大型表的 DELETE 性能

发布于 2024-11-16 15:02:53 字数 146 浏览 2 评论 0原文

我有一个包含超过 2000 万行的表,当我这样做时:

DELETE [Table] WHERE ID = ?

需要 40 多秒。 ID 列是聚集的。

这是你所期望的吗?或者是否可以对此进行优化?

I have a table with more than 20 million rows, and when i do:

DELETE [Table] WHERE ID = ?

It takes over 40 seconds. The ID column is clustered.

Is this what you could expect? or is it possible to optimize this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流年已逝 2024-11-23 15:02:53

除了他们的答案中包含的要点 JNK 之外,我看到的一个特殊杀手是当您从一个或多个外键约束的引用表中删除行以及引用表中的引用列时( s) 没有索引 - 在接受删除之前,您将强制对每个表进行表扫描。

In addition to the fine points JNK included in their answer, one particular killer I've seen is when you're deleting rows from the referenced table for one or more foreign key constraints, and the referencing column(s) in the referencing table(s) aren't indexed - you're forcing a table scan on each of those tables to occur before the delete can be accepted.

亚希 2024-11-23 15:02:53

这将取决于许多您没有告诉我们的因素...

删除了多少行?更多的行显然意味着更多的时间。

还有其他索引吗? 每个索引都需要更新,而不仅仅是集群索引。如果您要删除 10 个索引,则需要大约 10 倍的时间(非常粗略)。

还有其他活动吗?如果发生更新或插入,则很可能会出现等待和争用。

一般来说,操作所需的秒数很大程度上取决于您的硬件设置。如果您在台式机上运行此程序,而不是在具有高性能阵列和 12 核的服务器上运行,则期望会非常不同。

This is going to depend on a lot of factors that you don't tell us about...

How many rows are deleted? More rows obviously means more time.

Are there other indexes? Every index needs to get updated, not just the clustered. If you are deleting through 10 indexes it will take about 10x as long (very roughly).

Is there other activity? If there are updates or inserts happening there are very likely waits and contention.

Also very generally speaking, the number of seconds an operation takes is HIGHLY dependent on your hardware setup. If you ran this on a desktop machine vs. a server with a high-performance array and 12 cores the expectations will be very different.

疯了 2024-11-23 15:02:53

也可以尝试批量删除数据。例子

set rowcount 10000
delete [table] where id = ? 
while @@rowcount >0
begin
delete [table] where id = ? 
end

Also try deleting data in a batch. Example

set rowcount 10000
delete [table] where id = ? 
while @@rowcount >0
begin
delete [table] where id = ? 
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文