MySQL通过添加索引解决线上数据库服务器压力大问题

昨天线上应用反馈晚上 19:30-19:43以及 20:13到20:21两个时间段所有人包括我们自己建的教室都进不去,

classroom B套无法登陆,主页无法访问,已有超过10个老师和学生反馈进不去教室的问题

通过监控查看出现问题时数据库服务器的cpu使用率,负载,io,内存使用,swap剩余量等状况,发现数据库当时压力特别大,

数据库连接数:

数据库慢查询也出现很多

查看慢查询日志,发现一个SQL在慢查询日志中频繁出现切执行时间较长

# [email protected]: cms[cms] @  [172.17.43.24]
# Query_time: 10.252490  Lock_time: 0.000052 Rows_sent: 1  Rows_examined: 2345869
SET timestamp=1522065887;
select participan0_.id as id19_, participan0_.conferenceid as conferen2_19_, participan0_.name as name19_, participan0_.phone as phone19_, participan0_
.pin as pin19_, participan0_.email as email19_, participan0_.mobile as mobile19_, participan0_.valid as valid19_, participan0_.userdefine1 as userdefin
e9_19_, participan0_.userdefine2 as userdefine10_19_, participan0_.userdefine3 as userdefine11_19_, participan0_.userdefine4 as userdefine12_19_ from p
articipant participan0_ where (participan0_.conferenceid=2680447 )and(participan0_.pin='1219' );
# [email protected]: cms[cms] @  [172.17.43.25]
# Query_time: 10.297055  Lock_time: 0.000050 Rows_sent: 1  Rows_examined: 2345869
SET timestamp=1522065887;
select participan0_.id as id19_, participan0_.conferenceid as conferen2_19_, participan0_.name as name19_, participan0_.phone as phone19_, participan0_
.pin as pin19_, participan0_.email as email19_, participan0_.mobile as mobile19_, participan0_.valid as valid19_, participan0_.userdefine1 as userdefin
e9_19_, participan0_.userdefine2 as userdefine10_19_, participan0_.userdefine3 as userdefine11_19_, participan0_.userdefine4 as userdefine12_19_ from p
articipant participan0_ where (participan0_.conferenceid=2697493 )and(participan0_.pin='1492' );
# [email protected]: cms[cms] @  [172.17.43.25]
# Query_time: 10.319839  Lock_time: 0.000048 Rows_sent: 1  Rows_examined: 2345869
SET timestamp=1522065887;
select participan0_.id as id19_, participan0_.conferenceid as conferen2_19_, participan0_.name as name19_, participan0_.phone as phone19_, participan0_
.pin as pin19_, participan0_.email as email19_, participan0_.mobile as mobile19_, participan0_.valid as valid19_, participan0_.userdefine1 as userdefin
e9_19_, participan0_.userdefine2 as userdefine10_19_, participan0_.userdefine3 as userdefine11_19_, participan0_.userdefine4 as userdefine12_19_ from p
articipant participan0_ where (participan0_.conferenceid=2680355 )and(participan0_.pin='9590' );
# [email protected]: cms[cms] @  [172.17.43.24]
# Query_time: 10.163372  Lock_time: 0.000063 Rows_sent: 1  Rows_examined: 2345872
SET timestamp=1522065887;
select participan0_.id as id19_, participan0_.conferenceid as conferen2_19_, participan0_.name as name19_, participan0_.phone as phone19_, participan0_
.pin as pin19_, participan0_.email as email19_, participan0_.mobile as mobile19_, participan0_.valid as valid19_, participan0_.userdefine1 as userdefin
e9_19_, participan0_.userdefine2 as userdefine10_19_, participan0_.userdefine3 as userdefine11_19_, participan0_.userdefine4 as userdefine12_19_ from p
articipant participan0_ where (participan0_.conferenceid=2731041 )and(participan0_.pin='1506' );
# [email protected]: cms[cms] @  [172.17.43.24]
# Query_time: 9.950549  Lock_time: 0.000073 Rows_sent: 1  Rows_examined: 2345881
SET timestamp=1522065887;
select participan0_.id as id19_, participan0_.conferenceid as conferen2_19_, participan0_.name as name19_, participan0_.phone as phone19_, participan0_
.pin as pin19_, participan0_.email as email19_, participan0_.mobile as mobile19_, participan0_.valid as valid19_, participan0_.userdefine1 as userdefin
e9_19_, participan0_.userdefine2 as userdefine10_19_, participan0_.userdefine3 as userdefine11_19_, participan0_.userdefine4 as userdefine12_19_ from p
articipant participan0_ where (participan0_.conferenceid=2682013 )and(participan0_.pin='6086' );
# [email protected]: cms[cms] @  [172.17.43.25]
# Query_time: 9.992145  Lock_time: 0.000051 Rows_sent: 1  Rows_examined: 2345879
SET timestamp=1522065887;
select participan0_.id as id19_, participan0_.conferenceid as conferen2_19_, participan0_.name as name19_, participan0_.phone as phone19_, participan0_
.pin as pin19_, participan0_.email as email19_, participan0_.mobile as mobile19_, participan0_.valid as valid19_, participan0_.userdefine1 as userdefin
e9_19_, participan0_.userdefine2 as userdefine10_19_, participan0_.userdefine3 as userdefine11_19_, participan0_.userdefine4 as userdefine12_19_ from p
articipant participan0_ where (participan0_.conferenceid=2697493 )and(participan0_.pin='1103' );

