MySQL通过添加索引解决线上数据库服务器压力大问题
2018-03-27 11:32
811 查看
昨天3月26号线上应用反馈:晚上19:30-19:43以及 20:13到20:21两个时间段所有人包括他们自己建的教室都进不去,cla***oom B套无法登陆,主页无法访问,已有超过10个老师和学生反馈进不去教室的问题
通过监控查看26号00:00到27号11:00之间的监控,查看出现问题时数据库服务器的cpu使用率,负载,内存使用,swap剩余量等状况,发现数据库当时压力特别大,
数据库慢查询日志中出现很多慢SQL,
查看慢查询日志,发现一个SQL在慢查询日志中频繁出现切执行时间较长
# User@Host: 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' ); # User@Host: 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' ); # User@Host: 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' ); # User@Host: 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' ); # User@Host: 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' ); # User@Host: 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
查看该表的索引
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 | 2360697 | NULL | NULL | | BTREE | | | | participant | 1 | index_conferenceid | 1 | conferenceid | A | 199 | NULL | NULL | YES | BTREE | | | | participant | 1 | index_pin | 1 | pin | A | 199 | NULL | NULL | YES | BTREE | | | +-------------+------------+--------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 3 rows in set (0.00 sec)
再次查看该SQL的执行计划,不再走全表扫描,而是走了index_merge,执行时间也大大缩短,
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)
-----------------------------我是分割线-----------------------
等到晚上约课较多的时间再次观察数据库的负载和慢查询日志一切正常
通过此次事故:
① 及时优化慢查询
② 打开参数 log_queries_not_using_indexes 及时发现没有走索引的SQL
③可以采用SQL审核-自主上线平台,解放人力
http://blog.51cto.com/hcymysql/2053798#comment
相关文章推荐
- mysql-proxy解决只能内网访问线上数据库问题
- mysql-proxy解决只能内网访问线上数据库问题
- 解决asp.net 用 mySql 数据库 添加数据时中文为乱码问题
- 完美解决MySQL通过localhost无法连接数据库的问题
- VC 通过ODBC连接sql2005 在 sys.servers 中找不到服务器的解决方法,请执行 sp_addlinkedserver 将该服务器添加到 sysservers问题详解
- C#中DataGridView绑定了DataTable后,通过代码修改DataGridView中的数据,总有一行(被修改过并被用户选中的行集合中索引为0的行)不能被UpDate回数据库的问题和解决办法
- 如何解决 JMeter 通过 JDBC 访问 Oracle 和 MySQL 的问题 (留言中有 Test Plan 实例下载)
- java 连接 mysql 数据库"javax.servlet.ServletException: org.gjt.mm.mysql.Driver"是什么问题,怎样解决?
- .Net中通过DataSet直接更新数据库相关问题和解决方法
- 解决不能通过mysql.sock连接MySQL问题的办法
- 解决JBOSS服务器无法通过IP访问的问题
- SQL Server无法通过IP地址注册服务器及连接问题的解决
- 解决mysql+hibernate在j2ee框架下的数据库中文乱码问题
- 转 windows 环境下 mysql 数据库的升级以及 出现问题的解决 Communication link failure: Bad handshake
- MySQL5.0中文问题及JDBC数据库连接和JSP汉字编码问题解决方法总结
- 如何解决 JMeter 通过 JDBC 访问 Oracle 和 MySQL 的问题 (留言中有 Test Plan 实例下载)
- 解决JBOSS服务器无法通过IP访问的问题
- 在控制器中添加过滤器,解决中文由jsp到action进而到数据库传输过程中出现的乱码问题(Struts)
- 解决 web服务器部署常见问题,server application unavailable 和 程序无法连接数据库 的问题(asp.net 2.0 + oracle9i + winXP)