mysql5.6 thread pool_Percona 5.6 thread pool说明及使用

Thread pool是什么:

MySQL是每个连接上来都要创建一个线程来执行语句。这样每一个新的连接进来即会创建一个新的线程,这种动作对MySQL本身压力比较大。Threadpool是提供一种线程代理的模型执行每个连接的语句。而MySQL内部维护一个可能接受的线程总数,减少线程太多在CPU切换等方面的压力。

使用Threadpool的好处:

1.使用线程代理的模型在连接数已知可控的情况下,提前创建好过线程并利用操作系统的threadpool技术维护,能减少很多开销

2.尽可能的使用操作系统的线程方面的管理,把线程管理开销降到最低

3. 利用thread_pool_max_threads控制资源使用

使用方法:

目前支持threadpool的有MariaDB, Percona, 官方MySQL 5.5的一个收费plugin功能。percona与mariadb配置启用:

在my.cnf 中添加参数:

[mysqld]

#thread pool

thread_handling=pool-of-threads

thread_pool_max_threads=100       #设置thread pool线程的上限值(同时running的最大线程数为100)

thread_pool_size=50               #thread pool中线程数的下限值

mysql> show variables like 'thread%';

+-------------------------------+-----------------+

| Variable_name                 |      Value      |

+-------------------------------+-----------------+

| thread_cache_size             |         28      |

| thread_concurrency            |         10      |

| thread_handling               |pool-of-threads  |

| thread_pool_high_prio_mode    | transactions    |

| thread_pool_high_prio_tickets | 4294967295      |

| thread_pool_idle_timeout      |    60           |

| thread_pool_max_threads       |    120          |

| thread_pool_oversubscribe     |      3          |

| thread_pool_size              |     50          |

| thread_pool_stall_limit       |    500          |

| thread_stack                  | 262144          |

| thread_statistics             | OFF             |

+-------------------------------+-----------------+

Thread_pool_stall_limit:以10ms为单位,默认60ms范围值4-600,每个语句的时间片大小,一旦耗完则挂起将CPU交于其他线程

空闲时:

mysql> show global status like 'thread%';

+-------------------------+-------+

| Variable_name           | Value |

+-------------------------+-------+

| Threadpool_idle_threads | 51    |

| Threadpool_threads      | 52    |

| Threads_cached          | 0     |

| Threads_connected       | 22    |

| Threads_created         | 148   |

| Threads_running         | 1     |

+-------------------------+-------+

6 rows in set (0.00 sec)

系统中22个线程数(Threads_connected)为zabbix的连接,虽然目前总连接数为22,但是thread pool中的线程数(Threadpool_threads)为52。

该值的最小值决定于参数thread_pool_size 的值。

目前running线程数为1,空闲线程数为 51.

sysbench增加连接数:

使用sysbench创建1000个线程进行读写操作,同时还有22个zabbix的连接,共1022个连接:

sysbench --num-threads=1000 --test=oltp --mysql-user=root --mysql-password=root --mysql-table-engine=innodb  --init-rng=on --oltp-table-size=19390000 --max-time=$RT --max-requests=20000 --mysql-db=test --mysql-socket=/var/lib/mysql/mysql.sock run > sysbench.log

mysql> show global status like 'thread%';

+-------------------------+-------+

| Variable_name           | Value |

+-------------------------+-------+

| Threadpool_idle_threads | 46    |     --&gt目前thread pool中还剩余46个空闲thread

| Threadpool_threads      | 120   |--&gtthread pool中允许同时running的最大线程数

| Threads_cached          | 0     |

| Threads_connected| 1022  |     --&gt目前数据库一共有1022个连接

| Threads_created         | 476   |

| Threads_running| 87    |--&gt目前正在运行的线程数为87

+-------------------------+-------+

6 rows in set (0.37 sec)

数据库中 Threadpool_idle_threads(46) + Threads_running(87) 之和(133) 大致是与 Threadpool_threads(120) 的值相等的,

而 Threadpool_threads 的最大值是受限于 参数thread_pool_max_threads的值。

系统中总共连接数有1022,而running的线程数只有87,还有46空闲,是因为操作系统的CPU已经耗尽。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值