线上MySQL数据库高负载的解决思路--再次论程序应用索引的重要性

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 前言:过去的笔记整理而得,未免丢失,发布个人博客。[2012年的资料笔记] 场景:数据库的负载飙升,CPU高达99%。查看进程。通过猜测推理,定位了一些select语句 363478427 | apps_read     | 192.
前言:过去的笔记整理而得,未免丢失,发布个人博客。[2012年的资料笔记]
场景:数据库的负载飙升,CPU高达99%。

查看进程。通过猜测推理,定位了一些select语句
363478427 | apps_read     | 192.168.1.113:48945 | apps       | Query       |       1 | Sending data                                                   | select * from category_doc_info
 where (doc_title = '???')                                            | 
| 363478430 | apps_read     | 192.168.1.113:48948 | apps       | Query       |       1 | Sending data                                                   | select * from category_doc_info
 where (doc_title = '??????')                                         | 
| 363478434 | apps_read     | 192.168.1.113:48952 | apps       | Query       |       1 | Sending data                                                   | select * from category_doc_info
 where (doc_title = '?????????')                                      | 
| 363478437 | apps_read     | 192.168.1.113:48955 | apps       | Query       |       1 | Sending data                                                   | select * from category_doc_info
 where (doc_title = '????????')                                       | 
| 363478462 | apps_read     | 192.168.1.113:48957 | apps       | Query       |       1 | Sending data                                                   | select * from category_doc_info
 where (doc_title = '???????')                                        | 
| 363478500 | apps_read     | 192.168.1.113:48960 | apps       | Query       |       1 | Sending data                                                   | select * from category_doc_info
 where (doc_title = '????')                                           | 
| 363478511 | apps_read     | 192.168.1.113:48963 | apps       | Query       |       0 | Sending data                                                   | select * from category_doc_info
 where (doc_title = '??????')                                         | 
| 363478518 | apps_read     | 192.168.1.113:48964 | apps       | Query       |       1 | Sending data                                                   | select * from category_doc_info
 where (doc_title = 'T2??')                                           | 
| 363478535 | apps_read     | 192.168.1.113:48965 | apps       | Query       |       0 | Sending data                                                   | select * from category_doc_info
 where (doc_title = '???')                                            | 
| 363478540 | apps_read     | 192.168.1.113:48968 | apps       | Query       |       1 | Sending data                                                   | select * from category_doc_info
 where (doc_title = '??')                                             | 
| 363478613 | apps_read     | 192.168.1.113:48971 | apps       | Query       |       0 | Sending data                                                   | select * from category_doc_info
 where (doc_title = '???')                                            | 
| 363478630 | apps_read     | 192.168.1.113:48975 | apps       | Query       |       0 | Sending data                                                   | select *

查看此表的数据库表结构如下:
---------------------------------------------------------------------------------------------------------------------------------------------------------
| category_doc_info | CREATE TABLE `category_doc_info` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `category_id` int(10) unsigned NOT NULL COMMENT
  `doc_title` varchar(255) NOT NULL COMMENT 
  `category_show` tinyint(1) unsigned NOT NULL COMMENT
  `category_Coordinate` tinyint(1) unsigned NOT NULL default '2' 
  `category_order` tinyint(1) unsigned NOT NULL default '0' 
  PRIMARY KEY  (`id`),
  UNIQUE KEY `INDEX_SEARCH` (`category_id`,`doc_title`),
) ENGINE=InnoDB AUTO_INCREMENT=343502 DEFAULT CHARSET=utf8 | 
---------------------------------------------------------------------------------------------------------------------------------------------------------
发现只有一个组合索引。但是完全没有用到。
现实场景:都是where (doc_title = '???')  的语句

查看具体的一条SQL语句的执行计划,如下:
mysql> explain  select * from category_doc_info where (doc_title = '独出新裁');
+----+-------------+-------------------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows   | Extra       |
+----+-------------+-------------------+------+---------------+------+---------+------+--------+-------------+
|  1 | SIMPLE      | category_doc_info | ALL  | NULL          | NULL | NULL    | NULL |  232717 | Using where | 
+----+-------------+-------------------+------+---------------+------+---------+------+--------+-------------+

可以发现都是全表查询,并且是高并发的访问上述语句。

经过分析,修改生产环境的表结构,如下:
| category_doc_info | CREATE TABLE `category_doc_info` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `category_id` int(10) unsigned NOT NULL,
  `doc_title` varchar(255) NOT NULL',
  `category_show` tinyint(1) unsigned NOT NULL,
  `category_Coordinate` tinyint(1) unsigned NOT NULL default '2',
  `category_order` tinyint(1) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `INDEX_SEARCH` (`category_id`,`doc_title`),     
  KEY `idx_category_title` (`doc_title`)                            //新添加的表索引
) ENGINE=InnoDB AUTO_INCREMENT=343502 DEFAULT CHARSET=utf8 |

让上述的程序应用走索引,数据库的负载恢复正常,性能恢复正常。 

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4天前
|
存储 安全 搜索推荐
酒店管理系统的数据库的应用以及选择
酒店管理系统数据库关乎运营效率和服务质量。数据库用于数据存储、管理、分析及客户关系管理,确保房态与预订精准。选择时重视性能稳定性、数据安全、易用性、可扩展性和成本效益。合适的数据库能提升酒店运营效率并优化客户体验。
16 2
|
6天前
|
关系型数据库 MySQL 索引
mysql 分析5语句的优化--索引添加删除
mysql 分析5语句的优化--索引添加删除
7 0
|
6天前
|
数据库 索引
数据库索引的作用和优点缺点
数据库索引的作用和优点缺点
11 0
|
12天前
|
SQL 关系型数据库 MySQL
轻松入门MySQL:保障数据完整性,MySQL事务在进销存管理系统中的应用(12)
轻松入门MySQL:保障数据完整性,MySQL事务在进销存管理系统中的应用(12)
|
12天前
|
存储 关系型数据库 MySQL
轻松入门MySQL:优化进销存管理,掌握MySQL索引,提升系统效率(11)
轻松入门MySQL:优化进销存管理,掌握MySQL索引,提升系统效率(11)
|
18天前
|
存储 自然语言处理 关系型数据库
ElasticSearch索引 和MySQL索引那个更高效实用那个更合适
ElasticSearch索引 和MySQL索引那个更高效实用那个更合适
33 0
|
18天前
|
SQL 存储 关系型数据库
MySQL not exists 真的不走索引么
MySQL not exists 真的不走索引么
23 0
|
22天前
|
SQL 存储 关系型数据库
对线面试官 - 如何理解MySQL的索引覆盖和索引下推
索引下推是MySQL 5.6引入的优化,允许部分WHERE条件在索引中处理,减少回表次数。例如,对于索引(zipcode, lastname, firstname),查询`WHERE zipcode='95054' AND lastname LIKE '%etrunia%'`时,索引下推先过滤zipcode,然后在索引中应用lastname条件,降低回表需求。索引下推可在EXPLAIN的`Using index condition`中看到。
对线面试官 - 如何理解MySQL的索引覆盖和索引下推
|
1月前
|
存储 监控 关系型数据库
数据库核心术语解析与应用
数据库核心术语解析与应用
61 0
|
1月前
|
监控 关系型数据库 MySQL
MySQL创建索引的注意事项
在数据库设计和优化中,索引的合理使用是提高查询性能和加速数据检索的关键因素之一。通过选择适当的列、了解数据分布、定期维护和监控索引性能,我们能够最大程度地发挥索引的优势,提高数据库的效率和响应速度。
29 0

热门文章

最新文章