您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL重点关注性能及相关分析命令详解

2024/3/12 22:53:13发布11次查看
一、mysql性能监控关注点
qps(每秒query 量):这里的qps 实际上是指mysql server 每秒执行的query总量:
qps =  queries / seconds
tps(每秒事务量): 在mysql server 中并没有直接事务计数器,我们只能通过回滚和提交计数器来计算出系统的事务量。所以,我们需要通过以下方式来得到客户端应用程序所请求的tps 值:
tps = (com_commit + com_rollback) / seconds
key buffer 命中率:key buffer 命中率代表了myisam 类型表的索引的cache命中率。该命中率的大小将直接影响myisam 类型表的读写性能。key buffer 命
中率实际上包括读命中率和写命中率两种,mysql 中并没有直接给出这两个命中率的值,但是可以通过如下方式计算出来:
key_buffer_read_hits = (1 - key_reads / key_read_requests) * 100%
key_buffer_write_hits= (1 - key_writes / key_write_requests) * 100%
innodb buffer 命中率:这里innodb buffer 所指的是innodb_buffer_pool,也就是用来缓存innodb 类型表的数据和索引的内存空间。类似key buffer,我们
同样可以根据mysql server 提供的相应状态值计算出其命中率:
innodb_buffer_read_hits=(1-innodb_buffer_pool_reads/innodb_buffer_pool_read_requests) * 100%
query cache 命中率:如果我们使用了query cache,那么对query cache 命中率进行监控也是有必要的,因为他可能告诉我们是否在正确的使用query cache。
query cache 命中率的计算方式如下:
query_cache_hits= (qcache_hits / (qcache_hits + qcache_inserts)) * 100%
table cache 状态量:table cache 的当前状态量可以帮助我们判断系统参数table_open_cache 的设置是否合理。如果状态变量open_tables 与opened_tables 之间的比率过低,则代表table cache 设置过小:
show status like 'open%';
thread cache 命中率:thread cache 命中率能够直接反应出我们的系统参数thread_cache_size 设置的是否合理。一个合理的thread_cache_size 参数能够
节约大量创建新连接时所需要消耗的资源。
thread cache 命中率计算方式如下:
thread_cache_hits = (1 - threads_created / connections) * 100%
锁定状态:锁定状态包括表锁和行锁两种,我们可以通过系统状态变量获得锁定总次数,锁定造成其他线程等待的次数,以及锁定等待时间信息。
show  status  like '%lock%';
通过锁相关的系统变量,我们可以得出表锁总次数,其中造成其他现线程等待的次数。同时还可以得到非常详细的行锁信息,如行锁总次数,行锁总时间,每次行锁等待时间,行锁造成最大等待时间以及当前等待行锁的线程数。通过对这些量的监控,我们可以清晰的了解到系统整体的锁定是否严重。如当table_locks_waited 与table_locks_immediate 的比值较大,则说明我们的表锁造成的阻塞比较严重,可能需要调整query 语句,或者更改存储引擎,亦或者需要调整业务逻辑。当然,具体改善方式必须根据实际场景来判断。而innodb_row_lock_waits 较大,则说明innodb 的行锁也比较严重,且影响了其他线程的正常处理。同样需要查找出原因并解决。造成innodb 行锁严重的原因可能是query 语句所利用的索引不够合理(innodb 行锁是基于索引来锁定的),造成间隙锁过大。也可能是系统本身处理能力有限,则需要从其他方面(如硬件设备)来考虑解决。
复制延时量:复制延时量将直接影响了slave 数据库处于不一致状态的时间长短。
在slave 节点上执行“show slave status”命令,取seconds_behind_master 项的值来了解slave 当前的延时量(单位:秒)。
tmp table 状况:tmp table 的状况主要是用于监控mysql 使用临时表的量是否过多,是否有临时表过大而不得不从内存中换出到磁盘文件上。临时表使用状态信息可以通过如下方式获得:
show status like 'created_tmp%';
+-------------------------+-------+
| variable_name           | value |
+-------------------------+-------+
| created_tmp_disk_tables | 0     |
| created_tmp_tables      | 0     |
+-------------------------+-------+
如果created_tmp_tables 非常大,则可能是系统中排序操作过多,或者是表连接方式不是很优化。而如果是created_tmp_disk_tables created_tmp_tables 的比率过高,如超过10%,则我们需要考虑是否tmp_table_size 这个系统参数所设置的足够大。
binlog cache 使用状况:binlog cache 用于存放还未写入磁盘的binlog 信息。
相关状态变量如下:
show status like 'binlog_cache%';
binlog_cache_disk_use 值不为0,则说明binlog cache 大小可能不够,可以增加binlog_cache_size 系统参数大小。
innodb_log_waits 量:innodb_log_waits 状态变量直接反应出innodb log buffer 空间不足造成等待的次数。
show status like 'innodb_log_waits';
该变量值发生的频率将直接影响系统的写入性能,所以当该值达到每秒1 次时就该增加系统参数innodb_log_buffer_size 的值,毕竟这是一个系统共用的缓存,适当增加并不会造成内存不足的问题。
二、性能分析命令详解
show status;
flush status;
查看当前连接数 show status like 'thread_%';
thread_cached:被缓存的线程的个数
thread_running:处于激活状态的线程的个数
thread_connected:当前连接的线程的个数
thread_created:总共被创建的线程的个数
thread cache hits 
thread_connected = show global status like thread_created;
connections = show global status like 'connections';
tch=(1 - (threads_created / connections)) * 100
查看活动连接内容
show processlist;
如果 tch数小于90%,创建连接耗费了时间,增大thread_cached数量
qps (每秒查询处理量)myisam 引擎
questions = show global status like 'questions';
uptime = show global status like 'uptime';
qps=questions/uptime
tps(每秒传输的事物处理个数),即服务器每秒处理的事务数,如果是innodb会显示,没有innodb就不会显示。
com_commit = show global status like 'com_commit';
com_rollback = show global status like 'com_rollback';
uptime = show global status like 'uptime';
tps=(com_commit + com_rollback)/uptime
qps 和 tps值一定要实时监控,如果接近架构搭建时的测试峰值,愿上帝与你同在
read/writes ratio
qcache_hits = show global status like 'qcache_hits';
com_select = show global status like 'com_select';
com_insert = show global status like 'com_insert';
com_update = show global status like 'com_update';
com_delete = show global status like 'com_delete';
com_replace = show global status like 'com_replace';
r/w=(com_select + qcache_hits) / (com_insert + com_update + com_delete + com_replace) * 100
读写比,优化数据库的重要依据,读的多就去优化读,写的多就去优化写
slow queries per minute
slow_queries = show global status like 'slow_queries';
uptime = show global status like 'uptime';
sqpm=slow_queries / (uptime/60)
slow queries /questions ratio
slow_queries = show global status like 'slow_queries';
questions = show global status like 'questions';
s/q=slow_queries/questions
新版本上线时要着重关注慢查询
full_join per minute
select_full_join = show global status like 'select_full_join';
uptime = show global status like 'uptime';
fjpm=select_full_join / (uptime/60)
没有使用索引而造成的full_join,优化索引去吧
innodb buffer read hits
innodb_buffer_pool_reads = show global status like 'innodb_buffer_pool_reads';
innodb_buffer_pool_read_requests = show global status like 'innodb_buffer_pool_read_requests';
ifrh=(1 - innodb_buffer_pool_reads/innodb_buffer_pool_read_requests) * 100
innodb buffer命中率 目标 95%-99%;
table cache
open_tables= show global status like 'open_tables';
opened_tables= show global status like 'opened_tables';
table_cache= show global status like 'table_cache';
table_cache应该大于 open_tables 小于 opened_tables
temp tables to disk ratio
created_tmp_tables = show global status like 'created_tmp_tables';
created_tmp_disk_tables = show global status like 'created_tmp_disk_tables';
tdr=(created_tmp_disk_tables/created_tmp_tables)*100
show global status like 'innodb_row_lock_%';
innodb_row_lock_current_waits
the number of row locks currently being waited for. added in mysql 5.0.3.
innodb_row_lock_time
the total time spent in acquiring row locks, in milliseconds. added in mysql 5.0.3.
innodb_row_lock_time_avg
the average time to acquire a row lock, in milliseconds. added in mysql 5.0.3.
innodb_row_lock_time_max
the maximum time to acquire a row lock, in milliseconds. added in mysql 5.0.3.
innodb_row_lock_waits
the number of times a row lock had to be waited for. added in mysql 5.0.3.
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product