【MYSQL索引性能分析以及使用规则】

性能分析

1.慢查询日志

  • 慢查询日志记录了所有执行时间超过指定参数(long_query_time,单位:秒,默认10秒)的所有sql语句的日志。
  • mysql的慢查询日志默认没有开启,需要在mysql的配置文件(/etc/my.cnf)中配置如下信息:
  1. 开启mysql慢查询日志开关
    show_query_log = 1
  2. 设置慢查询日志的时间为2秒,sql语句执行时间超过2秒,就会视为慢查询,记录慢查询日志
    long_query_time = 2
  3. 配置完成后,需要重启mysql服务器,会生成查看慢日志文件记录的信息/var/lib/mysql/localhost-slow.log
    重启mysql服务器:systemctl restart mysqld
    检查慢日志记录是否开启:show variables like ‘slow_query_log’
    查看慢查询日志:直接到mysql目录下打开/var/lib/mysql/localhost-slow.log这个文件查看即可

2.profile详情(查询所有sql耗时)

  1. 查看当前数据库是否支持profile详情(查看have_profiling的值)
select @@have_profiling;  --yes支持 no不支持
  1. 查看是否开启profiling,默认是关闭状态
select @@profiling; --0关闭 1打开
  1. 打开profiling
set profiling = 1;
  1. 查看所有sql执行时间
show profiles;

【MYSQL索引性能分析以及使用规则】_第1张图片
4. 查看执行query_id的sql语句各个阶段的耗时时情况

show profile cpu 

你可能感兴趣的:(mysql,数据库)