MySQL性能分析脚本
#!/usr/bin/python
#!coding:utf-8 import mysql.connector as connector
import json """
目标 : 这个工具用于分析MySQL实例的性能问题
作者 : 蒋乐兴
QQ : 1721900707
版本信息 : 基于python3.4 MySQL 5.7.11
MySQL用户要用到的一些权限:
create user admin@'127.0.0.1' identified by '131417';
grant select on performance_schema.* to admin@'localhost';
""" show_golbal_value="select variable_name,variable_value from performance_schema.global_variables where variable_name= %s"
show_global_statu="select variable_name,variable_value from performance_schema.global_status where variable_name= %s" def analyse_innodb_cache(cursor,results):
'''
用于分析innodb_buffer_pool的缓存命中率
'''
#查询出innodb_buffer_pool_read_requests
cursor.execute(show_global_statu,('Innodb_buffer_pool_read_requests',))
key,value=cursor.fetchone()
innodb_buffer_pool_read_requests=value
#查询出Innodb_buffer_pool_reads
cursor.execute(show_global_statu,('Innodb_buffer_pool_reads',))
key,value=cursor.fetchone()
Innodb_buffer_pool_reads=value
#计算结果
innodb_buffer_pool_hit_rate=float(innodb_buffer_pool_read_requests)/(float(innodb_buffer_pool_read_requests)+float(Innodb_buffer_pool_reads))
#组织结果
tempResult={}
tempResult['innodb_buffer_pool_read_requests']=innodb_buffer_pool_read_requests
tempResult['Innodb_buffer_pool_reads']=Innodb_buffer_pool_reads
tempResult['innodb_buffer_pool_hit_rate']=innodb_buffer_pool_hit_rate results['innodb_buffer_pool_hit_rate']=tempResult def analyse_query_no_use_index(curosr,results):
"""
用于分析没有用索引的查询,最多只返回8条没有使用索引的SQL;执行次数越多的SQL在结果集中越靠前。
"""
query="select digest_text,sum_no_index_used from performance_schema.events_statements_summary_by_digest where sum_no_index_used>=1 and digest_text not like 'SHOW%' order by sum_no_index_used desc limit 8"
cursor.execute(query)
index=0
tempResult={}
for digest_text,sum_no_use_index in curosr:
tempResult[index]=digest_text
index=index+1
results['analyse_query_no_use_index']=tempResult def analyse_query_cache(cursor,results):
"""
本函数用于分析mysql实例的查询缓存、如果query_cache_type=0说明没有开启这个工能,那么分析结束。
不然要分析查询缓存的剩余内存,和命中率。把分析的结果包装到results变量中。
"""
analysis_var=("query_cache_type",)
cursor.execute(show_golbal_value,analysis_var)
key,value=cursor.fetchone()
#如果value的值等于OFF、说明本实例并没有开启查询缓存。
if value=='OFF':
results['query_cache']='query cache function not in use for this instance'
else:
#如果逻辑走到了这里说明、实例开启了查询缓存
#Qcache_free_memory 对应着剩余的查询缓存内存。
cursor.execute(show_global_statu,("Qcache_free_memory",))
key,value = cursor.fetchone()
#由于这个是延时计算的;所以查出来就要把它用掉。********************
Qcache_free_memory=value
#query_cache_size 对应着查询缓存的内存大小。
cursor.execute(show_golbal_value,("query_cache_size",))
key,value = cursor.fetchone();
query_cache_size=value
#用于查询缓存的内存空闲率
if float(query_cache_size) != 0:
query_cache_memory_free_rate=float(Qcache_free_memory)/float(query_cache_size)
else:
query_cache_memory_free_rate=None
#Qcache_hits 对应着命中的次数
cursor.execute(show_global_statu,("Qcache_hits",))
key,value=cursor.fetchone()
Qcache_hits=value
#Qcache_inserts 对应的没有命中的次数----由于没有命中所以要插入。
cursor.execute(show_global_statu,("Qcache_inserts",))
key,value=cursor.fetchone()
Qcache_inserts=value
#查询缓存的命中率为
if float(Qcache_hits+Qcache_inserts) != 0:
query_cache_hit_rate=float(Qcache_hits)/float(Qcache_hits+Qcache_inserts)
else:
query_cache_hit_rate=None
#组织结果
tempResult={}
tempResult['Qcache_free_memory']=Qcache_free_memory
tempResult['query_cache_size']=query_cache_size
tempResult['query_cache_memory_free_rate']=query_cache_memory_free_rate
tempResult['Qcache_hits']=Qcache_hits
tempResult['Qcache_inserts']=Qcache_inserts
tempResult['query_cache_hit_rate']=query_cache_hit_rate
results['query_cache']=tempResult analysis_function_sets={
'anaylsis_query_cache':analyse_query_cache,
'analyse_query_no_use_index':analyse_query_no_use_index,
'analyse_innodb_cache':analyse_innodb_cache
} if __name__=="__main__":
cnx=None
cursor=None
config={
'host':'127.0.0.1',
'port':3306,
'user':'admin',
'password':''
}
results={}
try:
cnx=connector.connect(**config)
cursor=cnx.cursor(buffered=True)
for key,function in analysis_function_sets.items():
print('start analyse {0}'.format(key))
function(cursor,results)
print(json.dumps(results))
except Exception as err:
print(err)
finally:
if cnx != None:
cnx.close()
cursor.close()
MySQL性能分析脚本的更多相关文章
- MySQL性能分析, mysql explain执行计划详解
MySQL性能分析 MySQL性能分析及explain用法的知识是本文我们主要要介绍的内容,接下来就让我们通过一些实际的例子来介绍这一过程,希望能够对您有所帮助. 1.使用explain语句去查看分析 ...
- MySQL性能分析及explain的使用
MySQL性能分析及explain用法的知识 1.使用explain语句去查看分析结果 如explain select * from test1 where id=1;会出现:id selectty ...
- MySQL性能分析及explain的使用说明
1.使用explain语句去查看分析结果 如explain select * from test1 where id=1;会出现:id selecttype table type possible_k ...
- mysql性能分析show profile/show profiles
MySQL性能分析show profiles show profile 和 show profiles 语句可以展示当前会话(退出session后,profiling重置为0) 中执行语句的资源使用情 ...
- MySQL性能分析和优化-part 1
MySQL性能优化 平时我们在使用MySQL的时候,怎么评估系统的运行状态,怎么快速定位系统瓶颈,又如何快速解决问题呢? 本文总结了多年来MySQL优化的经验,系统介绍MySQL优化的方法. OS性能 ...
- MySQL性能分析、及调优工具使用详解
本文汇总了MySQL DBA日常工作中用到的些工具,方便初学者,也便于自己查阅. 先介绍下基础设施(CPU.IO.网络等)检查的工具: vmstat.sar(sysstat工具包).mpstat.op ...
- mysql性能分析工具
一.EXPALIN 在SQL语句之前加上EXPLAIN关键字就可以获取这条SQL语句执行的计划 那么返回的这些字段是什么呢? 我们先关心一下比较重要的几个字段: 1. select_type 查询类型 ...
- MySQL性能分析(转)
第一步:检查系统的状态 通过操作系统的一些工具检查系统的状态,比如CPU.内存.交换.磁盘的利用率.IO.网络,根据经验或与系统正常时的状态相比对,有时系统表面上看起来看空闲,这也可能不是一个正常的状 ...
- mysql性能分析-------profiling和explain
1. profiling之性能分析 MySQL5.0.37版本以上支持了Profiling – 官方手册.此工具可用来查询 SQL 会执行多少时间,System lock和Table lock 花多少 ...
随机推荐
- Xamarin devexpress datagrid 样式
DevExpress的提供光与暗的内置,可以应用到主题GridControl改变其外观. 主题 iOS版 Android版 光(默认适用于iOS) 黑暗(默认为Android) 应用预定义的主题 ...
- 在SQL Server中使用命令调用SSIS包
在SQL Server中可以使用dtexec命令运行SSIS包(2005以上版本),当然也可以通过系统过程:xp_cmdshell调用dtexec运行SSIS包. 具体操作步骤如下: 1.首先,当然是 ...
- Linux内核源代码解析——TCP状态转移图以及其实现
本文原创为freas_1990,转载请标明出处http://blog.csdn.net/freas_1990/article/details/10223581 TCP状态转移的原理并不高深,但是处理逻 ...
- rsyslog 日志服务器接收日志权限问题
rsyslog 权限: 默认 [root@dr-mysql01 zjzc_log]# ls -ltr zj-frontend0*-access*27 -rw------- 1 root root 32 ...
- 【转】Linux 中断学习之小试牛刀篇
原文网址:http://www.linuxidc.com/Linux/2011-02/32129.htm 前言 在前面分析了中断的基本原理后,就可以写一个内核中断程序来体验以下,也可以借此程序继续深入 ...
- 30种IT技能让你年薪过10万美元!
科技行业的高薪岗位向来不少,但在不断变化的职场中,热门技能却随时在变:今天的热门技术,明天可能就会过时.美国求职网站 Dice.com 最近发布了 2015 年薪酬报告,通过对 23,470 位 IT ...
- [转]10款 Web 开发常备工具
文章地址:https://my.oschina.net/u/2903254/blog/798135 工欲善其事,必先利其器.如今 Web 开发标准越来越高,Web 开发者也在不断寻找途径提升自己的技能 ...
- 远程连接mysql
win系统下,连接别人的mysql或者让别人链接自己的mysql: 打开命令行cmd 进入mysql: mysql -u root -p mysql>use mysql; mysql>s ...
- jsp中导入导出excel,ssh框架
导入Excel:jsp中 <form action="user_importTradingMoney" enctype="multipart/form-data&q ...
- Odoo “坑” 系列之 XML中的布尔类型
在Odoo中试图通过XML方式更新某条Record的值,却意外发现根本不能更新,经查,对于XML中Boolean类型的字段,更新的方式应该采用eval的方式.