• MySQL 慢查询


    什么是慢查询

      就是很慢的查询啦,那多慢才算慢呢?这个你说了算,你要是觉得0.000000000000秒都算慢,那么就是说,你将每一条查询都认为是慢查询。

    慢查询有什么用呢

      没啥用,如果没有慢查询才好呢

      有慢查询的时候,证明该优化数据库或者查询语句了,只是一个相当于日志的概念,没事找事。

      

    查询相关项的开启状态

    #查看是否开启慢查询日志
    mysql> show variables like 'slow_query_log';
    +----------------+-------+
    | Variable_name  | Value |
    +----------------+-------+
    | slow_query_log | OFF   |
    +----------------+-------+
    1 row in set (0.06 sec)
    
    #查看是否开启没有使用索引的查询
    mysql> show variables like '%log_qu%';
    +-------------------------------+-------+
    | Variable_name                 | Value |
    +-------------------------------+-------+
    | log_queries_not_using_indexes | OFF   |
    +-------------------------------+-------+
    1 row in set (0.00 sec)
    
    #一个查询超过多少秒会被记录到慢查询日志中(如果是0,则表示每条命令都要记录)
    mysql> show variables like '%long_query%';
    +-----------------+-----------+
    | Variable_name   | Value     |
    +-----------------+-----------+
    | long_query_time | 10.000000 |
    +-----------------+-----------+
    1 row in set (0.01 sec)
    

      

    开启慢查询

    #开启慢查询
    mysql> set global slow_query_log=ON;
    Query OK, 0 rows affected (0.07 sec)
    
    #查看慢查询状态、以及慢查询的日志文件
    mysql> show variables like 'slow_query%';
    +---------------------+------------------------------------------+
    | Variable_name       | Value                                    |
    +---------------------+------------------------------------------+
    | slow_query_log      | ON                                       |
    | slow_query_log_file | /usr/local/mysql/data/localhost-slow.log |
    +---------------------+------------------------------------------+
    2 rows in set (0.00 sec)
    
    #设置查询超时时间,此处设为0,表示将每一条命令都记录到慢查询日志中
    mysql> set @@long_query_time = 0.000000;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> show variables like '%long_query%';
    +-----------------+----------+
    | Variable_name   | Value    |
    +-----------------+----------+
    | long_query_time | 0.000000 |
    +-----------------+----------+
    1 row in set (0.00 sec)
    
    mysql> set global log_queries_not_using_indexes=on;
    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)
    

      

    测试

    mysql> select * from category left join goods on category.id=goods.cate_id order by category.id;
    +----+---------+------+---------+-------------------+
    | id | cate    | id   | cate_id | name              |
    +----+---------+------+---------+-------------------+
    |  1 | food    |    2 |       1 | water             |
    |  1 | food    |    3 |       1 | rice              |
    |  2 | clothes |    1 |       2 | T-shirt           |
    |  3 | book    |    4 |       3 | C++ primer        |
    |  4 | sport   |    5 |       4 | basketbal         |
    |  5 | music   |    0 |       5 | You Are Not Alone |
    |  6 | video   | NULL |    NULL | NULL              |
    +----+---------+------+---------+-------------------+
    7 rows in set (0.00 sec)
    
    #查看慢查询日志
    localhost:~ root# cat /usr/local/mysql/data/localhost-slow.log
    
    # Time: 180730 15:54:14
    # User@Host: root[root] @ localhost []  Id:    10
    # Query_time: 0.000582  Lock_time: 0.000181 Rows_sent: 7  Rows_examined: 20
    SET timestamp=1532937254;
    select * from category left join goods on category.id=goods.cate_id order by category.id;
    

    使用mysqldumpslow

    localhost:~ root# mysqldumpslow -t 10 /usr/local/mysql/data/localhost-slow.log
    
    Reading mysql slow query log from /usr/local/mysql/data/localhost-slow.log
    Count: 10  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=7.0 (70), root[root]@localhost
      select * from category left join goods on category.id=goods.cate_id order by category.id
    

      

      

      

      

      

      

  • 相关阅读:
    Could not connect to '192.168.80.145' (port 22): Connection failed的解决办法(远程连不上xshell)
    分布式集群HBase启动后某节点的HRegionServer自动消失问题
    Hive环境的安装部署(完美安装)(集群内或集群外都适用)(含卸载自带mysql安装指定版本)
    大数据各子项目的环境搭建之建立与删除软连接(博主推荐)
    TeamViewer的下载、安装和使用(windows7、CentOS6.5和Ubuntu14.04(64bit))(图文详解)
    Python *的下载、安装和使用
    JetBrains PyCharm(Community版本)的下载、安装和初步使用
    Spark SQL概念学习系列之DataFrame与RDD的区别
    手游接入Facebook的那些坑
    J2EE基础篇——十三个规范
  • 原文地址:https://www.cnblogs.com/-beyond/p/9391105.html
Copyright © 2020-2023  润新知