show profile
查看是否开启
show VARIABLES like ‘profiling’;
off时开启
set profiling=on;
使用
- sql select * from User;
- show profiles;
- show profile cpu,block io for query 121;
上图为cpu以及io开销
更多字段可以 show profile all for query 121; - 直接查询INFORMATION_SCHEMA表也可以查出数据
show status
使用
show session/global status
session表示当前会话(当前连接),global表示全局的(指数据库上次启动至今),默认是session
- 查看查询时间超过long_query_time秒的查询的个数。
show status like ‘slow_queries’; - 查看创建时间超过slow_launch_time秒的线程数。
show status like ‘slow_launch_threads’;
-查看不能立即获得的表的锁的次数。如果该值较高,并且有性能问题,你应首先优化查询,然后拆分表或使用复制。
show status like ‘table_locks_waited’; - 查看立即获得的表的锁的次数。
show status like ‘table_locks_immediate’; - 查看激活的(非睡眠状态)线程数。
show status like ‘threads_running’; - 查看创建用来处理连接的线程数。如果Threads_created较大,你可能要增加thread_cache_size值。
show status like ‘threads_created’; - 查看当前打开的连接的数量。
show status like ‘threads_connected’; - 查看线程缓存内的线程的数量。
show status like ‘threads_cached’; - 查看试图连接到MySQL(不管是否连接成功)的连接数
show status like ‘connections’; - com_* 查看数据库各种操作的数量【select/update/delete/insert/commit/rollback】
show [global] status like ‘com_select’; - 查看MySQL本次启动后的运行时间(单位:秒)
show status like ‘uptime’;
慢查询日志
-
查看是否开启
show variables like ‘slow_query%’; -
配置修改
/etc/mysql/conf.d/mysql.cnf
[mysqld]
slow_query_log = ON
slow_query_log_file = /var/lib/mysql/instance-1-slow.log
long_query_time = 0.3
explain
- 使用
explain select * from User
- 如果from子句中包含子查询,explain时会执行执行子查询的sql
- 只解释select语句
explain中的列
- id
标识符,标识select所属的行。 - select_type
显示对应行是简单还是复杂sql
simple 不包括子查询和union
primay 如果查询有复杂的子部分,最外层标记
subquery 包含在select列表中的子查询中的select(不在from子句中)
derived 包含在from子句的子查询中的select (派生表)
union 连表
union result 从union的匿名临时表检索结果的select - table
正在访问哪个表 - type
关联类型(访问类型),mysql如何查找表中的行
ALL 全表扫描
index 按索引次序进行全表扫描
range 范围扫描-有限制的索引扫描
ref 返回所有匹配某单个值的行(使用非唯一性索引/唯一性索引的非唯一性前缀是触发)
ref_or_null 二次查找出null条目
eq_ref 主键/唯一索引查找时,最多只返回一条数据
const,system 对查询的某部分就行优化并将其转换为一个常量时
NULL mysql优化后认为不用访问表或者索引 - possible_key
可能/可以会使用哪些索引 - key
实际使用的索引 - key_len
索引的字节数 - ref
key列记录的索引中查找值所用的列或者常量 - rows
找到所需行而要读取的行数 - filtered
表里复合某个条件的记录数的百分比的悲观估算 - extra
额外信息
using index - 覆盖索引
using where - 使用where
using temporary - mysql对结果排序时使用临时表
using filesort 对结果使用一个外部索引排序
range checked fro each record - 没有好用的索引
Performance Schema
待填