• MySQL日志之慢查询日志(slow-log)


    mysql> show status like 'slow_queries';    #慢查询数
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | Slow_queries  | 0     |
    +---------------+-------+
    1 row in set (0.00 sec)
    mysql> show global variables like 'slow%';   #全局 慢查询变量
    +---------------------+------------------------------------------+
    | Variable_name       | Value                                    |
    +---------------------+------------------------------------------+
    | slow_launch_time    | 2    # 表示如果建立线程花费了比这个值更长的时间,slow_launch_threads 计数器将增加                                    |
    | slow_query_log      | OFF                                      |
    | slow_query_log_file | /usr/local/mysql/data/localhost-slow.log |
    +---------------------+------------------------------------------+
    3 rows in set (0.00 sec)
    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=0.01;     #设置慢查询时间为1   global下次连接生效
    Query OK, 0 rows affected (0.00 sec)
    

    mysql> show variables like 'long_query_time';
    +-----------------+-----------+
    | Variable_name | Value |
    +-----------------+-----------+
    | long_query_time | 10.000000 |
    +-----------------+-----------+
    1 row in set (0.00 sec)

    mysql> set long_query_time=0.01; #当前生效
    Query OK,
    0 rows affected (0.00 sec)

    mysql> show variables like 'long_query_time';
    +-----------------+----------+
    | Variable_name | Value |
    +-----------------+----------+
    | long_query_time | 0.010000 |
    +-----------------+----------+
    1 row in set (0.00 sec)

     开启慢查询

    mysql> set slow_query_log=on;
    ERROR 1229 (HY000): Variable 'slow_query_log' is a GLOBAL variable and should be set with SET GLOBAL
    mysql> set global slow_query_log=on;
    Query OK, 0 rows affected (0.04 sec)

    是否记录未使用索引的SQL

    mysql> show global 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 log_queries_not_using_indexes=on;
    ERROR
    1229 (HY000): Variable 'log_queries_not_using_indexes' is a GLOBAL variable and should be set with SET GLOBAL
    mysql
    > set global log_queries_not_using_indexes=on;
    Query OK,
    0 rows affected (0.00 sec)
    mysql
    > show global variables like '%log_queries_not_using_indexes%';
    +-------------------------------+-------+
    | Variable_name | Value |
    +-------------------------------+-------+
    | log_queries_not_using_indexes | ON |
    +-------------------------------+-------+
    1 row in set (0.00 sec)

    再次连接

    mysql> show global variables like 'slow%';
    +---------------------+------------------------------------------+
    | Variable_name       | Value                                    |
    +---------------------+------------------------------------------+
    | slow_launch_time    | 2                                        |
    | slow_query_log      | ON                                       |
    | slow_query_log_file | /usr/local/mysql/data/localhost-slow.log |
    +---------------------+------------------------------------------+
    3 rows in set (0.00 sec)

    慢查询不是只有查询数据才算,只要是操作数据库超过时间显限制都算

    [root@localhost data]# /usr/local/mysql/bin/mysqldumpslow localhost-slow.log 
    

    Reading mysql slow query log from localhost-slow.log
    Count: 4 Time=9.49s (37s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@2hosts
    INSERT INTO curriculum SELECT * FROM curriculum

    Count: 1 Time=5.39s (5s) Lock=0.00s (0s) Rows=27136.0 (27136), root[root]@[10.16.134.131]
    select * FROM curriculum where curr_id<>N

    [root@localhost data]# /usr/local/mysql/bin/mysqldumpslow localhost-slow.log

    Reading mysql slow query log from localhost-slow.log
    Count: 4 Time=9.49s (37s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@2hosts
    INSERT INTO curriculum SELECT * FROM curriculum

    Count: 1 Time=5.39s (5s) Lock=0.00s (0s) Rows=27136.0 (27136), root[root]@[10.16.134.131]
    select * FROM curriculum where curr_id<>N

    [root@localhost data]# /usr/local/mysql/bin/mysqldumpslow localhost-slow.log

    Reading mysql slow query log from localhost-slow.log
    Count: 4 Time=9.49s (37s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@2hosts
    INSERT INTO curriculum SELECT * FROM curriculum

    Count: 1 Time=5.39s (5s) Lock=0.00s (0s) Rows=27136.0 (27136), root[root]@[10.16.134.131]
    select * FROM curriculum where curr_id<>N

    Count: 1 Time=0.06s (0s) Lock=0.00s (0s) Rows=3.0 (3), root[root]@localhost
    select * from sakila.payment_copy LIMIT N,N

    Count: 1 Time=0.05s (0s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost
    set global slow_query_log=on

  • 相关阅读:
    计算机网络学习目录
    手把手教你玩微信小程序跳一跳
    (三)python函数式编程
    跟托福说分手
    (二)python高级特性
    BitCoin工作原理
    反向传播的工作原理(深度学习第三章)
    1.22计划
    梯度下降——神经网络如何学习?(深度学习第二章)
    什么是神经网络 (深度学习第一章)?
  • 原文地址:https://www.cnblogs.com/yhq-qhh/p/11284368.html
Copyright © 2020-2023  润新知