Percona 5.6 thread pool说明及使用

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

本文主要简单介绍一下thread pool,因为目前网上的文章都只是针对thread pool的原理说明,没有实际的使用示范。因此本会针对thread pool的使用做一些说明。


欢迎转载,请注明作者、出处。
作者:张正
blog:http://space.itpub.net/26355921 
QQ:176036317
如有疑问,欢迎联系。

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    |     -->目前thread pool中还剩余46个空闲thread
| Threadpool_threads      | 120   |     -->thread pool中允许同时running的最大线程数
| Threads_cached          | 0     |
| Threads_connected       | 1022  |     -->目前数据库一共有1022个连接
| Threads_created         | 476   |
| Threads_running         | 87    |     -->目前正在运行的线程数为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已经耗尽。
本文转自ITPUB博客84223932的博客,原文链接:Percona 5.6 thread pool说明及使用,如需转载请自行联系原博主。
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
5351
分享
相关文章
【MySQL异常】MySQL事务锁问题----lock wait timeout exceeded; try restarting transaction
【MySQL异常】MySQL事务锁问题----lock wait timeout exceeded; try restarting transaction
649 0
MySQL的Buffer Pool线程安全吗?
假设MySQL同时接收到了多个请求,他自然会用多线程处理,那这多线程就可能会同时访问BP,即同时操作里面的缓存页,同时操作一个free链表、flush链表、lru链表。现在多线程来并发的访问这个BP,此时他们都是在访问内存里的一些共享数据结构,如缓存页、各种链表,必要加锁,然后让一个线程先完成一系列操作,比如说加载数据页到缓存页,更新free、lru链表,然后释放锁,接着下个线程再执行操作。
115 0
【MySQL】thread_cache_size=16,是干什么的?底层原理是什么?
【MySQL】thread_cache_size=16,是干什么的?底层原理是什么?
330 0
【MySQL】innodb_lock_wait_timeout=120,是干什么的?底层原理是什么?
【MySQL】innodb_lock_wait_timeout=120,是干什么的?底层原理是什么?
478 0
【MySQL】thread_concurrency=8,是干什么的?底层原理是什么?
【MySQL】thread_concurrency=8,是干什么的?底层原理是什么?
478 0
easyswoole mysql-pool连接池empty null的问题
计算机是由许多零件组装而成,比如CPU、内存、硬盘等等。 当我们进行网络连接、请求的时候,就需要在不同组件中传递和返回各种信号、数据 比如在CPU、内存、网卡中,数据的传递,请求,获取。 如果在短时间内进行一万次mysql的连接,就需要在这个往返过程循环,在路上浪费了很多时间、性能消耗。 如果我们先把连接连接好,并且放在连接池中,程序中需要使用就从池中获取,执行操作。
183 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等