MySQL线程池只在Percona,MariaDB,Oracle MySQL企业版中提供。Oracle MySQL社区版并不提供。html
在传统方式下,MySQL线程调度方式有两种:每一个链接一个线程(one-thread-per-connection)和全部链接一个线程(no-threads)。在实际生产中,通常用的是前者。即每当有一个客户端链接到MySQL服务器,MySQL服务器都会为该客户端建立一个单独的线程。链接数越多,则相应的线程会越多。mysql
若是大部分线程处于空闲状态,则不会对服务器的性能形成很大的影响。但若是同时执行的线程太多,会致使操做系统频繁的上下文切换。sql
引入线程池的目的,就是为了减小同时运行的线程的数量,下降上下文切换的次数。服务器
线程池是MariaDB最早实现的,有两种实现方式,分别对应Windows和Unix操做系统。其中,Windows系统上是直接利用操做系统的本地缓冲池功能,Unix系统上则是本身实现的。这致使两个操做系统上的系统变量有所不一样。ide
下面,基于Percona 5.6.31版本看看线程池的相关参数性能
thread_handlingui
默认是one-thread-per-connection,若是要使用链接池功能,则必须设置为pool-of-threads。this
thread_pool_sizespa
用于设置线程池中线程组的个数,默认为服务器CPU的核心数。实现分组的目的是为了把每一个分组对应到每一个CPU核心上,这样在同一时间点,每一个分组可调用1个线程进行执行。操作系统
thread_pool_max_threads
控制线程池的最大线程数,若该值为1000,表明线程池中所能建立的最大线程数不能超过1000。
This variable can be used to limit the maximum number of threads in the pool. Once this number is reached no new threads will be created.
thread_pool_oversubscribe
用于控制单个CPU核心在同一时间活跃的线程数。相似于一种“超频”的概念
The higher the value of this parameter the more threads can be run at the same time, if the values is lower than 3 it could lead to more sleeps and wake-ups
thread_pool_stall_limit
线程池中无可用线程时,thread_pool_stall_limit决定等待多久后建立新线程,单位为毫秒。默认是500。
在合适范围内,该值越大,MySQL服务器的总体处理性能就越好,由于较少数量的线程,会下降对于系统资源的征用。可是,并非越大越好,由于该值越大,新线程的建立将等待更长的时间,用户的查询延迟就会越明显。
The number of milliseconds before a running thread is considered stalled. When this limit is reached thread pool will wake up or create another thread. This is being used to prevent a long-running query from monopolizing the pool.
thread_pool_idle_timeout
设置空闲线程销毁前的等待时间,单位为秒,默认是60。
用户能够根据本身的业务场景来调整该参数的值,若是设置得过短,会致使线程频繁的销毁与建立,若是设置的太长,则会致使线程池中的线程数长时间不会降低。
This variable can be used to limit the time an idle thread should wait before exiting.
extra_port
用于设置MySQL服务端口以外的端口,供管理员管理服务器。
This variable can be used to specify additional port Percona Server will listen on. This can be used in case no new connections can be established due to all worker threads being busy or being locked when pool-of-threads feature is enabled.
extra_max_connections
用于设置extra_port端口容许的最大链接数,经过extra_port端口建立的链接,采用的是one-thread-per-connection的方式
This variable can be used to specify the maximum allowed number of connections plus one extra SUPER users connection on the extra_port. This can be used with the extra_port variable to access the server in case no new connections can be established due to all worker threads being busy or being locked when pool-of-threads feature is enabled。
除此以外,Percona还新增了两个参数用于实现优先级队列。
thread_pool_high_prio_mode
线程池分组内的待处理任务会放到任务队列中,等待worker线程处理。
每一个分组有两个队列:高优先级队列和普通队列,worker线程先从高优先队列取event处理,只有当高优先队列为空时才从普通队列取event处理。
经过优先级队列,可让已经开启的事务或短事务获得优先处理,及时提交释放锁等资源。
该参数可设置三种模式:
transactions:默认的,只有一个已经开启了事务的SQL,而且thread_pool_high_prio_tickets不为0,才会进入到高优先级队列中,每一个链接在thread_pool_high_prio_tickets次被放到优先队列中后,会移到普通队列中。
statements:单独的SQL老是进入高优先级队列
none:禁用高优先级队列功能,全部的链接都放到普通队列中处理。
thread_pool_high_prio_tickets
给每一个新的链接授予的tickets大小
This variable controls the high priority queue policy. Each new connection is assigned this many tickets to enter the high priority queue. Setting this variable to 0 will disable the high priority queue.默认为4294967295。
相关状态变量
能够根据下面两个状态变量,查看线程池的工做状态
Threadpool_threads
线程池中的线程个数
This status variable shows the number of threads in the pool.
Threadpool_idle_threads
线程池中处于空闲状态的线程数
This status variable shows the number of idle threads in the pool.
线程池的适用场景:
适用于有大量短查询的业务场景
在该场景下,每一个链接一个线程,过多的链接数很容易达到链接数的最大值,同时,过多的活跃线程会致使频繁的上下文切换。此时,可以使用线程池,由于是短查询,不会有某个链接长时间占用线程池中的线程,因此几乎不会影响客户端请求的响应时间,而且,随着链接数的增长,线程池中的线程数被控制都在必定范围内,减轻了系统的压力。
在有大量长查询的业务场景下不适合使用线程池
在该场景下,长查询可能会占据线程池的全部线程,致使线程池出现效率低效的状况,客户端设置不能进行链接。
参考:
1. 深刻理解MariaDB与MySQL
2. MariaDB原理与实现
3. http://www.tuicool.com/articles/7NveimQ
4. https://www.percona.com/blog/2013/03/16/simcity-outages-traffic-control-and-thread-pool-for-mysql/