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

來源:互聯網
上載者:User

標籤:style   grep   close   連接埠   mysq   pre   body   sql   war   

有時候我們需要統計線上的SQL執行情況,比如想知道哪條SQL執行最頻繁,我們可以開啟general_log,然後進行統計,但是general_log開啟非常損耗效能,那麼我們可以使用vc-mysql-sniffer來代替,改工具是編譯好的二進位,下載即可使用。:

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

我這裡修改了網上的一個指令碼,分析vc-mysql-sniffer抓取到的結果統計頻繁執行的SQL。

#!/usr/bin/python#coding:utf8# python analysis-vc-log.py 3315 | sort | uniq -c | sort -nr |head -n 10import reimport sysimport osimport commandsvc_sniffer_time=5port=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:|# [email protected]‘ /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)        # 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 limit x,y to limit        lineTmp = re.sub(r"limit.*","limit",lineTmp)        print lineTmplogFo.close()
python analysis-vc-log.py 3310 | sort | uniq -c | sort -nr |head -n 10

抓取mysql 3310連接埠,統計執行最頻繁的10條SQL。結果我這裡就不貼上來了。有興趣的同學自己測試。

 

參考資料:

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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.