批量更新MySQL表

我正在尝试使用c更新MySQL InnoDB表. 1亿行.查询耗时将近一个小时,这不是问题.

但是,我想将此更新分成较小的块,以便不阻止表访问.此更新不必是孤立的事务.

同时,就额外的开销而言,拆分更新不应太昂贵.

我考虑过使用以下方法在表中循环:

UPDATE TABLENAME SET NEWVAR=<expression> LIMIT batchsize, offset,

但是UPDATE在MySQL中没有偏移选项.

我知道我可以尝试更新在键上选择的数据范围以及LIMIT选项,但这对于该简单任务而言似乎相当复杂.

解决方法:

我完成了下面列出的过程.它可以工作,但是我不确定所有查询来识别连续范围是否有效.可以使用以下参数(示例)来调用它:

call chunkUpdate('SET var=0','someTable','theKey',500000);

基本上,第一个参数是update命令(例如,诸如“ set x = …”之类的东西),其后是mysql表名,其后是必须唯一的数字(整数)键,其后是要处理的块.密钥应具有合理性能的索引.可以删除以下代码中的“ n”变量和“ select”语句,仅用于调试.

delimiter //
CREATE PROCEDURE chunkUpdate (IN cmd VARCHAR(255), IN tab VARCHAR(255), IN ky VARCHAR(255),IN sz INT)
BEGIN
  SET @sqlgetmin = CONCAT("SELECT MIN(",ky,")-1 INTO @minkey FROM ",tab); 
  SET @sqlgetmax = CONCAT("SELECT MAX(",ky,") INTO @maxkey FROM ( SELECT ",ky," FROM ",tab," WHERE ",ky,">@minkey ORDER BY ",ky," LIMIT ",sz,") AS TMP"); 
  SET @sqlstatement = CONCAT("UPDATE ",tab," ",cmd," WHERE ",ky,">@minkey AND ",ky,"<=@maxkey");
  SET @n=1;

  PREPARE getmin from @sqlgetmin;
  PREPARE getmax from @sqlgetmax;
  PREPARE statement from @sqlstatement;

  EXECUTE getmin;

  REPEAT
    EXECUTE getmax; 
    SELECT cmd,@n AS step, @minkey AS min, @maxkey AS max;
    EXECUTE statement;
    set @minkey=@maxkey;
    set @n=@n+1;
  UNTIL @maxkey IS NULL
  END REPEAT; 
  select CONCAT(cmd, " EXECUTED IN ",@n," STEPS") AS MESSAGE;
END//
上一篇:c# – 以下线程是否安全?


下一篇:旋转日志文件和文件锁定