vc-mysql-sniffer統計MySQL的SQL分佈

有時候咱們須要統計線上的SQL執行狀況,好比想知道哪條SQL執行最頻繁,咱們能夠開啓general_log,而後進行統計,可是general_log開啓很是損耗性能,那麼咱們能夠使用vc-mysql-sniffer來代替,該工具是編譯好的二進制,下載便可使用。下載地址:python

https://www.vividcortex.com/resources/network-analyzer-for-mysqlmysql

我這裏修改了網上的一個腳本,分析vc-mysql-sniffer抓取到的結果統計頻繁執行的SQL。sql

#!/usr/bin/python
#coding:utf8
# python analysis-vc-log.py 3315 | sort | uniq -c | sort -nr |head -n 10
import re
import sys
import os
import commands
vc_sniffer_time=5
port=sys.argv[1]
vc_cmd=""" /usr/bin/timeout %s  /data/software/vc-mysql-sniffer -binding="[::]:%s"  > /tmp/tmp_vc_mysql_%s.txt """ % (vc_sniffer_time,port,port)
outtext = commands.getoutput(vc_cmd)
cmd=""" grep -Ev '# Time:|# User@Host' /tmp/tmp_vc_mysql_%s.txt |sed 's/# Query_time.*/myxxxxx/g' |awk BEGIN{RS=EOF}'{gsub(/\\n/," ");print}'|awk BEGIN{RS=EOF}'{gsub(/myxxxxx/,"\\n");print}' >/tmp/vc_mysql_%s.txt""" % (port,port)
outtext = commands.getoutput(cmd)
file="/tmp/vc_mysql_%s.txt" % (port)
logFo = open(file)
for line in logFo:
    line = re.sub(r"\n","",line)
    lineMatch = re.match(r".*",line)
    if lineMatch:
        lineTmp = lineMatch.group(0)
        lineTmp = lineTmp.lower()
        # remove extra space
        lineTmp = re.sub(r"\s+", " ",lineTmp)
        # replace values (value) to values (x)
        lineTmp = re.sub(r"values\s*\(.*?\)", "values (x)",lineTmp)
        # replace filed = 'value' to filed = 'x'
        lineTmp = re.sub(r"(=|>|<|>=|<=)\s*('|\").*?\2","\\1 'x'",lineTmp)
        # replace filed = value to filed = x
        lineTmp = re.sub(r"(=|>|<|>=|<=)\s*[0-9]+","\\1 x",lineTmp)
        # replace like 'value' to like 'x'
        lineTmp = re.sub(r"like\s+('|\").*?\1","like 'x'",lineTmp)
        # replace in (value) to in (x)
        lineTmp = re.sub(r"in\s+\(.*?\)","in (x)",lineTmp)
        # replace between '...' and '...' to between 'x' and 'x'
        lineTmp = re.sub(r"between\s+('|\").*?\1\s+and\s+\1.*?\1","between 'x' and 'x' ",lineTmp)
        # replace between ... and ... to between x and x
        lineTmp = re.sub(r"between\s+[0-9]+\s+and\s+[0-9]+","between x and x ",lineTmp)
        # replace limit x,y to limit
        lineTmp = re.sub(r"limit.*","limit",lineTmp)
        print lineTmp
logFo.close()
python analysis-vc-log.py 3310 | sort | uniq -c | sort -nr |head -n 10

抓取mysql 3310端口,統計執行最頻繁的10條SQL。結果我這裏就不貼上來了。有興趣的同窗本身測試。centos

 

參考資料:工具

https://www.centos.bz/2015/02/analysis-mysql-general-log/性能

相關文章
相關標籤/搜索