新特性解讀 | MySQL 8.0 資源組

原創做者: 楊濤濤 mysql

 

在MySQL 8.0 以前, 咱們假設一下有一條爛SQL,sql

mysql
select * from t1 order by rand() ;

以多個線程在跑,致使CPU被跑滿了,其餘的請求只能被阻塞進不來。那這種狀況怎麼辦? 運維

 

大概有如下幾種解決辦法:ui

  1. 設置max_execution_time 來阻止太長的讀SQL。那可能存在的問題是會把全部長SQL都給KILL 掉。有些必需要執行很長時間的也會被誤殺。.net

  2. 本身寫個腳本檢測這類語句,好比order by rand(), 超過必定時間用Kill query thread_id 給殺掉。線程

那能不能不要殺掉而讓他正常運行,可是又不影響其餘的請求呢?code

那mysql 8.0 引入的資源組(resource group,後面簡寫微RG)能夠基本上解決這類問題。orm

好比我能夠用 RG 來在SQL層面給他限制在特定的一個CPU核上,這樣我就無論他,讓他繼續運行,若是有新的此類語句,讓他排隊好了。ssl

爲何說基本呢?目前只能綁定CPU資源,其餘的暫時不行。資源

那我來演示下如何使用RG。

 

建立一個資源組user_ytt. 這裏解釋下各個參數的含義,

  1. type = user 表示這是一個用戶態線程,也就是前臺的請求線程。若是type=system,表示後臺線程,用來限制mysql本身的線程,好比Innodb purge thread,innodb read thread等等。

  2. vcpu 表明cpu的邏輯核數,這裏0-1表明前兩個核被綁定到這個RG。能夠用lscpu,top等列出本身的CPU相關信息。

  3. thread_priority 設置優先級。user 級優先級設置大於0。

mysql
mysql> create resource group user_ytt type = user vcpu = 0-1 thread_priority=19 enable;
Query OK, 0 rows affected (0.03 sec)

RG相關信息能夠從 information_schema.resource_groups 系統表裏檢索。

mysql
mysql> select * from information_schema.resource_groups;
+---------------------+---------------------+------------------------+----------+-----------------+
| RESOURCE_GROUP_NAME | RESOURCE_GROUP_TYPE | RESOURCE_GROUP_ENABLED | VCPU_IDS | THREAD_PRIORITY |
+---------------------+---------------------+------------------------+----------+-----------------+
| USR_default | USER | 1 | 0-3 | 0 |
| SYS_default | SYSTEM | 1 | 0-3 | 0 |
| user_ytt | USER | 1 | 0-1 | 19 |
+---------------------+---------------------+------------------------+----------+-----------------+
3 rows in set (0.00 sec)

咱們來給語句select guid from t1 group by left(guid,8) order by rand() 賦予RG user_ytt。

mysql> show processlist;
+-----+-----------------+-----------+------+---------+-------+------------------------+-----------------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+-----------------+-----------+------+---------+-------+------------------------+-----------------------------------------------------------+
| 4 | event_scheduler | localhost | NULL | Daemon | 10179 | Waiting on empty queue | NULL |
| 240 | root | localhost | ytt | Query | 101 | Creating sort index | select guid from t1 group by left(guid,8) order by rand() |
| 245 | root | localhost | ytt | Query | 0 | starting | show processlist |
+-----+-----------------+-----------+------+---------+-------+------------------------+-----------------------------------------------------------+
3 rows in set (0.00 sec)

找到鏈接240對應的thread_id。

mysql
mysql> select thread_id from performance_schema.threads where processlist_id = 240;
+-----------+
| thread_id |
+-----------+
| 278 |
+-----------+
1 row in set (0.00 sec)

給這個線程278賦予RG user_ytt。沒報錯就算成功了。

mysql
mysql> set resource group user_ytt for 278;
Query OK, 0 rows affected (0.00 sec)

固然這個是在運維層面來作的,咱們也能夠在開發層面結合 MYSQL HINT 來單獨給這個語句賦予RG。好比:

mysql
mysql> select /*+ resource_group(user_ytt) */guid from t1 group by left(guid,8) order by rand().
...
8388602 rows in set (4 min 46.09 sec)

RG的限制:

  1. Linux 平臺上須要開啓 CAPSYSNICE 特性。好比我機器上用systemd 給mysql 服務加上

    systemctl edit mysql@80 [Service] AmbientCapabilities=CAP_SYS_NICE

  2. mysql 線程池開啓後RG失效。

  3. freebsd,solaris 平臺thread_priority 失效。

  4. 目前只能綁定CPU,不能綁定其餘資源。

相關文章
相關標籤/搜索