Thread pool引起的程序连接数据库响应慢
數據庫版本:percona-mysql 5.6.16
?在很長一段時間,都會出現程序連接數據庫,出現響應慢的情況,正常在幾到幾十毫秒之間,但是偶爾會出現上百毫秒的情況;
開始由于開發重新設置并調整過程序連接池,一直懷疑是連接池的問題,但是問題依舊;
因為使用的版本是percona-mysql 5.6.16 并且使用了數據庫連接池。
Thread Pool根據參數thread_pool_size被分為若干個group,每個group維護client 發起的 connections,當MySQL建立 connection 時,?
MySQL 根據connection的thread id 對thread_pool_size取模,將connection 發起的sql 語句分配到對應的group。每個group的最大worker
數量為thread_pool_oversubscribe+1。若worker達到最大數量后還是不足以處理回話請求,則連接在本group上等待,導致sql 語句的rt 增大;
因此將thread_pool_oversubscribe參數調大,但是問題依舊;
檢查thread_cache_size,服務器內存大小為64G,thread_cache_size=128;
每建立一個連接,都需要一個線程來與之匹配,此參數用來緩存空閑的線程,以至不被銷毀,如果線程緩存中有空閑線程,這時候如果建立新連接,MYSQL就會很快的響應連接請求;
show status like '%thread%';
Variable_name Value
Threads_cached ? ? 0
Threads_connected ? 219
Threads_created ? ? 655068
Threads_running ? ? 48
使用 show status查看當前mysql連接情況:
SHOW STATUS WHERE Variable_name LIKE '%Thread%';
Threads_cached :代表當前此時此刻線程緩存中有多少空閑線程。
Threads_connected :代表當前已建立連接的數量,因為一個連接就需要一個線程,所以也可以看成當前被使用的線程數。
Threads_created :代表從最近一次服務啟動,已創建線程的數量。
Threads_running :代表當前激活的(非睡眠狀態)線程數。并不是代表正在使用的線程數,有時候連接已建立,但是連接處于sleep狀態,這里相對應的線程也是sleep狀態。
最好將thread_cache_size設置成與threads_connected一樣。
show variables like '%thread%';
+-----------------------------------------+-----------------+
| Variable_name ? ? ? ? ? ? ? ? ? ? ? ? ? | Value ? ? ? ? ? |
+-----------------------------------------+-----------------+
| innodb_purge_threads ? ? ? ? ? ? ? ? ? ?| 1 ? ? ? ? ? ? ? |
| innodb_read_io_threads ? ? ? ? ? ? ? ? ?| 4 ? ? ? ? ? ? ? |
| innodb_thread_concurrency ? ? ? ? ? ? ? | 0 ? ? ? ? ? ? ? |
| innodb_thread_sleep_delay ? ? ? ? ? ? ? | 10000 ? ? ? ? ? |
| innodb_write_io_threads ? ? ? ? ? ? ? ? | 4 ? ? ? ? ? ? ? |
| max_delayed_threads ? ? ? ? ? ? ? ? ? ? | 20 ? ? ? ? ? ? ?|
| max_insert_delayed_threads ? ? ? ? ? ? ?| 20 ? ? ? ? ? ? ?|
| myisam_repair_threads ? ? ? ? ? ? ? ? ? | 1 ? ? ? ? ? ? ? |
| performance_schema_max_thread_classes ? | 50 ? ? ? ? ? ? ?|
| performance_schema_max_thread_instances | -1 ? ? ? ? ? ? ?|
| pseudo_thread_id ? ? ? ? ? ? ? ? ? ? ? ?| 8735851 ? ? ? ? |
| thread_cache_size ? ? ? ? ? ? ? ? ? ? ? | 128 ? ? ? ? ? ? |
| thread_concurrency ? ? ? ? ? ? ? ? ? ? ?| 24 ? ? ? ? ? ? ?|
| 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 ? ? ? ? ? ? ? ? | 100000 ? ? ? ? ?|
| thread_pool_oversubscribe ? ? ? ? ? ? ? | 40 ? ? ? ? ? ? ?|
| thread_pool_size ? ? ? ? ? ? ? ? ? ? ? ?| 12 ? ? ? ? ? ? ?|
| thread_pool_stall_limit ? ? ? ? ? ? ? ? | 500 ? ? ? ? ? ? |
| thread_stack ? ? ? ? ? ? ? ? ? ? ? ? ? ?| 262144 ? ? ? ? ?|
| thread_statistics ? ? ? ? ? ? ? ? ? ? ? | OFF ? ? ? ? ? ? |
+-----------------------------------------+-----------------+
可以不斷刷新,如果Threads_cached = 0 且 Threads_created 不斷增大,那么當前thread_cache_size的值設置要改大,改到 Threads_connected 值左右,
再結合物理內存 1G ?—> 8;2G ?—> 16; 3G ?—> 32; >3G ?—> 64 二個情況綜合考慮一下值,將thread_cache_size改為512;
注譯:
Threads_cached :代表當前此時此刻線程緩存中有多少空閑線程。
Threads_connected :代表當前已建立連接的數量,因為一個連接就需要一個線程,所以也可以看成當前被使用的線程數。
Threads_created :代表從最近一次服務啟動,已創建線程的數量。
Threads_running :代表當前激活的(非睡眠狀態)線程數。并不是代表正在使用的線程數,有時候連接已建立,但是連接處于sleep狀態,這里相對應的線程也是sleep狀態。
修改thread_cache_size為512后,重新測試程序連接數據庫響應時間,速度極快,不再出現程序連接數據庫響應慢的情況!
?
轉載于:https://www.cnblogs.com/hankyoon/p/8023458.html
總結
以上是生活随笔為你收集整理的Thread pool引起的程序连接数据库响应慢的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery插件之选项卡
- 下一篇: 10大主流压力测试工具推荐(转载)