MySQL線程池

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/

5. http://mysql.taobao.org/monthly/2016/02/09/

6. http://www.cnblogs.com/cchust/p/4510039.html

相關文章
相關標籤/搜索