zoukankan      html  css  js  c++  java
  • mysql慢查询

    1、慢查询的作用

      慢查询可以用于记录一些查询较慢的SQL语句,帮助我们后面分析问题所在,并为我们的SQL提供优化来节约资源,但是开启慢查询或多或少会带来一定的性能影响。

    2、慢查询的相关参数

      slow_query_log:是否开启慢查询日志,1表示开启,0表示关闭

      slow_query_log_file:指定慢查询日志的存放路径

      long_query_time:开启慢查询的阈值,默认情况下为10s

      log_output:慢查询的日志存储方式,通常有两种方式,file和table,存储方式为文件则性能更高

      log_queries_not_using_indexes:未使用索引的查询也被记录到慢查询日志

    3、开启方式

      注:开启方式有两种(当下 和永久),对于永久开启慢查询的方式,直接修改/etc/my.cf文件即可

    3.1 slow_query_log

      默认情况下slow_query_log的值为OFF,表示慢查询日志是禁用的,可以通过设置slow_query_log的值来开启

    mysql> show variables  like '%slow_query_log%';
     +---------------------+-----------------------------------------------+
     | Variable_name       | Value                                         |
     +---------------------+-----------------------------------------------+
     | slow_query_log      | OFF                                           |
     | slow_query_log_file | /home/WDPM/MysqlData/mysql/DB-Server-slow.log |
     +---------------------+-----------------------------------------------+
     2 rows in set (0.00 sec)
     
    mysql> set global slow_query_log=1;
     Query OK, 0 rows affected (0.09 sec)
    

      

    3.2 slow_query_log_file

      这个参数用于指定慢查询日志的存放路径

    mysql> show variables like 'slow_query_log_file';
     +---------------------+-----------------------------------------------+
     | Variable_name       | Value                                         |
     +---------------------+-----------------------------------------------+
     | slow_query_log_file | /home/WDPM/MysqlData/mysql/DB-Server-slow.log |
     +---------------------+-----------------------------------------------+
     1 row in set (0.00 sec)
    

    3.3 slow_query_time

      指定慢查询的阈值,默认为10s,修改之后时间显示未变,用 show global variables like 'long_query_time' 查询

    mysql> show variables like 'long_query_time%';
     +-----------------+-----------+
     | Variable_name   | Value     |
     +-----------------+-----------+
     | long_query_time | 10.000000 |
     +-----------------+-----------+
     1 row in set (0.00 sec)
     
    mysql> set global long_query_time=4;
     Query OK, 0 rows affected (0.00 sec)
     
    mysql> show variables like 'long_query_time';
     +-----------------+-----------+
     | Variable_name   | Value     |
     +-----------------+-----------+
     | long_query_time | 10.000000 |
     +-----------------+-----------+
    

    3.4 log_output

      指定日志的存储方式

    mysql> show variables like '%log_output%';
     +---------------+-------+
     | Variable_name | Value |
     +---------------+-------+
     | log_output    | FILE  |
     +---------------+-------+
     1 row in set (0.00 sec)
     
    mysql> set global log_output='TABLE';
     Query OK, 0 rows affected (0.00 sec)
     
    mysql> show variables like '%log_output%';
     +---------------+-------+
     | Variable_name | Value |
     +---------------+-------+
     | log_output    | TABLE |
     +---------------+-------+
     1 row in set (0.00 sec)
    

      试验查询

    mysql> select sleep(5) ;
     +----------+
     | sleep(5) |
     +----------+
     |        0 |
     +----------+
     1 row in set (5.00 sec)
    

     3.5 log_queries_not_using_indexes

      该系统变量指定未使用索引的查询也被记录到慢查询日志中

    mysql> show variables like 'log_queries_not_using_indexes';
     +-------------------------------+-------+
     | Variable_name                 | Value |
     +-------------------------------+-------+
     | log_queries_not_using_indexes | OFF   |
     +-------------------------------+-------+
     1 row in set (0.00 sec)
     
    mysql> set global log_queries_not_using_indexes=1;
     Query OK, 0 rows affected (0.00 sec)
     
    mysql> show variables like 'log_queries_not_using_indexes';
     +-------------------------------+-------+
     | Variable_name                 | Value |
     +-------------------------------+-------+
     | log_queries_not_using_indexes | ON    |
     +-------------------------------+-------+
     1 row in set (0.00 sec)
    

      

  • 相关阅读:
    E4A易安卓3.6无需注册编译Release
    PHP中几种常用的网页跳转代码
    超详细教你重新封装GHO(ndeer和绿茶)教程
    程序员和编码员之间的区别
    迅闪三层下载游戏无速度
    POST注册DZ论坛或发帖
    解决局域网文件共享设置
    妻子 情人 红颜知己
    照片与同行元素居中的方法
    【转】SVN使用教程总结
  • 原文地址:https://www.cnblogs.com/afeiiii/p/14385624.html
Copyright © 2011-2022 走看看