zoukankan      html  css  js  c++  java
  • msyql开启慢查询以及分析慢查询

    慢查询的用途是用来发现执行时间长的查询语句,以便对这些语句进行优化

    [mysqld]              #在这里面增加,其它地方无效
    #server-id=1
    #log-bin=master-bin
    slow_query_log=1      #开启查询
    slow_query_log_file=/bp/mysql.log       
    #慢查询日志文件存放位置,注意这个比较坑,要求目标目录可写,最好还是自己创建一个目录,并-R 添加写权限,这个是网上说的,不过我实验时发现赋777权限才可以
    long_query_time=1                       #定义超过1秒就算是慢查询,一般是5到10秒吧
    

    重启服务

    MariaDB [(none)]> show variables like '%slow%';
    +---------------------+--------------------------------------------------------------------------------------------------------------+
    | Variable_name       | Value                                                                                                        |
    +---------------------+--------------------------------------------------------------------------------------------------------------+
    | log_slow_filter     | admin,filesort,filesort_on_disk,full_join,full_scan,query_cache,query_cache_miss,tmp_table,tmp_table_on_disk |
    | log_slow_queries    | ON                #开启成功                                                                                           |
    | log_slow_rate_limit | 1                                                                                                            |
    | log_slow_verbosity  |                                                                                                              |
    | slow_launch_time    | 2                                                                                                            |
    | slow_query_log      | ON                                                                                                           |
    | slow_query_log_file | /bp/mysql.log                                                                                                |
    +---------------------+--------------------------------------------------------------------------------------------------------------+
    7 rows in set (0.00 sec)
    
    MariaDB [(none)]> 
    

    那么怎么测试慢查询是否成功呢
    比较简单的一种方法就是select sleep(5);这条语句会执行5秒钟
    看一下慢查询日志文件

    [root@localhost bp]# cat mysql.log 
    /usr/libexec/mysqld, Version: 5.5.52-MariaDB (MariaDB Server). started with:
    Tcp port: 0  Unix socket: /var/lib/mysql/mysql.sock
    Time                 Id Command    Argument
    # Time: 170806 23:37:32
    # User@Host: root[root] @ localhost []
    # Thread_id: 2  Schema:   QC_hit: No
    # Query_time: 2.001045  Lock_time: 0.000000  Rows_sent: 1  Rows_examined: 0
    SET timestamp=1502087852;
    select sleep(2);
    # Time: 170806 23:37:38
    # User@Host: root[root] @ localhost []
    # Thread_id: 2  Schema:   QC_hit: No
    # Query_time: 3.000681  Lock_time: 0.000000  Rows_sent: 1  Rows_examined: 0
    SET timestamp=1502087858;
    select sleep(3);
    # Time: 170806 23:37:46
    # User@Host: root[root] @ localhost []
    # Thread_id: 2  Schema:   QC_hit: No
    # Query_time: 5.000415  Lock_time: 0.000000  Rows_sent: 1  Rows_examined: 0
    SET timestamp=1502087866;
    select sleep(5);
    [root@localhost bp]# 
    

    终端中执行以下命令
    mysqldumpslow -t 10 查看访问时间最长的10个sql语句
    mysqldumpslow -s c -t 10 查看访问次数最多的10个sql语句
    也可以考虑安装第三方分析工具,例如pt-query-digest
    具体自行百度吧(因为我运行之后报错了)

  • 相关阅读:
    每天一篇文献:A SURVEY OF LEARNING FROM DEMONSTRATION USED IN ROBOTICS
    PyBullet(七)在PyBullet中使用VR
    期刊模板搜索网址
    论文阅读:Robot Program Parameter Inference via Differentiable Shadow Program Inversion
    visio画图如何插入到latex中
    win10下TensorFlow-GPU安装(GTX1660+CUDA10+CUDNN7.4)
    Object detection with localization using Unity Barracuda and ARFoundation
    论文阅读:Design and Implementation of a Virtual Reality Application for Mechanical Assembly Training
    Qt开发经验小技巧151-155
    Qt开发经验小技巧146-150
  • 原文地址:https://www.cnblogs.com/biaopei/p/7730491.html
Copyright © 2011-2022 走看看