MySQL之thread cache_MySQL
bitsCN.com
最近突然对MySQL的连接非常感兴趣,从status根据thread关键字可以查出如下是个状态
show global status like 'thread%';+-------------------+-------+| Variable_name | Value |+-------------------+-------+| Threads_cached | 57 || Threads_connected | 1268 || Threads_created | 31715 || Threads_running | 1 |+-------------------+-------+
Thread_cached:The number of threads in the thread cache
Thread_connected:The number of currently open connections.
Thread_created:The number of threads created to handle connections.
Thread_running:The number of threads that are not sleeping.
以上是这4个状态的含义,thread_connected等于show processlist,thread_running代表真正在运行的(等于1一般就是这个show status命令本身),thread_cached代表mysql管理的线程池中还有多少可以被复用的资源,thread_created代表新创建的thread(根据官方文档,如果thread_created增大迅速,需要适当调高thread_cache_size)。
我们先来实际看下这4个状态之间的直观关系。
从上面这个图,我们可以总结出来一个公式:running和其他三个状态关系不大,但肯定不会超过thread_connected
(new_con-old_con)=create+(old_cache-new_cache)
从上面公式可以看出,如果create等于0,那么thread_connected减少的和thread_cached增加的相等,thread_connected增加的和thread_cached减少的相等。(其实这也就是thread_cached存在的意义,资源可以复用)
我们来看眼影响thread_cached的参数thread_cache_size
How many threads the server should cache for reuse. When a client disconnects, the client's threads are put in the cache if there are fewer than thread_cache_size threads there. Requests for threads are satisfied by reusing threads taken from the cache if possible, and only when the cache is empty is a new thread created. This variable can be increased to improve performance if you have a lot of new connections. Normally, this does not provide a notable performance improvement if you have a good thread implementation. However, if your server sees hundreds of connections per second you should normally set thread_cache_size high enough so that most new connections use cached threads. By examining the difference between the Connections and Threads_created status variables, you can see how efficient the thread cache is. For details, see Section 5.1.6, “Server Status Variables”.
众所周知,mysql建立连接非常消耗资源,所以就有了thread_cache,当已有连接不再使用之后,mysql server不是直接断开连接,而是将已有连接转入到thread_cache中,以便下次在有create thread的需求时,可以在cache中复用,提高性能,降低资源消耗。
当然,如果已经有了中间件或者其他的连接池管理,那么这个参数就没有那么重要了,但是如果没有其他的连接池管理,那么优化这个参数还是可以得到不错的回报的。
bitsCN.com
熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

深入解析C語言中static關鍵字的功能和用法在C語言中,static是一種非常重要的關鍵字,它可以被用於函數、變數和資料類型的定義。使用static關鍵字可以改變物件的連結屬性、作用域和生命週期,以下就來詳細解析一下static關鍵字在C語言中的作用和用法。 static變數與函數:在函數內部使用static關鍵字定義的變數稱為靜態變量,它具有全域生命週

在java中可有兩種方式實作多線程,一種是繼承Thread類,一種是實作Runnable介面;Thread類別是在java.lang套件中定義的。一個類別只要繼承了Thread類別同時覆寫了本類別中的run()方法就可以實作多執行緒運算了,但是一個類別只能繼承一個父類,這是此方法的限制。以下看範例:packageorg.thread.demo;classMyThreadextendsThread{privateStringname;publicMyThread(Stringname){super();this

標題:C語言中go是關鍵字嗎?詳細解析在C語言中,"go"並不是一個關鍵字。 C語言的關鍵字是由C標準規定的,用來表示特定的語法結構或功能,在編譯器中有特殊的意義,不能被用來當作標識符或變數名稱。例如,關鍵字"int"表示整數資料型別,"if"表示條件語句等等。如果我們想要驗證在C語言中"go"是否是關鍵字,可以寫一個簡單的程式來測試。下面是一個範例:#inc

PHP中var關鍵字的作用和範例在PHP中,var關鍵字用來聲明一個變數。在先前的PHP版本中,使用var關鍵字是宣告成員變數的慣用方式,現在不再建議使用。然而,在某些情況下,var關鍵字依然會被使用。 var關鍵字主要用於宣告一個局部變量,並且會自動將該變數標記為局部作用域。這意味著該變數僅在當前的程式碼區塊中可見,並且不能在其他函數或程式碼區塊中存取。使用var

C語言的關鍵字共有32個,根據關鍵字的作用,可分其為資料類型關鍵字、控制語句關鍵字、儲存類型關鍵字和其它關鍵字四類。資料型別關鍵字有12個,包括char、double、float、int等;控制語句關鍵字有12個,包括for、break、if、else、do等;儲存類型關鍵字有4個,包括auto、static 、extern等;其它關鍵字有4個,包括const、sizeof等。

使用Java的Thread.start()函數啟動新執行緒在Java中,我們可以使用多執行緒來實作並發執行多個任務。 Java提供了Thread類別來建立和管理執行緒。 Thread類別中的start()函數用於啟動一個新線程,並執行該線程的run()方法中的程式碼。程式碼範例:publicclassMyThreadextendsThread{@Overr

在go語言中,while不是關鍵字,可以用for語句加break來實現while循環的效果,例「for {sum++ if sum>10{break}else{...}}」。 go語言有break、default 、func、select、case、defer、go、map、else、goto、for、if、var等25個關鍵字。

在java中,說到線程,Thread是必不可少的。執行緒是一個比過程更輕的調度執行器。為什麼要使用線程?透過使用線程,可以將作業系統過程中的資源分配和執行調度分開。每個執行緒不僅可以共享過程資源(記憶體位址、檔案I/O等),還可以獨立調度(執行緒是CPU調度的基本單位)。說明1、Thread是製作線程最重要的類,這個字本身也代表線程。 2.Thread類別實作了Runnable介面。實例publicclassThreadDemoextendsThread{publicvoidrun(){for(inti=0
