【问题标题】:bulk update with store procedure using while loop使用 while 循环对存储过程进行批量更新
【发布时间】:2014-03-12 09:09:35
【问题描述】:

我想通过一些优化更新一个包含数百万条记录的表,但我无法创建一个过程。这是我的脚本

DROP PROCEDURE IF EXISTS test_f;
Delimiter //
create procedure test_f()
begin
DECLARE i INT unsigned DEFAULT 1;
while i < 10 do
update top 2000 test t
set 
t.total = 123

where t.total=0;
set i = i+1;
end while;      


END //

如何在我的程序中使用 top 或 limit。

【问题讨论】:

  • 您是否尝试过使用limit 2000update 语句?
  • 我试过了,但我得到了语法错误,我可以在哪里使用限制子句

标签: mysql sql stored-procedures mysql-workbench


【解决方案1】:

您可以将limit 与您的update 语句一起使用。

DROP PROCEDURE IF EXISTS test_f;

Delimiter //

create procedure test_f()
begin
    DECLARE i INT unsigned DEFAULT 1;
    while i < 10 do
        update test t
        set t.total = 123
        where t.total=0
        limit 2000;

        set i = i+1;
    end while;      
end;
//

delimiter ;

上述过程仅导致更新首次发现 2000 条记录。
如果要更新 2000 以上的记录,则需要在 limit 子句中指定,例如 limit 2000, 2000 更新行号从 2001 到 4000 的记录。

【讨论】:

  • 我想更新所有的表,但要分批更新 2000 个
猜你喜欢
  • 1970-01-01
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
  • 2010-12-19
相关资源
最近更新 更多