MySQL根据主键切割大事务(变相ROWID切片)
Posted robinson1988
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL根据主键切割大事务(变相ROWID切片)相关的知识,希望对你有一定的参考价值。
不管是Oracle,mysql,还是PostGre SQL,跑大事务都会严重影响数据库性能
在Oracle里面可以利用rowid切片的方式处理大事务,如果不清楚什么是rowid切片,可以买一本《SQL优化核心思想》看看
MySQL没有Oracle的区,块,段这些概念,所以也就不能使用rowid切片的方式处理大事务
MySQL中表一般都是存在innodb引擎中的,使用innodb引擎每个表都必须要设置主键(当然了你也可以不设置主键,你屌)
MySQL中主键一般都是自增(auto_increment)的,所以可以模仿Oracle的做法来变相实现ROWID切片
MySQL跑大事务危害非常大,DBA最担心的就是主从同步问题,MySQL里面跑个大事务很容易引起主从同步延迟
现在有个表t,主键为id,最小值和最大值如下:
mysql> select min(id) from t;
+---------+
| min(id) |
+---------+
| 2 |
+---------+
1 row in set (0.00 sec)
mysql> select max(id) from t;
+---------+
| max(id) |
+---------+
| 365926 |
+---------+
1 row in set (0.00 sec)
要执行update t set owner='沙雕';
可以根据主键对表切割,比如切为50份:
mysql> SELECT concat('update t set owner= ''沙雕'' where id>',
-> avg_row * (n - 1),
-> ' and id<=',
-> avg_row * n,
-> ';') split_sql
-> FROM (SELECT n, min_id, max_id, ceil(max_id / 50) avg_row
-> FROM (WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL
-> SELECT n + 1
-> FROM x
-> WHERE n < 50
-> )
-> SELECT *
-> FROM x) a, (SELECT min(id) min_id FROM t) b, (SELECT max(id) max_id
-> FROM t) c
-> ) a;
+--------------------------------------------------------------+
| split_sql |
+--------------------------------------------------------------+
| update t set owner= '沙雕' where id>0 and id<=7319; |
| update t set owner= '沙雕' where id>7319 and id<=14638; |
| update t set owner= '沙雕' where id>14638 and id<=21957; |
| update t set owner= '沙雕' where id>21957 and id<=29276; |
| update t set owner= '沙雕' where id>29276 and id<=36595; |
| update t set owner= '沙雕' where id>36595 and id<=43914; |
| update t set owner= '沙雕' where id>43914 and id<=51233; |
| update t set owner= '沙雕' where id>51233 and id<=58552; |
| update t set owner= '沙雕' where id>58552 and id<=65871; |
| update t set owner= '沙雕' where id>65871 and id<=73190; |
| update t set owner= '沙雕' where id>73190 and id<=80509; |
| update t set owner= '沙雕' where id>80509 and id<=87828; |
| update t set owner= '沙雕' where id>87828 and id<=95147; |
| update t set owner= '沙雕' where id>95147 and id<=102466; |
| update t set owner= '沙雕' where id>102466 and id<=109785; |
| update t set owner= '沙雕' where id>109785 and id<=117104; |
| update t set owner= '沙雕' where id>117104 and id<=124423; |
| update t set owner= '沙雕' where id>124423 and id<=131742; |
| update t set owner= '沙雕' where id>131742 and id<=139061; |
| update t set owner= '沙雕' where id>139061 and id<=146380; |
| update t set owner= '沙雕' where id>146380 and id<=153699; |
| update t set owner= '沙雕' where id>153699 and id<=161018; |
| update t set owner= '沙雕' where id>161018 and id<=168337; |
| update t set owner= '沙雕' where id>168337 and id<=175656; |
| update t set owner= '沙雕' where id>175656 and id<=182975; |
| update t set owner= '沙雕' where id>182975 and id<=190294; |
| update t set owner= '沙雕' where id>190294 and id<=197613; |
| update t set owner= '沙雕' where id>197613 and id<=204932; |
| update t set owner= '沙雕' where id>204932 and id<=212251; |
| update t set owner= '沙雕' where id>212251 and id<=219570; |
| update t set owner= '沙雕' where id>219570 and id<=226889; |
| update t set owner= '沙雕' where id>226889 and id<=234208; |
| update t set owner= '沙雕' where id>234208 and id<=241527; |
| update t set owner= '沙雕' where id>241527 and id<=248846; |
| update t set owner= '沙雕' where id>248846 and id<=256165; |
| update t set owner= '沙雕' where id>256165 and id<=263484; |
| update t set owner= '沙雕' where id>263484 and id<=270803; |
| update t set owner= '沙雕' where id>270803 and id<=278122; |
| update t set owner= '沙雕' where id>278122 and id<=285441; |
| update t set owner= '沙雕' where id>285441 and id<=292760; |
| update t set owner= '沙雕' where id>292760 and id<=300079; |
| update t set owner= '沙雕' where id>300079 and id<=307398; |
| update t set owner= '沙雕' where id>307398 and id<=314717; |
| update t set owner= '沙雕' where id>314717 and id<=322036; |
| update t set owner= '沙雕' where id>322036 and id<=329355; |
| update t set owner= '沙雕' where id>329355 and id<=336674; |
| update t set owner= '沙雕' where id>336674 and id<=343993; |
| update t set owner= '沙雕' where id>343993 and id<=351312; |
| update t set owner= '沙雕' where id>351312 and id<=358631; |
| update t set owner= '沙雕' where id>358631 and id<=365950; |
+--------------------------------------------------------------+
50 rows in set (0.00 sec)
以上是关于MySQL根据主键切割大事务(变相ROWID切片)的主要内容,如果未能解决你的问题,请参考以下文章