MySQL性能分析命令

服務性能簡介

三個性能服務請求

  • 如何確認服務器是否達到了性能最佳狀態
  • 找出某條sql語句執行慢的原因
  • 診斷被用戶描述成“停頓”,“堆積”或者是“卡死”的間歇性疑難故障.

性能定義

  • 爲完成某件任務所需要的時間度量,換言之,性能即響應時間,這是一個非常重要的原則.

SQL性能分析

  • 執行分析 - 基於執行時間的分析
  • 等待分析 - 基於等待時間的分析
SQL性能優化

在一定的工作負載下儘可能地降低響應時間,在SQL服務器中定義的吞吐量指標爲單位時間內的查詢量.

性能優化工具

慢查詢日誌是一種輕量而且功能全面的性能剖析功能,優化服務器查詢的利器.

  • 通過--processlist查看記錄出現的時間和消失的時間
show full processlist
  • 通過TCP網絡抓包
tcpdump -i lo -l port 3306 -w lo.pcap | strings
tcpdump host mysql_ip and port mysql_port

開啓binlog慢日誌查詢

## 查看mysql所有的日誌信息的狀態以及對應存儲的位置
show variables like '%log%';

## 查看bin log是否開啓
show variables like 'log_bin';

## 查看mysql的版本,我的當前爲5,7
show variables like '%version%';

## 啓用bin log
## /etc/mysql/mysql.conf.d/mysqld.cnf
server_id         =  1   # 隨機數,集羣中要保證唯一
log_bin			= /var/log/mysql/mysql-bin.log
max_binlog_size  = 100M
binlog_do_db = include_database_name

## 重啓服務
service mysql start

## 再次查看bilog是否生效
show variables like 'log_bin';

查看binlog

/usr/bin/mysqlbinlog --base64-output=DECODE-ROWS -v /var/log/mysql/mysql-bin.000001

參考

參考: https://blog.51cto.com/lookingdream/1921162

單條SQL查詢性能分析

## 在mysql5之後可以通過profile來啓用分析
## 查看是否啓用
show variables like '%profil%';

## 設置啓用
set profiling=1;

## 查看啓用結果
show variables like '%profil%';

## 查看sql的執行性能
show profiles;

## 根據上述的結果獲取queryId
show profile from query queryId;

## 格式化輸出
set @query_id = 1;
select state, sum(duration) as total_r, round(100 * sum(duration) / (select sum(duration) from information_schema.profiling where query_id=@query_id ), 2) as pct_r, count(*) as calls, sum(duration) / count(*) as "R/Call" from information_schema.profiling where query_id = @query_id group by state order by total_r desc;

上述的執行輸出結果如下:
在這裏插入圖片描述

非性能分析工具的show status

mysql的show status命令返回一些計數器,既有服務器級別的計數器,也有基於某個連接會話級別的計數器.可以查看服務器級別的從服務器啓動開始計算的查詢次數統計

截取部分結果圖:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章