mysql 优化之计数参数分析

参数:
1、thread相关计数

mysql> show global status like 'thread%';
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| Threadpool_idle_threads | 0     |
| Threadpool_threads      | 0     |
| Threads_cached          | 96    |
| Threads_connected       | 60    |
| Threads_created         | 1321  |
| Threads_running         | 3     |
+-------------------------+-------+

如果thread_created在不停地增加,则表明存在短连接访问数据库,且当连接建立时,mysql主线程会创建线程。
1、考虑增大thread_cache_size.
2、考虑是否将短连接改造成长连接的方式
注:

短连接是指程序和数据库通信时需要建立连接,执行操作后,连接关闭。短连接简单来说就是每一次操作数据库,都要打开和关闭数据库连接,基本步骤是:连接→数据传输→关闭连接。

长连接是指程序之间的连接在建立之后,就一直打开,被后续程序重用。使用长连接的初衷是减少连接的开销,尽管MySQL的连接比其他数据库要快得多。

对于thread相关计数,mysql是单进程多线程的数据库,每一个用户连接,是由一个对应的线程来服务的。当一个新的用户会话进来的时候,如果需要创建新的线程,则thread_created这个参数值就会增加1.

2、table cache相关计数

mysql> show global status like 'table_%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Table_locks_immediate      | 1861  |
| Table_locks_waited         | 0     |
| Table_open_cache_hits      | 44922 |
| Table_open_cache_misses    | 6641  |
| Table_open_cache_overflows | 4630  |
+----------------------------+-------+

table_open_cache 缓存的是sql执行过程中必须使用的一个对象(可以理解为 数据加工/访问过程中必须使用的一个工具),如果没有,则需要重构,将大大降低处理速度。如果table_open_cache_misses不停地增长,则需要考虑参数table_open_cache是否设置的足够大

mysql> show variables like 'table_%open%cache';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| table_open_cache | 2000  |
+------------------+-------+
1 row in set (0.00 sec)

3、handler 相关计数

mysql> show global status like '%handler_read%';
+-----------------------+--------+
| Variable_name         | Value  |
+-----------------------+--------+
| Handler_read_first    | 1780   |
| Handler_read_key      | 1769   |
| Handler_read_last     | 0      |
| Handler_read_next     | 2433   |
| Handler_read_prev     | 0      |
| Handler_read_rnd      | 0      |
| Handler_read_rnd_next | 222238 |
+-----------------------+--------+

以上handler相关指标:精确到行级别的粒度反应数据库的压力。指示是累计值,需要做差才能反应当前压力状态。

  • handler_read_key 与 handler_read_next分析:
    handler_read_key表示基于key值得查找次数。如select* from xcytest_ind where a in(1,2,3) ,如果sql执行走a列上的索引,则handler_read_key会增加3.因为在执行时,是通过制定key的方式,在索引上进行三次查找。如果a列是唯一索引,则handler_read_next不会增加。
    唯一索引与非唯一性索引的区别在于,唯一性索引上查找对于指定key值查找,命中就返回。在非唯一性索引上查找,找到后还需要查找下一条(行)记录是否满足,所以handler_read_next会增加,直到下一条记录不满足查询条件,才能完成本次查询。满足条件的记录越多,handler_read_next增加越多(非唯一性索引在“覆盖索引”的时候是直接返回值)。
    如果handler_read_next增加较多,则表明可能存在范围查询或者模糊查询,或者等值查询时表匹配的行较多。

  • handler_read_first 与handler_read_rnd_next分析:
    这两个指标跟全表扫描有关。
    handler_read_first:当进行行全表扫描时,查找第一条记录之前,改值会增加1。通过该值可以判断全表扫描的次数。
    handler_read_rnd_next:该值直接跟表的记录数对应,每读一行,该值增加1。该值的增长速度直接反应全表扫描给服务器带来的压力。

    4、buffer pool活跃度指标

    mysql> show global status like '%buffer%';
    +---------------------------------------+-------------+
    | Variable_name                         | Value       |
    +---------------------------------------+-------------+
    | Innodb_buffer_pool_dump_status        | not started |
    | Innodb_buffer_pool_load_status        | not started |
    | Innodb_buffer_pool_pages_data         | 7147        |
    | Innodb_buffer_pool_bytes_data         | 117096448   |
    | Innodb_buffer_pool_pages_dirty        | 0           |
    | Innodb_buffer_pool_bytes_dirty        | 0           |
    | Innodb_buffer_pool_pages_flushed      | 12300       |
    | Innodb_buffer_pool_pages_free         | 1024        |
    | Innodb_buffer_pool_pages_misc         | 20          |
    | Innodb_buffer_pool_pages_total        | 8191        |
    | Innodb_buffer_pool_read_ahead_rnd     | 0           |
    | Innodb_buffer_pool_read_ahead         | 0           |
    | Innodb_buffer_pool_read_ahead_evicted | 0           |
    | Innodb_buffer_pool_read_requests      | 502142      |
    | Innodb_buffer_pool_reads              | 2857        |
    | Innodb_buffer_pool_wait_free          | 0           |
    | Innodb_buffer_pool_write_requests     | 231944 |

    以上指标反应buffer的活跃度,包括对buffer中的数据页的读写、页面刷新,以及物理读等次数。
    仅仅通过buffer pool读写次数,无法判断性能是否良好。
    例如存在全表扫描,buffer pool读的次数很高,但性能反而降低。如果存在表锁等待等,进而buffer pool的读(写)频率可能会降低,buffer pool的活跃度降低。侧面推导:如果数据库性能低,同时buffer pool的活跃度低,则可以排除全表扫描,问题可能出现在其他部分。
    因此,buffer pool的活跃数据,仅代表buffer pool的活跃度,只能作为性能参考。

猜你喜欢

转载自blog.csdn.net/q936889811/article/details/80201372