提示:mysql索引相关的sql检查、分析、优化
show index from city;
mysql> show index from city;
+-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| city | 0 | PRIMARY | 1 | ID | A | 4149 | NULL | NULL | | BTREE | | | YES | NULL |
| city | 1 | CountryCode | 1 | CountryCode | A | 232 | NULL | NULL | | BTREE | | | YES | NULL |
| city | 1 | idx_city_name | 1 | Name | A | 3997 | NULL | NULL | | BTREE | | | YES | NULL |
+-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
可以查看出具体使用的索引类别、索引名称、使用索引的字段等详细信息,
查看sql执行的操作频率包括(insert select delete update)等信息
show global status like 'Com____';
mysql> show global status like 'Com_______';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_binlog | 0 |
| Com_commit | 0 |
| Com_delete | 1 |
| Com_import | 0 |
| Com_insert | 2 |
| Com_repair | 1 |
| Com_revoke | 0 |
| Com_select | 2949 |
| Com_signal | 0 |
| Com_update | 0 |
| Com_xa_end | 0 |
+---------------+-------+
11 rows in set (0.00 sec)
根据查询的信息可以明显体现出数据库相关操作频率。查询频率居高可以制定相关的查询优化方案进行优化
show status like ‘Com___’
mysql> show status like 'Com_______';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_binlog | 0 |
| Com_commit | 0 |
| Com_delete | 1 |
| Com_import | 0 |
| Com_insert | 0 |
| Com_repair | 0 |
| Com_revoke | 0 |
| Com_select | 7 |
| Com_signal | 0 |
| Com_update | 0 |
| Com_xa_end | 0 |
+---------------+-------+
11 rows in set (0.00 sec)
查看当前会话中数据库的操作频率
1、mysql配置文件中my.cnf中配置慢日志开启状态、慢日志记录时间等设置
slow-query-log = 1 # 开启慢日志
long_query_time = 1 # 慢日志记录时间
截取慢日志记录信息:
# User@Host: root[root] @ localhost [] Id: 63
# Query_time: 0.001890 Lock_time: 0.000101 Rows_sent: 4077 Rows_examined: 4077
SET timestamp=1695334403;
select * from city;
user:用户
host:主机
Query_time:查询时间
Rows_sent:发送行数
Rows_examined:执行行数
select * from city;执行的具体sql
2、查看数据库慢日志是否开启(ON/OFF)
show variables like ‘%query%’;
mysql> show variables like '%query%';
+------------------------------+-------------------------------------+
| Variable_name | Value |
+------------------------------+-------------------------------------+
| binlog_rows_query_log_events | OFF |
| ft_query_expansion_limit | 20 |
| have_query_cache | NO |
| long_query_time | 1.000000 |
| query_alloc_block_size | 8192 |
| query_prealloc_size | 8192 |
| slow_query_log | ON |
| slow_query_log_file | /data/mysql_log/mysql-slow-3306.log |
+------------------------------+-------------------------------------+
8 rows in set (0.00 sec)
slow_query_log :表示慢日志状态ON开启,OFF未开启
slow_query_log_file:表示慢日志存放的位置
1、查询是否支持 profile
select @@have_profiling;
mysql> select @@have_profiling;
+------------------+
| @@have_profiling |
+------------------+
| YES |
+------------------+
yes表示支持
2、查看profile状态
select @@profiling;
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set, 1 warning (0.00 sec)
set profiling=1;//开启profiling
set profiling=0;//关闭profiling
完成上述检查和设置之后就可以统计所有具体sql的耗费时间信息,
3、show profiles 查看执行sql耗费时间
mysql> show profiles;
+----------+------------+----------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+----------------------------------------+
| 1 | 0.00199825 | select * from city |
| 2 | 0.00019750 | select * from city where id=4078 |
| 3 | 0.00025000 | select * from city where id=4075 |
| 4 | 0.00023525 | select * from city where name='Nablus' |
| 5 | 0.00012450 | select @@have_profiling |
| 6 | 0.00006000 | set profiles |
| 7 | 0.00004925 | set profile |
| 8 | 0.00020050 | select @@profiling |
+----------+------------+----------------------------------------+
4、查看具体一条指定query_id的sql各阶段耗费时间
语法:
show profile for query queryId
mysql> show profile for query 4;
+----------------------------+----------+
| Status | Duration |
+----------------------------+----------+
| starting | 0.000080 |
| checking permissions | 0.000004 |
| Opening tables | 0.000025 |
| init | 0.000003 |
| System lock | 0.000006 |
| optimizing | 0.000007 |
| statistics | 0.000048 |
| preparing | 0.000012 |
| executing | 0.000024 |
| end | 0.000002 |
| query end | 0.000002 |
| waiting for handler commit | 0.000005 |
| closing tables | 0.000003 |
| freeing items | 0.000008 |
| cleaning up | 0.000005 |
+----------------------------+----------+
15 rows in set, 1 warning (0.00 sec)
详细的输出一条sql语句耗费时间的各阶段(总的耗费时间花费到哪里了)
5、查看指定query_id sql cup 耗费时间
mysql> show profile cpu for query 5;
+----------------------+----------+----------+------------+
| Status | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| starting | 0.000079 | 0.000038 | 0.000035 |
| checking permissions | 0.000003 | 0.000002 | 0.000002 |
| Opening tables | 0.000008 | 0.000004 | 0.000003 |
| init | 0.000004 | 0.000002 | 0.000002 |
| optimizing | 0.000005 | 0.000002 | 0.000003 |
| executing | 0.000007 | 0.000004 | 0.000003 |
| end | 0.000002 | 0.000001 | 0.000001 |
| query end | 0.000004 | 0.000002 | 0.000001 |
| closing tables | 0.000002 | 0.000001 | 0.000001 |
| freeing items | 0.000006 | 0.000003 | 0.000003 |
| cleaning up | 0.000006 | 0.000003 | 0.000003 |
+----------------------+----------+----------+------------+
11 rows in set, 1 warning (0.00 sec)
explain详细介绍
语法:
explain +sql语句
explain select 字段 from 表名 条件
Mysql—explain详解:explain详解
根据里面关键字段信息的描述来分析sql性能指标
Mysql调优工具------profiling使用: Mysql调优工具------profiling使用