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审核

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



相关内容

热门资讯

今日重大发现“暗宝可以开挂吗?... 今日重大发现“暗宝可以开挂吗?”(必胜开挂神器)您好,暗宝这个游戏其实有挂的,确实是有挂的,需要了解...
今日重磅消息“经典联盟牛牛开挂... 您好:经典联盟牛牛这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款...
玩家最新攻略“天天十三水是不是... 家人们!今天小编来为大家解答天天十三水透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买...
今日重磅消息“掌心麻将圈开挂神... 网上科普关于“掌心麻将圈有没有挂”话题很是火热,小编也是针对掌心麻将圈作*弊开挂的方法以及开挂对应的...
我来教教您“拱趴游戏辅助器?”... 有 亲,根据资深记者爆料拱趴游戏是可以开挂的,确实有挂(咨询软件无需打开...
【第一消息】“九点麻将开挂神器... 家人们!今天小编来为大家解答九点麻将透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里买很...
重磅消息“皇豪互娱牛牛真的有挂... 有 亲,根据资深记者爆料皇豪互娱牛牛是可以开挂的,确实有挂(咨询软件无需...
最新引进“蜀山四川麻将到底是不... 有 亲,根据资深记者爆料蜀山四川麻将是可以开挂的,确实有挂(咨询软件无需...
月租低流量多的电信卡,打游戏不... 办卡关注 微 信 公 众 号 :找卡乐园 这里是专为你打造的流量卡服务站,既能帮你揭开流量卡市场的各...
搏兔数智申请数据传输防泄露方法... 国家知识产权局信息显示,搏兔数智(深圳)企业家服务中心有限公司申请一项名为“一种数据传输防泄露的方法...