查看该SQL的执行计划发现走了全表扫描,扫描了200多万行的数据;

mysql> explain select participan0_.id as id19_, participan0_.conferenceid as conferen2_19_, participan0_.name as name19_, participan0_.phone as phone19_,
 participan0_.pin as pin19_, participan0_.email as email19_, participan0_.mobile as mobile19_, participan0_.valid as valid19_, 
 participan0_.userdefine1 as userdefine9_19_, participan0_.userdefine2 as userdefine10_19_, participan0_.userdefine3 as userdefine11_19_,
 participan0_.userdefine4 as userdefine12_19_ from participant participan0_ where (participan0_.conferenceid=2724963 )and(participan0_.pin='5476' );
+----+-------------+--------------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table        | type | possible_keys | key  | key_len | ref  | rows    | Extra       |
+----+-------------+--------------+------+---------------+------+---------+------+---------+-------------+
|  1 | SIMPLE      | participan0_ | ALL  | NULL          | NULL | NULL    | NULL | 2042005 | Using where |
+----+-------------+--------------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.02 sec)

发现该表where条件的列上没有索引:

mysql> show index from participant;
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table       | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| participant |          0 | PRIMARY  |            1 | id          | A         |     2384122 |     NULL | NULL   |      | BTREE      |         |               |
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

和发开沟通后在该表上加上以下索引:

