mysql sql诊断建议_MySQL诊断调优常用SQL语句
幫忙多點點文章末右下角的“好看”支持下,也可以將本文分享到朋友圈或你身邊的朋友,謝謝
在很多時候,我們需要通過SQL語句來查看MySQL執行SQL的情況,例如查看SQL執行隊列,是否存在慢查詢等等。
先看下基礎配置,監控mysql執行的sql語句需要先開啟相關日志
linux系統
可以在/etc/mysqld中添加如下:
指定日志路徑
log =/usr/local/mysql/mysql.log (這個路徑自定義即可)
就可以使用:
tail -f mysql.log
如果需要監控慢查詢可以添加如下內容:
添加慢查下記錄
log-slow-queries = /usr/local/mysql/slowquery.log(這個路徑自定義即可)
long_query_time = 1
windows系統
修改my.ini,在mysqld下添加log一行,
[mysqld]
log = "D:/tmp/mysql_log/mysql_log.sql" ?(這里路徑自定義即可)
然后,重新啟動mysql,就可以實時看到myql服務器當前正在執行的語句了。
常用的SQL,用于診斷排查問題
注:下列SQL在執行時,可能還需要你開啟MySQL相應的配置項,請根據提示進行配置
1. 查看當前應用連接,連接數突增排查
select user,SUBSTRING_INDEX(host,':',1) as ip , count(*) as count,db from information_schema.processlist where host not in ('localhost') and user not in ('replicater') group by ip order by count;
2. 當前有沒有鎖
select * from information_schema.innodb_locks;
3. 查看哪些sql執行最多
SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,SUM_ROWS_SENT,SUM_ROWS_EXAMINED,FIRST_SEEN,LAST_SEEN FROM performance_schema.events_statements_summary_by_digest where SCHEMA_NAME is not null and SCHEMA_NAME !='information_schema' ORDER BY COUNT_STAR desc LIMIT 1;
4. 哪個SQL掃描的行數最多(IO消耗)
SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,AVG_TIMER_WAIT,SUM_ROWS_SENT,SUM_ROWS_EXAMINED,FIRST_SEEN,LAST_SEEN FROM performance_schema.events_statements_summary_by_digest where SCHEMA_NAME is not null and SCHEMA_NAME !='information_schema' ORDER BY SUM_ROWS_EXAMINED desc LIMIT 1\G
5. 哪個SQL使用的臨時表最多
SELECT SCHEMA_NAME,DIGEST_TEXT,SUM_CREATED_TMP_DISK_TABLES,SUM_CREATED_TMP_TABLES,FIRST_SEEN,LAST_SEEN FROM performance_schema.events_statements_summary_by_digest where SCHEMA_NAME is not null and SCHEMA_NAME !='information_schema' ORDER BY SUM_CREATED_TMP_DISK_TABLES desc LIMIT 1\G
6.?哪個SQL排序數最多(CPU消耗)
SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,SUM_ROWS_SENT,SUM_SORT_ROWS,FIRST_SEEN,LAST_SEEN FROM performance_schema.events_statements_summary_by_digest where SCHEMA_NAME is not null and SCHEMA_NAME !='information_schema' ORDER BY SUM_SORT_ROWS desc LIMIT 1\G
7.?哪個索引使用最多
SELECT OBJECT_NAME, INDEX_NAME, COUNT_FETCH, COUNT_INSERT, COUNT_UPDATE, COUNT_DELETE FROM performance_schema.table_io_waits_summary_by_index_usage ORDER BY SUM_TIMER_WAIT DESC limit 1;
8.哪個索引沒有使用過
SELECT OBJECT_SCHEMA, OBJECT_NAME, INDEX_NAME FROM performance_schema.table_io_waits_summary_by_index_usage WHERE INDEX_NAME IS NOT NULL AND COUNT_STAR = 0 AND OBJECT_SCHEMA <> 'mysql' ORDER BY OBJECT_SCHEMA,OBJECT_NAME;
9.哪個表、文件邏輯IO最多(熱數據)
SELECT FILE_NAME,EVENT_NAME,COUNT_READ,SUM_NUMBER_OF_BYTES_READ,COUNT_WRITE,SUM_NUMBER_OF_BYTES_WRITE FROM performance_schema.file_summary_by_instance ORDER BY SUM_NUMBER_OF_BYTES_READ+SUM_NUMBER_OF_BYTES_WRITE DESC LIMIT 2\G
10.查看某條sql各階段執行時間,可開啟profiling功能
set global profiling=on;
其他的,大家可以去搜索下,更多的SQL性能分析、調優方面的常用語句
大家可以自己動手去試試上面的語句,看看是什么樣的輸出,如果發現語句有問題,可以加下面的微信,進去一起交流。
掃一掃加我微信,入群一起討論交流各種開源測試技術、工具、經驗和解決方案。
掃一掃,關注開源優測公眾號,原創文章首發平臺
順手點點點右下角的
總結
以上是生活随笔為你收集整理的mysql sql诊断建议_MySQL诊断调优常用SQL语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MariaDB(MySQL)_Maria
- 下一篇: springboot 按钮权限验证_sp