zoukankan      html  css  js  c++  java
  • linux下开启mysql慢查询,分析查询语句

    一、为什么要开启这个查询呢?

    数据库是很容易产生瓶颈的地方,现在Nosql大家讨论这么热,估计都被数据库搞郁闷 了。mysql中最影响速度的就是那些查询非常慢的语句,这些慢的语句,可能是写的不够合理或者是大数据下多表的联合查询等等,所以我们要找出这些语句, 分析原因,加以优化。这也是发这篇博文的原因。

    二、开启mysql的慢查询

    方法1:用命令开启慢查询

    mysql> show variables like "%long%"; //查看一下默认为慢查询的时间10秒

    +-----------------+----------+
    | Variable_name   | Value    |
    +-----------------+----------+
    | long_query_time | 10.000000 |
    +-----------------+----------+
    1 row in set (0.23 sec)

    mysql> set global long_query_time=2; //设置成2秒,加上global,下次进mysql已然生效

    Query OK, 0 rows affected (0.00 sec)

    mysql> show variables like "%slow%"; //查看一下慢查询是不是已经开启

    mysql> show variables like "%slow%";
    +---------------------+-----------------------------------+
    | Variable_name       | Value                             |
    +---------------------+-----------------------------------+
    | log_slow_queries    | ON                                |
    | slow_launch_time    | 2                                 |
    | slow_query_log      | ON                                |
    | slow_query_log_file | /home/mysql/data/slow_queries.log |
    +---------------------+-----------------------------------+
    4 rows in set (0.00 sec)


    mysql> set slow_query_log='ON'; //加上global,不然会报错的。

    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.28 sec)

    mysql> show variables like "%slow%"; //查看是否已经开启

    mysql> show variables like "%slow%"; 
    +---------------------+-----------------------------------+
    | Variable_name       | Value                             |
    +---------------------+-----------------------------------+
    | log_slow_queries    | ON                                |
    | slow_launch_time    | 2                                 |
    | slow_query_log      | ON                                |
    | slow_query_log_file | /home/mysql/data/slow_queries.log |
    +---------------------+-----------------------------------+
    4 rows in set (0.00 sec)


    方法2、修改mysql的配置文件my.cnf

    在[mysqld]里面加上以下内容

    long_query_time = 2
    log-slow-queries = /usr/local/mysql/mysql-slow.log

    重起一下
        /usr/local/mysql/libexec/mysqld restart

  • 相关阅读:
    初识python 2.x与3.x 区别
    装饰器
    函数的进阶
    Spring Boot启动问题:Cannot determine embedded database driver class for database type NONE
    22.Spring Cloud Config安全保护
    23.Spring Cloud Bus 无法更新问题(踩坑) Spring cloud config server Could not fetch remote for master remote
    24.Spring Cloud之Spring Cloud Config及Spring Cloud Bus
    Spring Boot整合Spring Data Elasticsearch 踩坑
    项目中Spring Security 整合Spring Session实现记住我功能
    32.再谈SpringBoot文件上传
  • 原文地址:https://www.cnblogs.com/liqiu/p/2557888.html
Copyright © 2011-2022 走看看