这个问题似乎没有什么好的解决方法,有个朋友提示,能否将1千万条记录拆分成多个部分,并发更新,这种比单纯的一条SQL全表更新要快一些。真的是这样吗?我做了一个实验。
1. 环境配置:
机器配置:16核,32G
MySQL 版本:5.7.19,1主2从
1张sysbench表,1千万条数据
2. 测试结果:
(1)单独一条SQL更新全表,耗时 3 min 41.78 sec。
update sbtest1 set k=k+1;
(2)将1千万条数据分为4个部分,并发更新,耗时 1 min 23.05 sec。
update sbtest1 set k=k+1 where id >=1 and id <=2500000;
update sbtest1 set k=k+1 where id >=2500001 and id <=5000000;
update sbtest1 set k=k+1 where id >=5000001 and id <=7500000;
update sbtest1 set k=k+1 where id >=7500001 and id <=10000000;
从以上测试结果来看,分块并发更新,确实比单个SQL更新效率要高。
3. 遇到的问题:
在测试的过程,会遇到binlog cache不够的错误,如下:
ERROR 1197 (HY000): Multi-statement transaction required more than ‘max_binlog_cache_size’ bytes of storage; increase this mysqld variable and try again
调高max_binlog_cache_size参数,重新连接,再次执行就好了。