我正在尝试用c更新MySQL InnoDB表。 1亿行。查询花了将近一个小时,这不是问题。
但是,我想将此更新拆分为较小的块,以免阻止表访问。此更新不必是孤立的事务。
同时,在额外开销方面,更新的拆分不应太昂贵。
我考虑在使用:
的过程中循环遍历表格UPDATE TABLENAME SET NEWVAR=<expression> LIMIT batchsize, offset,
但是UPDATE在MySQL中没有偏移选项。
我知道我可以尝试更新键上选择的数据范围以及LIMIT选项,但这对于这个简单的任务来说似乎相当复杂。
答案 0 :(得分:4)
我最终得到了下面列出的程序。它有效,但我不确定所有查询是否有效识别连续范围。可以使用以下参数调用它(示例):
call chunkUpdate('SET var=0','someTable','theKey',500000);
基本上,第一个参数是更新命令(例如“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//