首页 > 代码库 > mysql慢查询

mysql慢查询

  

  有时我们为了定位系统中效率比较低下的Query语法,需要打开慢查询日志,也就是SlowQuery log。打开慢查询日志的相关命令如下:

查看

mysql> showvariables like ‘%slow%‘;

+---------------------+-------------------------------------------+

|Variable_name       |Value                                    |

+---------------------+-------------------------------------------+

|log_slow_queries    |ON                                       |

|slow_launch_time    |2                                        |

|slow_query_log      |ON                                       |

| slow_query_log_file| /app/mysql-5.5.32/data/localhost-slow.log |

+---------------------+-------------------------------------------+

4 rows in set (0.00sec)

 

mysql> show globalstatus like ‘%slow%‘;

+---------------------+-------+

|Variable_name       | Value |

+---------------------+-------+

| Slow_launch_threads| 0     |

|Slow_queries        |0     |

+---------------------+-------+

2 rows in set (0.00sec)

 

打开慢查询日志可能会对系统性能有一点点影响,如果你的MySQL是主从结构,可以考虑打开其中一台从服务器的慢查询日志,这样既可以监控慢查询,对系统性能影响也会很小。另外,可以用MySQL自带的命令mysqldumpslow进行查询。

比如:下面的命令可以查出访问次数最多的20SQL语句:

[root@localhost ~]#mysqldumpslow -s c -t 20  /app/mysql-5.5.32/data/localhost-slow.log

 

注意:

-s ORDER     what to sort by (al, at, ar, c, l, r, t), ‘at‘ isdefault

               al: average lock time

               ar: average rows sent

               at: average query time

                c: count

                l:lock time

                r: rows sent

                t: query time 

 -t NUM       just show the top n queries


本文出自 “一点点” 博客,请务必保留此出处http://pengai.blog.51cto.com/6326789/1890364

mysql慢查询