mysql> ALTER  TABLE  `participant`  ADD  INDEX index_conferenceid (`conferenceid`); 
Query OK, 0 rows affected (9.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER  TABLE  `participant`  ADD  INDEX index_pin (`pin`);
Query OK, 0 rows affected (6.96 sec)
Records: 0  Duplicates: 0  Warnings: 0

再次查看该SQL的执行计划:

mysql>  explain select participan0_.id as id19_, participan0_.conferenceid as conferen2_19_, participan0_.name as name19_, participan0_.phone as phone19_, participan0_.pin as pin19_, participan0_.email as email19_, participan0_.mobile as mobile19_, participan0_.valid as valid19_, participan0_.userdefine1 as userdefine9_19_, participan0_.userdefine2 as userdefine10_19_, participan0_.userdefine3 as userdefine11_19_, participan0_.userdefine4 as userdefine12_19_ from participant participan0_ where (participan0_.conferenceid=2724963 )and(participan0_.pin='5476' );
+----+-------------+--------------+-------------+------------------------------+------------------------------+---------+------+------+------------------------------------------------------------+
| id | select_type | table        | type        | possible_keys                | key                          | key_len | ref  | rows | Extra                                                      |
+----+-------------+--------------+-------------+------------------------------+------------------------------+---------+------+------+------------------------------------------------------------+
|  1 | SIMPLE      | participan0_ | index_merge | index_conferenceid,index_pin | index_conferenceid,index_pin | 5,7     | NULL |    1 | Using intersect(index_conferenceid,index_pin); Using where |
+----+-------------+--------------+-------------+------------------------------+------------------------------+---------+------+------+------------------------------------------------------------+
1 row in set (0.01 sec)

-----------------------------我是分割线-----------------------

等到晚上约课较多的时间再次观察数据库的负载和慢查询日志

原文地址:http://blog.51cto.com/11784929/2091555

时间: 2024-08-04 22:22:36

MySQL通过添加索引解决线上数据库服务器压力大问题的相关文章

关于线上优化服务器视频笔记1-----快速部署线上服务器

线上linux服务器优化经验 2.线上服务器网络安全配置与系统登录安全配置 2.1.授权用户登录与sudo的设定 /etc/sudoers 文件 <user list> <host list> = <operator list> <tag list> <command list> 常见配置: Mbb ALL=(ALL) NOPASSWD:ALL 2.2.ssh登陆登陆经验 备份sshd配置文件 Cp /etc/ssh/sshd_config /e

听说”双11”是这么解决线上bug的

--Android线上热修复的使用与原理 预备知识和开发环境 Android NDK编程 AndFix浅析 Android线上热修复的原理大同小异.这里仅仅针对眼下最火的框架AndFix进行解说.主要从AndFix的使用.原理以及优缺点三个方面进行阐述. 使用方式 介绍 AndFix是一个AndroidApp的在线热补丁框架. 使用此框架,我们可以在不反复发版的情况下,在线改动App中的Bug.AndFix就是 "AndroidHot-Fix"的缩写. 就眼下来说,AndFix支持An

关于线上优化服务器视频笔记1-----调优线上服务器

linux服务器调优的经验 目录: 1.系统故障排除思路 重视报错信息 永远不要忘记日志文件 分析.定位.解决问题 2.影响linux性能的因素 服务器硬件因素 操作系统的相关因素 程序因素 3.系统性能优化工具 Cpu性能优化工具 vmstat,iosta,sar 内存性能检测工具 free,top,sar,pidstat 磁盘性能评估工具 iostat,sar 网络性能分析工具 ping,mtr,netstat 4.系统性能分析与标准 5.性能调优的思路与技巧分享 几个故障鼓励案例和性能优化

一次惨痛的搬砖总结--线上管理服务器迁移

为什么有这次迁移,主要是因为年前针对nagios的扩展做了很多研究测试,希望应用到生产环境中去,但是生产环境的nagios所在服务器是个集中的管理服务器,上面运行了很多开源软件,而且大部分都是前人安装部署,结构已经固化,坑太多已经无法扩展;其次管理服务器操作系统版本为Centos5.4,老实说现在很多软件在6x的系统上安装起来比较方便,默认环境基本都能满足各种开源软件的运行,而且线下测试都是在6.5的系统上测试的.最后是因为管理服务器太老了,害怕哪一天Down了,虽然配置文件每天都有备份,但是软

线上Linux服务器(2TB)分区参考方案

如果是线上服务器,假设它是 2TB 的 SATA 硬盘.8GB 内存,建议按如下方式进行分区: / 20480M /boot 128M swap 10240M /data 2016152M(即剩余的所有磁盘空间) 如果是 个人电脑 学习用,假设虚拟机的硬盘定为 10GB,分区参考如下: / 2048M /boot 128M swap 512M /data 7552M(即剩余的所有硬盘空间)

mysql线上数据库单表超过200G的处理

tbl_user_data占用了大量磁盘空间,数据表占用大概200G,索引30G左右,查询非常慢,影响业务的支持进行现在需要对它进行清理 临时解决方案是将原表重命名,新建一个和这个表相同的空表来替换(缺点是不能做到根治,隔一段时间以后需要重新处理) 根除的办法是重新设计,或者在客户端进行过滤避免过多垃圾数据进入系统 1.新建一个和现在表相同结构的表create table tbl_user_data_new like tbl_user_data 将主键的ID改为bigint并且unsigned无

mysql语句添加索引

1.PRIMARY  KEY(主键索引)        mysql>ALTER  TABLE  `table_name`  ADD  PRIMARY  KEY (  `column`  ) 2.UNIQUE(唯一索引)        mysql>ALTER  TABLE  `table_name`  ADD  UNIQUE (`column` ) 3.INDEX(普通索引)        mysql>ALTER  TABLE  `table_name`  ADD  INDEX index

给MySQL字段添加索引的操作

1.添加PRIMARY KEY(主键索引): ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 2.添加UNIQUE(唯一索引) : ALTER TABLE `table_name` ADD UNIQUE ( `column` ) 3.添加INDEX(普通索引) : ALTER TABLE `table_name` ADD INDEX index_name ( `column` ) 4.添加FULLTEXT(全文索引) : ALTER T

tomcat 远程debug配置,教你远程调试代码,解决线上故障

IDEA远程DEBUG Tomcat很简单,配置如下: 1.修改tomcat服务器配置 打开tomcat/bin/catalina.sh 在空白处添加如下参数 CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=xxx.xxx.xx.xx:60222,suspend=n,server=y" 说明:address为tomcat服务器ip地址,这里必须填上(如果是局域网ip,就填局域网ip,如果不填ip,可能启动会用12