如何在SQL Server中快速停止插入语句流程

发布于 2025-02-14 01:02:18 字数 790 浏览 0 评论 0 原文

我有一个插入声明,该声明应该插入500K记录,但是2小时后,只插入了100K。

我决定取消该过程,现在可以看到插入的记录数量非常缓慢。

基于这将需要一天才能撤消那些插入的行。

有没有办法停止它并接受已插入或截断该表的所有内容并停止“取消查询”过程?

insert into xT2 (a_i, b_i, a_icd, b_icd, a_code, b_code, a_spcode, b_spcode, a_forcode, b_forcode, distance)
    select
        a.i a_i,
        b.i b_i,
        a.icd a_icd,
        b.icd b_icd,
        a.code a_code,
        b.code b_code,
        a.spcode a_spcode,
        b.spcode b_spcode,
        a.forcode a_forcode,
        b.forcode b_forcode, -99 Distance
    from 
        xT1 A
    inner join 
        xT1 B on a.i < b.i

I have an insert statement that should insert 500k records, but after 2 hours, only 100k have been inserted.

I decided to cancel the process and now I can see that number of records inserted is going down very slowly.

Based on that it will take a day to undo those inserted rows.

Is there a way to stop that and accept whatever has been inserted or truncate that table easily and stop the "Cancelling Query" process?

insert into xT2 (a_i, b_i, a_icd, b_icd, a_code, b_code, a_spcode, b_spcode, a_forcode, b_forcode, distance)
    select
        a.i a_i,
        b.i b_i,
        a.icd a_icd,
        b.icd b_icd,
        a.code a_code,
        b.code b_code,
        a.spcode a_spcode,
        b.spcode b_spcode,
        a.forcode a_forcode,
        b.forcode b_forcode, -99 Distance
    from 
        xT1 A
    inner join 
        xT1 B on a.i < b.i

enter image description here

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

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

发布评论

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

评论(1

又爬满兰若 2025-02-21 01:02:18

除了上面的肖恩·兰格(Sean Lange)的评论外,您不能接受部分插入物,该插件由于酸特性中的原子性而被插入。

原子性意味着您保证所有交易都成功或无能为力。您不会获得成功的一部分,也没有一部分。如果交易的一部分失败,则整个交易失败。具有原子性,它是“全部或全无”。我从简洁的。如果您还没有这样做,请阅读其他属性,因为它们非常基本了解DBMS的工作原理。

顺便说一句,500k行是相对较少的行,不应花费近2个小时的时间来插入,您的AI列中是否有零值,或者在查询正在运行时系统上的负载很重?只是为了添加另一个可能的项目,AI是否索引?

Further to Sean Lange's comment above, you cannot accept a partial insert which has been inserted due to the A for Atomicity in the ACID properties.

Atomicity means that you guarantee that either all of the transaction succeeds or none of it does. You don’t get part of it succeeding and part of it not. If one part of the transaction fails, the whole transaction fails. With atomicity, it's either "all or nothing". I took this description from https://database.guide/what-is-acid-in-databases/ as it describes is concisely. If you have not done so already, read up on the other properties as they are very fundamental in understanding how a DBMS works.

As an aside, 500K rows is a relatively small amount of rows and should not take anywhere near 2 hours to insert, are there any NULLS in your a.i column or is there a very heavy load on the system at the time the query is running? Just to add another possible item, is a.i indexed?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文