MySQL turns on slow query

Open and view the mysql slow query log
(1) Command method
 
set global log_queries_not_using_indexes=on ;
set global slow_query_log_file='/var/log/mysql/mysql-slow'.log;
set global long_query_time=0.1;
set global slow_query_log=on ;
 
Note 1:
    When slow_query_log_file is set, the directory must exist and mysql has permission to read and write the directory,
 
mkdir /var/log/mysql
chown mysql:mysql /var/log/mysql 
 
Otherwise, the following error will be reported:
mysql> set global slow_query_log_file='/var/log/mysql/mysql-slow.log';
ERROR 1231 (42000): Variable 'slow_query_log_file' can't be set to the value of '/var/log/mysql/mysql-slow.log'
 
Note 2:
     When the long_query_time parameter is set, it needs to be reconnected to take effect, and there is no need to restart the service.
     Otherwise, no changes can be seen through show variables like 'long_query_time'
 
(2) File method
Add the following information to the [mysqld] section
 
#Turn on sql logging without indexing
log_queries_not_using_indexes=on
 
#Set slow query logging location
slow_query_log_file=/var/log/mysql/mysql-slow.log
 
#More than how many seconds is a slow query and needs to be recorded
long_query_time=0.1
 
#Open slow query log
slow_query_log=on
 
 
Note:
a) The order and path of the configuration files loaded by the mysql client and server programs are as follows
/etc/my.cnf --> /etc/mysql/my.cnf --> /usr/local/mysql/etc/my.cnf --> ~/.my.cnf (--defaults-file= specify by yourself parameter file)
 
b) mysql adopts a centralized configuration file. The configuration file is divided into blocks, starting with a [NAME] independent line and ending with the next [NAME] independent line, which belongs to the configuration acting on the program specified by NAME.
There are two points to remember about the specific parameters inside:
1. In the same block [NAME], each parameter can appear repeatedly. For such repeated parameters, the parameters listed below by default override the parameters above.
2. The parameters in each [NAME] block are default parameter values, which will only take effect when the NAME command does not have these parameters.
For example, the port=3306 defined in [mysql],
When using mysql -uuser -ppasswd directly without the -P port parameter, the default connection port is 3306;
When using mysql -uuser -ppasswd -P3307, it will not connect to port 3306, but to port 3307. Parameters of other modules are analogous.
3. By default, MySQL starts with just enough resources. Therefore, these default values ​​of MySQL, in general, some key parameters cannot meet the needs of the production library.
4. Be clear that "there is no one best profile for all scenarios".
 
c)
[client] 是所有mysql客户端程序读取的配置块。针对的是MySQL客户端(不包含mysql命令,指的是具体应用程序,比如java的jdbc,C的mysql驱动等等)
[server]是所有服务端如mysqld会读取的配置块。一般不用设置,完全由[mysqld]替换。
[mysqldump]则是只有 mysqldump命令才会读取的配置文件,
[mysql] 是mysql命令这个客户端程序的配置块,
[mysqld]是mysql服务端程序mysqld 和 mysqld_safe 。在多实例环境下[mysqld3306]和[mysqld3307]可能会覆盖[mysqld]中的参数。
[mysqld_safe]针对mysqld_safe命令生效。
[mysqldump]针对mysqldump命令生效
[mysqladmin]针对mysqladmin命令生效
[mysqld_multi]针对mysqld_multi,管理MySQL单机多实例。
[mysqld3306]mysqld_multi管理的3306实例启动时mysqld读取的参数(也会读[mysqld]的参数,可以被覆盖)
[mysqld3307]mysqld_multi管理的3307实例启动时mysqld读取的参数(也会读[mysqld]的参数,可以被覆盖)
 
 
(3)查看
    slow_query_log
    show variables like 'log_queries_not_using_indexes';
    show variables like 'slow_query_log_file';
    show variables like 'long_query_time';
    show variables like 'slow_query_log';
 
    或
    show variables like '%log%';
 
 二 慢查询日志分析
 
# Time: 2016-07-17T08:37:58.844558Z
# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.000211  Lock_time: 0.000093 Rows_sent: 3  Rows_examined: 3
SET timestamp=1468744678;
select * from user;
 
a)查询执行时间 主要是时间戳的形式
b)用户及主机信息
c)SQL的执行信息:查询时间,锁时间,行数
d)SQL的执行时间
e)执行的查询语句
 
3)慢查询是日志分析工具
——用于对海量查询日志进行分析:mysqldumpslow,pt-query-digest
(1)mysqldumpslow 
 
(a)使用时报如下错误:
 
bash: /usr/local/mysql/bin/mysqldumpslow: /usr/bin/perl: bad interpreter: No such file or directory
 
解决方案:按照 perl
 
yum -y install perl perl-devel
 
(b)使用:
 
[root@64669e3cbf05 etc]# mysqldumpslow -s at -t 1 /var/log/mysql/mysql-slow.log
 
Reading mysql slow query log from /var/log/mysql/mysql-slow.log
Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), 0users@0hosts
  Time: N-N-17T08:N:N.908977Z
  # User@Host: root[root] @ localhost []  Id:     N
  # Query_time: N.N  Lock_time: N.N Rows_sent: N  Rows_examined: N
  SET timestamp=N;
  show tables
 
注:
     -s :排序字段
     -t  :查看top多少
 
只是做了初步的整理,比less直接看能找到最长度的
 
主要方法:1)找寻查询次数多,且时间长的sql,占用总时间百分比大的sql。
                   2)IO大的SQL:主要集中在扫描的行数越多,IO开销越大
                   3)索引命中率低的SQL:主要是扫描的记录数远远大于输出的记录数。 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326751203&siteId=291194637