最近突然对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中复用,提高性能,降低资源消耗。  

  当然,如果已经有了中间件或者其他的连接池管理,那么这个参数就没有那么重要了,但是如果没有其他的连接池管理,那么优化这个参数还是可以得到不错的回报的。

MySQL之thread cache的更多相关文章

  1. MySQL源码之Thread cache

    MySQL server为每一个connection建立一个thread为其服务,虽然thread create比着fork process代价高,单高并发的情况下,也不可忽略. 所以增加了Threa ...

  2. MySQL 之 query cache

    早上一打开网站,就看到了Percona官网发布的最新的关于 mysql query cache的文章: https://www.percona.com/blog/2015/08/07/mysql-qu ...

  3. MySQL学习笔记-cache 与 buffer

    Cache和Buffer是两个不同的概念,简单的说,Cache是加速"读",而 buffer是缓冲"写",前者解决读的问题,保存从磁盘上读出的数据,后者是解决写 ...

  4. MySQL 缓存 Query Cache

    QueryCache(下面简称QC)是根据SQL语句来cache的.一个SQL查询如果以select开头,那么MySQL服务器将尝试对其使 用QC.每个Cache都是以SQL文本作为key来存的.在应 ...

  5. mysql之Query Cache

    1,QueryCache的实现原理: 1.目前只有select语句会被cache,其他类似show,use的语句则不会被cache. 2.两个SQL语句,只要相差哪怕是一个字符(例如大小写不一样:多一 ...

  6. MySQL的Query Cache原理分析

    QueryCache(下面简称QC)是根据SQL语句来cache的.一个SQL查询如果以select开头,那么MySQL服务器将尝试对其使用QC.每个Cache都是以SQL文本作为key来存的. 原理 ...

  7. MySQL Replication Thread States

    1.主节点线程状态(Replication Master Thread States): Finished reading one binlog; switching to next binlog 线 ...

  8. mysql查询的cache

    Mysql SQL_NO_CACHE不生效的问题 贾春春 1 票 1224 我想通过SQL_NO_CACHE得知某个query查询速度,但似乎无法实现 例如首次查询: mysql> select ...

  9. MySQL监控模板说明-Percona MySQL Monitoring Template for Cacti

    http://blog.chinaunix.net/uid-16844903-id-3535535.html https://www.percona.com/doc/percona-monitorin ...

随机推荐

  1. flask插件系列之flask_restful设计API

    前言 flask框架默认的路由和视图函数映射规则是通过在视图函数上直接添加路由装饰器来实现的,这使得路由和视图函数的对应关系变得清晰,但对于统一的API开发就变得不怎么美妙了,尤其是当路由接口足够多的 ...

  2. mysqldump 逻辑备份的正确方法【转】

    1. 利用mysqldump进行逻辑备份 1)全逻辑备份: mysqldump -uxxx -p --flush-logs --delete-master-logs --all-databases & ...

  3. Linux是对用户的密码的复杂度要求设置【转】

    那么Linux是如何实现对用户的密码的复杂度的检查的呢?其实系统对密码的控制是有两部分组成: 1 cracklib 2 /etc/login.defs pam_cracklib.so 才是控制密码复杂 ...

  4. Codeforces Round #456 (Div. 2)

    Codeforces Round #456 (Div. 2) A. Tricky Alchemy 题目描述:要制作三种球:黄.绿.蓝,一个黄球需要两个黄色水晶,一个绿球需要一个黄色水晶和一个蓝色水晶, ...

  5. 设计模式之笔记--职责链模式(Chain of Responsibility)

    职责链模式(Chain of Responsibility) 定义 职责链模式(Chain of Responsibility),使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系 ...

  6. [How to] 使用HBase协处理器---Endpoint客户端代码的实现

    1.简介 不同于Observer协处理器,EndPoint由于需要同region进行rpc服务的通信,以及客户端出数据的归并,需要自行实现客户端代码. 基于[How to] 使用HBase协处理器-- ...

  7. nginx 各种配置

    first : mkdir /usr/local/nginx/conf/vhosts{网站配置}/usr/local/nginx/conf/vhosts/test.conf : server { li ...

  8. java中参数传递--值传递,引用传递

    java中的参数传递——值传递.引用传递   参数是按值而不是按引用传递的说明 Java 应用程序有且仅有的一种参数传递机制,即按值传递. 在 Java 应用程序中永远不会传递对象,而只传递对象引用. ...

  9. Mybatis的初步使用

    MyBatis 是当下最流行的持久层框架,也是ORM框架,本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google ...

  10. 垃圾回收算法与 JVM 垃圾回收器综述(转)

    垃圾回收算法与 JVM 垃圾回收器综述 我们常说的垃圾回收算法可以分为两部分:对象的查找算法与真正的回收方法.不同回收器的实现细节各有不同,但总的来说基本所有的回收器都会关注如下两个方面:找出所有的存 ...