首页 » MySQL » 双11网站访问慢

双11网站访问慢

 
文章目录

 

记一次网站慢解决方法

故障描述

访问变慢,有的打不开,加载特别慢。

解决方法

  1. 上服务器查看负载正常,4核的cpu,8G内存的去主机,负载没有到2,所以算是正常的。
  2. 内存使用正常,还有100m的free,不包括buffer和cache。
  3. 查看网卡流量正常,可以用iftop查看,总流量没有超过购买的带宽,或者用sar -n DEV 1 2
  4. 查看磁盘io是否正常,iostat查看iowait和idle等是否正常或sar -b 1 2 ,发现都很正常。
  5. 查看top,发现php占用了很大的cpu,进而查看ps -ef |grep php-fpm|grep -v “grep”|wc -l发现有100个,是php-fpm.conf设置的最大值,说明php连接很多,由于总体连接数,不是很多,那就可能是连接mysql占用了,后端的问题。
  6. 查看并发数据,发现1000多一点。netsta -an|grep ES|grep ip:80|wc -l,也是在正常的,反正服务器负载不高,
  7. 由于连接数不高,那可能是后面的数据库和php程序的问题,那么查看数据库,发现cpu到达了100%,那很明显肯定是数据库的问题,然后查看慢日志,发现太多的查询都要1秒以上,很大,一秒内都有20多条。

发现数据库慢是由于慢查询,查看及解决方法

  1. 首先查看慢查询日志,抓出哪些语句执行比较慢。
  2. 发现对其中几个同样或者说类似的语句出现慢的情况最多,
  3. 对这个表查询条件里的字段作索引
alter table ods_orders add index order_u_id_index(order_u_id, order_status, order_delivery_status, order_pay_status);
alter table ods_orders add index order_index(u_id, order_status, order_delivery_status, order_pay_status);
alter table uc_cart add index u_id_shop_id(u_id, cart_shop_id);

以上做了联合索引
4. 然后发现网站速度就快了,问题就解决了,可以通过show processlist;查看。

mysql相关慢查询用sql查询

  • 查看状态:show status;
  • 查看慢查询条数:show global status like ‘%slow%’;
  • 查看最大连接数: show variables like ‘max_connections’;
  • 查看服务器启动后已经同时使用的连接的最大数量: show status like ‘Maxusedconnections’;
  • 查看当前的连接数:show global status like ‘Threads_connected’;或者show processlist下面会显示多少行,
  • 查看慢查询:show processlist;可以显示执行时间,单位是秒。

建议mysql开启慢查询

永久开启慢查询

一般建议开启时间为1秒,超过1秒的都记录。 在【mysqld】下面添加以下:

long_query_time = 1 #超过1秒记录
slow_query_log = 1 #开启
log-slow-queries = /application/mysql/logs/mysql-slow.log
log_queries_not_using_indexes #查询没有用到索引的记录到日志,这样可以每天或定时查看日志,然后对慢的创建索引之类的。

用sql语句临时开启

set global long_query_time=1;
set slow_query_log='ON';
show variables like '%slow%;#查看是否开启
show variables like 'long%';#查看多长时间就记录日志

mysql5.7开始慢查询设置有点变化如下:

mysql5.7.16设置慢查询:
long_query_time = 2

slow_query_log = 1

slow_query_log_file = /var/log/mysql_slow.log

log_queries_not_using_indexes = ON

mysql> show variables like '%query%';

+------------------------------+-------------------------+

| Variable_name                | Value                   |

+------------------------------+-------------------------+

| binlog_rows_query_log_events | OFF                     |

| ft_query_expansion_limit     | 20                      |

| have_query_cache             | YES                     |

| long_query_time              | 1.000000                |

| query_alloc_block_size       | 8192                    |

| query_cache_limit            | 1048576                 |

| query_cache_min_res_unit     | 4096                    |

| query_cache_size             | 1048576                 |

| query_cache_type             | OFF                     |

| query_cache_wlock_invalidate | OFF                     |

| query_prealloc_size          | 8192                    |

| slow_query_log               | ON                      |

| slow_query_log_file          | /var/log/mysql_slow.log |

+------------------------------+-------------------------+

 

用explain来查看慢的原因

explain查看select语句的执行计划,看看有没有走索引去查询。
用了索引,查询只用扫一行,rows:1如下:

mysql> explain select * from test where name='oldboy'\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: test
         type: ref
possible_keys: index_name
          key: index_name
      key_len: 60
          ref: const
         rows: 1
        Extra: Using where; Using index
1 row in set (0.01 sec)

 

原文链接:双11网站访问慢,转载请注明来源!

0