MySQL性能分析脚本

"""
目标       :       这个工具用于分析MySQL实例的性能问题
作者       :       蒋乐兴
QQ         :       1721900707
版本信息   :       基于python3.4 MySQL 5.7.11  orzdba
MySQL用户要用到的一些权限:
    create user [email protected]‘127.0.0.1‘ identified by ‘131417‘;
"""

#!/usr/bin/python
#!coding:utf-8
import mysql.connector as connector
import json
import argparse
import sys

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 anaylsis_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‘:anaylsis_query_cache}

if __name__=="__main__":
    cnx=None
    cursor=None
    config={
        ‘host‘:‘127.0.0.1‘,
        ‘port‘:3306,
        ‘user‘:‘admin‘,
        ‘password‘:‘131417‘
        }
    results={}
    try:
        cnx=connector.connect(**config)
        cursor=cnx.cursor(buffered=True)
        anaylsis_query_cache(cursor,results)
        for key,function in analysis_function_sets.items():
            function(cursor,results)
        print(results)
    except Exception as err:
        print(err)
    finally:
        if cnx != None:
            cnx.close()
            cursor.close()
时间: 2024-08-05 08:23:43

MySQL性能分析脚本的相关文章

MySQL性能分析及explain的使用

MySQL性能分析及explain用法的知识是本文我们主要要介绍的内容,接下来就让我们通过一些实际的例子来介绍这一过程,希望能够对您有所帮助. 1.使用explain语句去查看分析结果 如explain select * from test1 where id=1;会出现:id  selecttype  table  type possible_keys  key key_len  ref rows  extra各列. 其中, type=const表示通过索引一次就找到了: key=primar

MySQL性能分析和优化-part 1

MySQL性能优化 平时我们在使用MySQL的时候,怎么评估系统的运行状态,怎么快速定位系统瓶颈,又如何快速解决问题呢? 本文总结了多年来MySQL优化的经验,系统介绍MySQL优化的方法. OS性能分析 使用top观察top cpu/memory进程 使用mpstat观察每个CPU核心的CPU使用情况 使用iostat观察系统io状况 使用sar -n DEV观察网卡流量 使用vmstat查看系统内存使用情况 查看系统日志 使用dstat 记录和查看历史数据 查看昨天的数据 查看swap 查看

mysql性能分析show profile/show profiles

MySQL性能分析show profiles show profile 和 show profiles 语句可以展示当前会话(退出session后,profiling重置为0) 中执行语句的资源使用情况. Profiling 功能由MySQL会话变量 : profiling控制,默认是OFF.关闭状态.select @@profiling; 或者show variables like '%profi%': mysql> select @@profiling; +-------------+ |

MySQL性能分析之explain

mysql性能分析之explain Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看SQL语句的执行效 果,可以帮助选择更好的索引和优化查询语句,写出更好的优化语句. Explain语法:explain select … from … [where …] 例如:explain select * from news; 输出:+--+-----------+-----+----+-------------+---+----

Yahoo 开源的 MySQL 性能分析工具(MySQL Performance Analyzer)

原文地址:https://github.com/yahoo/mysql_perf_analyzer MySQL Performance Analyzer is an open source project for MySQL performance monitoring and analysis. This repository includes two sub projects: Java web application project myperf Java web server jetty

mysql性能分析-------profiling和explain

1. profiling之性能分析 MySQL5.0.37版本以上支持了Profiling – 官方手册.此工具可用来查询 SQL 会执行多少时间,System lock和Table lock 花多少时间等等,对定位一条语句的 I/O消耗和CPU消耗 非常重要.查看profiling: select @@profiling; 启动profiling: set @@profiling=1 关闭profiling : set @@profiling=0; sql语句: 1.查看profile记录sh

mysql性能分析工具

一.EXPALIN 在SQL语句之前加上EXPLAIN关键字就可以获取这条SQL语句执行的计划 那么返回的这些字段是什么呢? 我们先关心一下比较重要的几个字段: 1. select_type 查询类型 1)simple 简单查询,没有UNION和子查询 2)priamry 主查询,有UNION或子查询的最外层查询 3)union 联合查询,有UNION的第二个和以后的查询 4)subquery 子查询,有子查询的除FROM包含的子查询 5)derived 派生查询,FROM包含的子查询 6)de

tomcat启动和jvm性能分析脚本

#!/bin/bash # chkconfig: - 95 15 # description: Tomcat start/stop/status script #Location of JAVA_HOME (bin files) export JAVA_HOME= /usr/java/jdk1.7.0_80 #Add Java binary files to PATH export PATH=$JAVA_HOME/bin:$PATH #CATALINA_HOME is the location

mysql 性能分析套件

#!/usr/local/python3.5/bin/python3.5 #!coding:utf-8 #################################### #目地:用于诊断mysql性能问题 #作者:蒋乐兴 #时间:2016-07-02 #create user [email protected]'127.0.0.1' identified by '[email protected]'; # #################################### impo