不管是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.13 sec)
mysql> select max(id) from t;
+---------+
| max(id) |
+---------+
| 88596 |
+---------+
1 row in set (0.15 sec)
可以根据主键对表切割,比如切为50份:
select concat('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;
mysql> select concat('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 |
+-------------------------------+
| where id>=0 and id<=1772 |
| where id>=1772 and id<=3544 |
| where id>=3544 and id<=5316 |
| where id>=5316 and id<=7088 |
| where id>=7088 and id<=8860 |
| where id>=8860 and id<=10632 |
| where id>=10632 and id<=12404 |
| where id>=12404 and id<=14176 |
| where id>=14176 and id<=15948 |
| where id>=15948 and id<=17720 |
| where id>=17720 and id<=19492 |
| where id>=19492 and id<=21264 |
| where id>=21264 and id<=23036 |
| where id>=23036 and id<=24808 |
| where id>=24808 and id<=26580 |
| where id>=26580 and id<=28352 |
| where id>=28352 and id<=30124 |
| where id>=30124 and id<=31896 |
| where id>=31896 and id<=33668 |
| where id>=33668 and id<=35440 |
| where id>=35440 and id<=37212 |
| where id>=37212 and id<=38984 |
| where id>=38984 and id<=40756 |
| where id>=40756 and id<=42528 |
| where id>=42528 and id<=44300 |
| where id>=44300 and id<=46072 |
| where id>=46072 and id<=47844 |
| where id>=47844 and id<=49616 |
| where id>=49616 and id<=51388 |
| where id>=51388 and id<=53160 |
| where id>=53160 and id<=54932 |
| where id>=54932 and id<=56704 |
| where id>=56704 and id<=58476 |
| where id>=58476 and id<=60248 |
| where id>=60248 and id<=62020 |
| where id>=62020 and id<=63792 |
| where id>=63792 and id<=65564 |
| where id>=65564 and id<=67336 |
| where id>=67336 and id<=69108 |
| where id>=69108 and id<=70880 |
| where id>=70880 and id<=72652 |
| where id>=72652 and id<=74424 |
| where id>=74424 and id<=76196 |
| where id>=76196 and id<=77968 |
| where id>=77968 and id<=79740 |
| where id>=79740 and id<=81512 |
| where id>=81512 and id<=83284 |
| where id>=83284 and id<=85056 |
| where id>=85056 and id<=86828 |
| where id>=86828 and id<=88600 |
+-------------------------------+
50 rows in set (0.25 sec)
想不想成为SQL大神?想不想精通SQL优化,精通所有关系型数据库优化?
加我微信692162374 报名单独辅导 , 顺带我还可以教你Python,Oracle,MySQL,以及大数据