MySQL Performance Tuning (c)

 

A. Explain command

explain Commands

explain command shows how to use the index to handle mysql select statement and connection table. You can help choose a better index and write more optimized query, use the plus explain before the select statement on it.

Example :

 

 

Mysql implementation of the plan illustration:

 

II. Slow query

1. What is the slow queries?

       MySQL slow query, full name is slow query log is a log provided by MySQL, the response time exceeds the threshold used to record statements in MySQL. Specific environment, running for more than long_query_time value of SQL statements, it will be logged to the slow query log. Long_query_time The default is 10, meaning that the recording operation is 10 seconds or more statements. By default, MySQL database does not start slow query log, you need to manually set this parameter. Of course, if it is not tuning required, generally we do not recommend activating this feature, because the slow query log will open more or less bring some performance impact. Support will slow query log records written to the log files and database tables.

Parameter Description:

slow_query_log        slow query on state (ON is ON, OFF OFF )
slow_query_log_file    slow query log storage location

long_query_time       queries exceed the number of seconds before the recording (logging in seconds)

 

2. Slow Query Procedure

1. Check whether to open slow query function

2. Set Methods

方法一:全局变量设置

1. slow_query_log 全局变量设置为“ON”状态

mysql> set global slow_query_log='ON';

2.设置慢查询日志存放的位置

mysql> set global slow_query_log_file='/usr/local/mysql/data/slow.log';

3.查询超过1秒就记录

mysql> set global long_query_time=1;

 方法二:配置文件设置

  修改配置文件my.cnf,在[mysqld]下的下方加入

[mysqld]

slow_query_log = ON

slow_query_log_file = /usr/local/mysql/data/slow.log

long_query_time = 1

3.重启MySQL服务

   service mysqld restart

 4.查看设置后的参数

设置完后我们来测试一下是否开启慢查询

1.执行一条慢查询SQL语句

mysql> select sleep(2);

2.查看是否生成慢查询日志

ls /usr/local/mysql/data/slow.log

如果日志存在,MySQL开启慢查询设置成功!

3. 对慢查询的分析

使用mysqldumpslow工具

   在生产环境中,如果要手工分析日志,查找、分析SQL,显然是个体力活。

MySQL提供了日志分析工具mysqldumpslow

查看mysqldumpslow的帮助信息:

三 .MySQL性能分析语句show profile

什么是show profile?

          show profiles 这个命令非常强大,能清晰的展示每条SQL的持续时间。通常结合show profile 命令可以更加详细的展示其耗时信息。这样就能很容易的分析出,到底慢在哪个环节了。比较遗憾的是,在MySQL中,该命令默认是关闭状态的。

Show profile的使用

1..开启命令:

set profiling = ON; 或等于1

2.查看是否生效:

 Value的取值范围有两个:其中 ON 为开启状态,OFF为关闭状态。

值得注意的是:通过上述命令开启后仅在当前会话有效。

作用:

      show profiles 其作用为显示当前会话服务器最新收到的15SQL的性能信息。

其中包括:持续时间,以及Query_ID。我们可以通过Query_ID分析其性能

1.Query_ID 表示执行SQL的唯一标识。

2.Duration 表示持续时间,默认单位为秒。

3.Query 就是我们所执行的SQL

注意:

 show profiles 语句 默认显示的是服务端接收到的最新的15条语句。

我们可以通过以下语句进行修改默认值:

set profiling_history_size =20;

profiling_history_size最大取值取值范围为[0,100]

当超过100时,则会设置自动设置为最大值100

当小于0时,则会自动设置最小值为0

当其等于0时,其效果等同于 set profiling=0,关闭性能分析模式

show profile命令的使用

  示例:Query_ID

当结果显示的比较多时,可以通过 limit 选项,来显示指定的行数.

   通过上述结果,我们可以非常清楚的查看每一步的耗时,其中(Druation的单位为秒)。这样,当我们遇到一条慢SQL时,就能很清楚的知道,为什么慢,慢在哪一步了。可以进行针对性的优化。我们对优化后的SQL语句也能查看其持续时间,是否符合我们的指标.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/limengcheng/p/12116404.html