mysql慢日志使用mysqldumpslow进行分析
环境:centos7、mysql5.7、慢日志
1、mysqldumpslow参数解析
mysql> show variables like '%slow_query%'; #mysql日志路径 +---------------------+--------------------------------------+ | Variable_name | Value | +---------------------+--------------------------------------+ | slow_query_log | ON | | slow_query_log_file | /var/lib/mysql/1d0a52e2683d-slow.log | +---------------------+--------------------------------------+ 2 rows in set (0.01 sec) [root@minion243 tmp]# mysqldumpslow --help Usage: mysqldumpslow [ OPTS... ] [ LOGS... ] Parse and summarize the MySQL slow query log. Options are --verbose verbose --debug debug --help write this text to standard output -v verbose -d debug -s ORDER what to sort by (al, at, ar, c, l, r, t), 'at' is default al: average lock time ar: average rows sent at: average query time c: count l: lock time r: rows sent t: query time -r reverse the sort order (largest last instead of first) -t NUM just show the top n queries -a don't abstract all numbers to N and strings to 'S' -n NUM abstract numbers with at least n digits within names -g PATTERN grep: only consider stmts that include this string -h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard), default is '*', i.e. match all -i NAME name of server instance (if using mysql.server startup script) -l don't subtract lock time from total time
2、访问次数最多的20个sql语句
[root@minion243 tmp]# mysqldumpslow -s c -t 20 1d0a52e2683d-slow.log Reading mysql slow query log from 1d0a52e2683d-slow.log Count: 5021 Time=13.90s (69785s) Lock=0.00s (1s) Rows=0.9 (4378), rkwtest[rkwtest]@3hosts SELECT authentication_id,token_id,user_name,client_id,refresh_token,token,authentication FROM oauth_access_token WHERE ( token like 'S' ) Count: 1487 Time=15.85s (23563s) Lock=0.00s (0s) Rows=0.9 (1379), 2users@4hosts select client_id, client_secret, resource_ids, scope, authorized_grant_types, web_server_redirect_uri, authorities, access_token_validity, refresh_token_validity, additional_information, autoapprove from oauth_client_details where client_id = 'S'
3、返回记录集最多的20个sql
[root@minion243 tmp]# mysqldumpslow -s r -t 20 1d0a52e2683d-slow.log Reading mysql slow query log from 1d0a52e2683d-slow.log Count: 1 Time=10.61s (10s) Lock=0.00s (0s) Rows=21180.0 (21180), rkwtest[rkwtest]@[100.98.100.246] SELECT u_id,alias,identity_card,phone,xh,mz,xx,csrq,sg,whcd,hyzk,university_id,university_name,nj_id,nj_name,xy_id,xy_name,zy_id,zy_name,bj_id,bj_name,create_date,create_user_id,update_date,update_user_id,university_dsdm,reserve1,reserve2,reserve3,reserve4,reserve5,profiles,status FROM biz_student_university WHERE university_id = 'S' AND status = 'S' Count: 976 Time=15.24s (14876s) Lock=0.00s (0s) Rows=12.0 (11730), 2users@2hosts SELECT a.u_id AS uId, b.guide_id AS guideId, a.dept_id AS deptId, b.city_code AS cityCode, a.dept_level AS deptLevel
4、查看sql语句的执行计划
mysql> EXPLAIN SELECT * FROM oauth_access_token WHERE token like '%282f39a1-b727-4c8a-be08-3ea924139d78%'; +----+-------------+--------------------+------------+------+---------------+------+---------+------+--------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------------------+------------+------+---------------+------+---------+------+--------+----------+-------------+ | 1 | SIMPLE | oauth_access_token | NULL | ALL | NULL | NULL | NULL | NULL | 169312 | 11.11 | Using where | +----+-------------+--------------------+------------+------+---------------+------+---------+------+--------+----------+-------------+ 1 row in set, 1 warning (0.01 sec)
5、查看表的index
mysql> show index from oauth_access_token; +--------------------+------------+--------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +--------------------+------------+--------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | oauth_access_token | 0 | PRIMARY | 1 | authentication_id | A | 169208 | NULL | NULL | | BTREE | | | | oauth_access_token | 1 | idx_token_id | 1 | token_id | A | 169319 | NULL | NULL | YES | BTREE | | | +--------------------+------------+--------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 2 rows in set (0.00 sec)
做一个决定,并不难,难的是付诸行动,并且坚持到底。