MySQL索引最左前缀原则导致系统瘫痪
admin
2023-04-21 01:42:52
0次

早上九点半左右 业务人员反映他们页面打开缓慢,后续页面出现502。

然后我这边收到报警 ,登录数据库服务器(4核cpu)查看  cpu 400% load 30左右

MySQL索引最左前缀原则导致系统瘫痪

进入到数据库中查看发现好多慢查询

MySQL索引最左前缀原则导致系统瘫痪


本以为这些慢查询是来自该系统每天的定时任务(该系统相当于一个olap系统,每天会进行批量的数据查询提取。)于是先crontab -e 把所有的定时任务都停掉。但是慢查询还是存在。

所有的慢查询都是同一个模板, 后来询问开发的同事昨天上线了新版本

SELECT (plan.due_amount + IF(plan.overdue_day > 0, (plan.due_amount * extend.supplement_penalty_rate /100), 0) - plan.reduce_amount) due_amount_total, plan.repay_status
                     FROM mostop_xiaodai_supplement_loan_repay_plan plan LEFT JOIN mostop_xiaodai_loan_info_extend extend ON extend.loan_id = plan.loan_id WHERE plan.base_plan_id = 11124546 AND plan.step_no = 2

查看执行表结构

mysql> show create table mostop_xiaodai_loan_info_extend\G
*************************** 1. row ***************************
       Table: mostop_xiaodai_loan_info_extend
Create Table: CREATE TABLE `mostop_xiaodai_loan_info_extend` (
  `id` bigint(20) unsigned NOT NULL COMMENT '编号',
  `agentid` int(10) unsigned NOT NULL COMMENT '渠道 ID',
  `loan_id` bigint(20) unsigned NOT NULL COMMENT '贷款编号',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` datetime NOT NULL COMMENT '更新时间',
  `total_rate` decimal(10,6) unsigned DEFAULT NULL COMMENT '总利率',
  `service_rate` decimal(10,6) unsigned DEFAULT NULL COMMENT '服务费率',
  `intrest_rate` decimal(10,6) unsigned DEFAULT NULL COMMENT '利息费率',
  `overdue_rate` decimal(10,6) unsigned DEFAULT NULL COMMENT '逾期总利率',
  `overdue_service_rate` decimal(10,6) unsigned DEFAULT NULL COMMENT '逾期服务费率',
  `penalty_rate` decimal(10,6) unsigned DEFAULT NULL COMMENT '违约金率',
  `is_split` tinyint(4) DEFAULT '0' COMMENT '息费打平,是否需要拆单',
  `desired_repay_type` varchar(9) DEFAULT NULL COMMENT '息费打平,理想还款方式',
  `desired_total_rate` decimal(10,6) DEFAULT NULL COMMENT '息费打平,理想总利率',
  `supplement_overdue_rate` decimal(10,6) DEFAULT NULL COMMENT '息费打平,白条订单逾期总利率',
  `supplement_penalty_rate` decimal(10,6) DEFAULT NULL COMMENT '息费打平,白条订单违约金率',
  `investor_rate` decimal(10,6) DEFAULT NULL COMMENT '投资人利率',
  `investor_repay_type` varchar(9) DEFAULT NULL COMMENT '投资人利率',
  PRIMARY KEY (`id`,`agentid`),
  UNIQUE KEY `agentid` (`agentid`,`loan_id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='贷款信息扩展表'

查看执行计划

mysql> explain SELECT (plan.due_amount + IF(plan.overdue_day > 0, (plan.due_amount * extend.supplement_penalty_rate /100), 0) 
| id | select_type | table  | partitions | type  | possible_keys   | key             | key_len | ref   | rows    | filtered | Extra       |
+----+-------------+--------+------------+-------+-----------------+-----------------+---------+-------+---------+----------+-------------+
|  1 | SIMPLE      | plan   | NULL       | const | idx_base_planid | idx_base_planid | 8       | const |       1 |   100.00 | NULL        |
|  1 | SIMPLE      | extend | NULL       | ALL   | NULL            | NULL            | NULL    | NULL  | 4690305 |   100.00 | Using where |
+----+-------------+--------+------------+-------+-----------------+-----------------+---------+-------+---------+----------+-------------+

虽然表中的联合索引上有loan_id这个列

 UNIQUE KEY `agentid` (`agentid`,`loan_id`)

但是根据索引的最左前缀原则,where条件中直接出了loan_id,复合索引出现了断开,所以索引失效。研发同学以为是可以用到表中的索引,没有审核就上线了,所以导致了全表扫描导致服务器的负载超高。

解决办法

添加索引

alter table  mostop_xiaodai_loan_info_extend add index IDX_loan_id (loan_id);

添加索引后执行计划

explain SELECT (plan.due_amount + IF(plan.overdue_day > 0, (plan.due_amount * extend.supplement_penalty_rate /100), 0) - plan.reduce_amount) due_amount_total, plan.repay_status
    ->                      FROM mostop_xiaodai_supplement_loan_repay_plan plan LEFT JOIN mostop_xiaodai_loan_info_extend extend ON extend.loan_id = plan.loan_id WHERE plan.base_plan_id = 11124546 AND plan.step_no = 2 ;
+----+-------------+--------+------------+-------+-----------------+-----------------+---------+-------+------+----------+-------------+
| id | select_type | table  | partitions | type  | possible_keys   | key             | key_len | ref   | rows | filtered | Extra       |
+----+-------------+--------+------------+-------+-----------------+-----------------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | plan   | NULL       | const | idx_base_planid | idx_base_planid | 8       | const |    1 |   100.00 | NULL        |
|  1 | SIMPLE      | extend | NULL       | ref   | IDX_loan_id     | IDX_loan_id     | 8       | const |    1 |   100.00 | Using where |
+----+-------------+--------+------------+-------+-----------------+-----------------+---------+-------+------+----------+-------------+

服务器负载立马回复正常


通过本次事故

上线前进行SQL审核

应用和数据库单独部署在不同服务器上



相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...