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.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)


 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章