zoukankan      html  css  js  c++  java
  • mysql优化思路

    通过脚本,刷新观察mysql的status,观察是否有周期性故障活波动,
    一般由访问高峰或者缓存失效引起,家缓存并更改缓存失效策略,是失效时间分散或页面定时失,

    SHOW PROCESSLIST显示哪些线程正在运行。

    您也可以使用mysqladmin processlist语句得到此信息。如果您有SUPER权限,您可以看到所有线程。否则,您只能看到您自己的线程
    mysql 开启慢查询日志

    slow_query_log 这个参数设置为ON,可以捕获执行时间超过一定数值的SQL语句。
    long_query_time 当SQL语句执行时间超过此数值时,就会被记录到日志中,建议设置为1或者更短
    slow_query_log_file 记录日志的文件名。
    log_queries_not_using_indexes 这个参数设置为ON,可以捕获到所有未使用索引的SQL语句,尽管这个SQL语句有可能执行得挺快。

    使用profiler来分析一条query的执行时间和性能瓶颈,
    开启 profiling ;

    set profiling=1;

    随便执行一条语句 select count(*) from user where id>2;

    show profiles;

    得到

    复制代码
    +----------+------------+--------------------------------------+
    | Query_ID | Duration   | Query                                |
    +----------+------------+--------------------------------------+
    |        2 | 0.00009200 | set profiling=1                      |
    |        5 | 0.02003525 | select count(*) from user where id>2 |
    +----------+------------+--------------------------------------+
    复制代码

    包含一个query_id和执行时间和query语句
    通过query_id可以查看到更详细的信息;

    show profile cpu ,block io  for query 5;
    复制代码
    +----------------------+----------+----------+------------+--------------+---------------+
    | Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
    +----------------------+----------+----------+------------+--------------+---------------+
    | starting             | 0.000170 | 0.000000 |   0.000000 |            0 |             0 |
    | checking permissions | 0.000019 | 0.000000 |   0.000000 |            0 |             0 |
    | Opening tables       | 0.000033 | 0.000000 |   0.000000 |            0 |             0 |
    | init                 | 0.000061 | 0.000000 |   0.000000 |            0 |             0 |
    | System lock          | 0.000021 | 0.000000 |   0.000000 |            0 |             0 |
    | optimizing           | 0.000021 | 0.000000 |   0.000000 |            0 |             0 |
    | statistics           | 0.010826 | 0.000000 |   0.000000 |          184 |             0 |
    | preparing            | 0.000041 | 0.000000 |   0.000000 |            0 |             0 |
    | executing            | 0.000005 | 0.000000 |   0.000000 |            0 |             0 |
    | Sending data         | 0.008731 | 0.008000 |   0.000000 |            0 |             0 |
    | end                  | 0.000020 | 0.000000 |   0.000000 |            0 |             0 |
    | query end            | 0.000018 | 0.000000 |   0.000000 |            0 |             0 |
    | closing tables       | 0.000012 | 0.000000 |   0.000000 |            0 |             0 |
    | freeing items        | 0.000032 | 0.000000 |   0.000000 |            0 |             0 |
    | cleaning up          | 0.000027 | 0.000000 |   0.000000 |            0 |             0 |
    +----------------------+----------+----------+------------+--------------+---------------+
    复制代码

     
    对mysql服务器的优化不要一上来就去优化sql语句,应该首先观察全局情况,至少要先搞清楚
    问题出在哪,应该使用脚本来观察服务器一段时间(一天或更长)的健康状况,比如cpu,io,进程连接数等
    最后才分析具体原因处在哪里;针对解决;

    通过 mysqladmin来查看mysql的状态;

    复制代码
    mysqladmin -P3306 -uroot -p123456 -h127.0.0.1 -r -i 1 ext |
    awk -F"|" 
    "BEGIN{ count=0; }"
    '{ if($2 ~ /Variable_name/ && ((++count)%20 == 1)){
        print "----------|---------|--- MySQL Command Status --|----- Innodb row operation ----|-- Buffer Pool Read --";
        print "---Time---|---QPS---|select insert update delete|  read inserted updated deleted|   logical    physical";
    }
    else if ($2 ~ /Queries/){queries=$3;}
    else if ($2 ~ /Com_select /){com_select=$3;}
    else if ($2 ~ /Com_insert /){com_insert=$3;}
    else if ($2 ~ /Com_update /){com_update=$3;}
    else if ($2 ~ /Com_delete /){com_delete=$3;}
    else if ($2 ~ /Innodb_rows_read/){innodb_rows_read=$3;}
    else if ($2 ~ /Innodb_rows_deleted/){innodb_rows_deleted=$3;}
    else if ($2 ~ /Innodb_rows_inserted/){innodb_rows_inserted=$3;}
    else if ($2 ~ /Innodb_rows_updated/){innodb_rows_updated=$3;}
    else if ($2 ~ /Innodb_buffer_pool_read_requests/){innodb_lor=$3;}
    else if ($2 ~ /Innodb_buffer_pool_reads/){innodb_phr=$3;}
    else if ($2 ~ /Uptime / && count >= 2){
      printf("|%6d %6d %6d %6d",com_select,com_insert,com_update,com_delete);
      printf("|%6d %8d %7d %7d",innodb_rows_read,innodb_rows_inserted,innodb_rows_updated,innodb_rows_deleted);
      printf("|%10d %11d
    ",innodb_lor,innodb_phr);
    }}'
    复制代码

    还可以在 printf的输出重定向到一个文件,这样就可以通过文件数据进行可视化分析;

  • 相关阅读:
    jquery
    实现元素垂直居中
    浏览器 标准模式和怪异模式
    cookie session ajax
    React props.children
    使用React.lazy报错Import in body of module; reorder to top import/first
    state 和 props 之间的区别
    Harbor打怪升级
    Centos7下安装yum工具
    正则表达式匹配两个特殊字符中间的内容(特殊字符不显示)
  • 原文地址:https://www.cnblogs.com/zox2011/p/7822831.html
Copyright © 2011-2022 走看看