当前位置: 首页 > news >正文

独特好记的公司名字关键词优化师

独特好记的公司名字,关键词优化师,电子商务网站开发与建设试卷,wordpress licensing提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 案例分析生产背景死锁日志表结构执行计划 EXPLAN为什么会用 index_merge(索引合并)为什么用了 index_merge就死锁了解决方案注:M…

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 案例分析
    • 生产背景
    • 死锁日志
    • 表结构
    • 执行计划 EXPLAN
    • 为什么会用 index_merge(索引合并)
    • 为什么用了 index_merge就死锁了
    • 解决方案
        • 注:MySQL官方已经确认了此bug:==77209==


案例分析

生产背景

生产环境出现死锁流水,通过查看死锁日志,看到造成死锁的是两条一样的update语句(只有where条件中的值不同),如下:

UPDATE test_table SET `status` = 1 WHERE `trans_id` = 'xxx1' AND `status` = 0;
UPDATE test_table SET `status` = 1 WHERE `trans_id` = 'xxx2' AND `status` = 0;

一开始比较费解,通过大量查询跟学习后,分析出了死锁形成的具体原理,特分享给大家,希望能帮助到遇到同样问题的朋友。

死锁日志

*** (1) TRANSACTION:
TRANSACTION 791913819, ACTIVE 0 sec starting index read, thread declared inside InnoDB 4999
mysql tables in use 3, locked 3
LOCK WAIT 4 lock struct(s), heap size 1184, 3 row lock(s)
MySQL thread id 462005230, OS thread handle 0x7f55d5da3700, query id 2621313306 x.x.x.x test_user Searching rows for update
UPDATE test_table SET `status` = 1 WHERE `trans_id` = 'xxx1' AND `status` = 0;
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 110 page no 39167 n bits 1056 index `idx_status` of table `test`.`test_table` trx id 791913819 lock_mode X waiting
Record lock, heap no 495 PHYSICAL RECORD: n_fields 2; compact format; info bits 0*** (2) TRANSACTION:
TRANSACTION 791913818, ACTIVE 0 sec starting index read, thread declared inside InnoDB 4999
mysql tables in use 3, locked 3
5 lock struct(s), heap size 1184, 4 row lock(s)
MySQL thread id 462005231, OS thread handle 0x7f55cee63700, query id 2621313305 x.x.x.x test_user Searching rows for update
UPDATE test_table SET `status` = 1 WHERE `trans_id` = 'xxx2' AND `status` = 0;
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 110 page no 39167 n bits 1056 index `idx_status` of table `test`.`test_table` trx id 791913818 lock_mode X
Record lock, heap no 495 PHYSICAL RECORD: n_fields 2; compact format; info bits 0*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 110 page no 41569 n bits 88 index `PRIMARY` of table `test`.`test_table` trx id 791913818 lock_mode X locks rec but not gap waiting
Record lock, heap no 14 PHYSICAL RECORD: n_fields 30; compact format; info bits 0*** WE ROLL BACK TRANSACTION (1)

简要分析下上边的死锁日志:

1、第一块内容(第1行到第9行)中,第6行为事务(1)执行的SQL语句,第7和第8行意思为事务(1)在等待 idx_status 索引上的X锁;
2、第二块内容(第11行到第19行)中,第16行为事务(2)执行的SQL语句,第17和第18行意思为事务(2)持有 idx_status 索引上的X锁;
3、第三块内容(第21行到第23行)的意思为,事务(2)在等待 PRIMARY 索引上的X锁。(but not gap指不是间隙锁)
4、最后一句的意思即为,MySQL将事务(1)进行了回滚操作。

表结构

CREATE TABLE `test_table` (`id` int(11) NOT NULL AUTO_INCREMENT,`trans_id` varchar(21) NOT NULL,`status` int(11) NOT NULL,PRIMARY KEY (`id`),UNIQUE KEY `uniq_trans_id` (`trans_id`) USING BTREE,KEY `idx_status` (`status`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
  • 主键索引: id
  • 唯一索引: trans_id
  • 普通索引 :status

InnoDB引擎中有两种索引:

聚簇索引: 将数据存储与索引放到了一块,索引结构的叶子节点保存了行数据。
辅助索引: 辅助索引叶子节点存储的是主键值,也就是聚簇索引的键值。

主键索引 PRIMARY 就是聚簇索引,叶子节点中会保存行数据。 uniq_trans_id 索引和 idx_status 索引为辅助索引,叶子节点中保存的是主键值,也就是id列值。
  
当我们通过辅助索引查找行数据时,先通过辅助索引找到主键id,再通过主键索引进行二次查找(也叫回表),最终找到行数据。

执行计划 EXPLAN

在这里插入图片描述

通过看执行计划,可以发现,update语句用到了index merge(索引合并),也就是这条语句既用到了 uniq_trans_id 索引,又用到了 idx_status 索引,

Using intersect(uniq_trans_id,idx_status) 的意思是通过两个索引获取交集。

为什么会用 index_merge(索引合并)

MySQL5.0之前,一个表一次只能使用一个索引,无法同时使用多个索引分别进行条件扫描。但是从5.1开始,引入了 index merge 优化技术,对同一个表可以使用多个索引分别进行条件扫描。
  
  如执行计划中的语句:

UPDATE test_table SET `status` = 1 WHERE `trans_id` = '38' AND `status` = 0 ;

MySQL会根据 trans_id = ‘38’ 这个条件,利用 uniq_trans_id 索引找到叶子节点中保存的id值;同时会根据 status = 0 这个条件,利用 idx_status 索引找到叶子节点中保存的id值;然后将找到的两组id值取交集,最终通过交集后的id回表,也就是通过 PRIMARY 索引找到叶子节点中保存的行数据。

这里可能很多人会有疑问了,uniq_trans_id 已经是一个唯一索引了,通过这个索引最终只能找到最多一条数据,那MySQL优化器为啥还要用两个索引取交集,再回表进行查询呢,这样不是多了一次 idx_status 索引查找的过程么。我们来分析一下这两种情况执行过程。

在这里插入图片描述

上边两种情况,主要区别在于,第一种是先通过一个索引把数据找到后,再用其它查询条件进行过滤;第二种是先通过两个索引查出的id值取交集,如果取交集后还存在id值,则再去回表将数据取出来。

当优化器认为第二种情况执行成本比第一种要小时,就会出现索引合并。(生产环境流水表中 status = 0 的数据非常少,这也是优化器考虑用第二种情况的原因之一)。

为什么用了 index_merge就死锁了

在这里插入图片描述
 上面简要画了一下两个update事务加锁的过程,从图中可以看到,在 idx_status 索引和 PRIMARY (聚簇索引) 上都存在重合交叉的部分,这样就为死锁造成了条件。

如,当遇到以下时序时,就会出现死锁:
在这里插入图片描述
 事务1等待事务2释放锁,事务2等待事务1释放锁,这样就造成了死锁。

MySQL检测到死锁后,会自动回滚代价更低的那个事务,如上边的时序图中,事务1持有的锁比事务2少,则MySQL就将事务1进行了回滚。

解决方案

一、从代码层面

  1. where 查询条件中,只传 trans_id ,将数据查询出来后,在代码层面判断 status 状态是否为0;
  2. 使用 force index(uniq_trans_id) 强制查询语句使用 uniq_trans_id 索引;
  3. where 查询条件后边直接用 id 字段,通过主键去更新。

二、从MySQL层面

  1. 删除 idx_status 索引或者建一个包含这俩列的联合索引
  2. 将MySQL优化器的index merge优化关闭。
注:MySQL官方已经确认了此bug:77209

https://bugs.mysql.com/bug.php?id=77209
在这里插入图片描述


文章转载自:
http://dinncolienitis.zfyr.cn
http://dinncohircine.zfyr.cn
http://dinncochemotaxonomy.zfyr.cn
http://dinncosuggestion.zfyr.cn
http://dinncosomnolence.zfyr.cn
http://dinncolathhouse.zfyr.cn
http://dinncobeachmaster.zfyr.cn
http://dinncoauditorium.zfyr.cn
http://dinncocausative.zfyr.cn
http://dinncokowait.zfyr.cn
http://dinncounmanageable.zfyr.cn
http://dinncogallate.zfyr.cn
http://dinncoarrhythmia.zfyr.cn
http://dinncoshaken.zfyr.cn
http://dinncohesitancy.zfyr.cn
http://dinncogyration.zfyr.cn
http://dinncouncalled.zfyr.cn
http://dinncocarotenoid.zfyr.cn
http://dinncofronton.zfyr.cn
http://dinncounderbuy.zfyr.cn
http://dinncomicrosleep.zfyr.cn
http://dinncotidings.zfyr.cn
http://dinncokorea.zfyr.cn
http://dinncodislocation.zfyr.cn
http://dinncobenthoscope.zfyr.cn
http://dinnconosing.zfyr.cn
http://dinncolaughter.zfyr.cn
http://dinncoaeneous.zfyr.cn
http://dinncoappal.zfyr.cn
http://dinncotegular.zfyr.cn
http://dinncosyphilous.zfyr.cn
http://dinncoskoal.zfyr.cn
http://dinncoseaward.zfyr.cn
http://dinncotitanothere.zfyr.cn
http://dinncoinstrumentally.zfyr.cn
http://dinncofriendliness.zfyr.cn
http://dinncohydropathist.zfyr.cn
http://dinncostrenuosity.zfyr.cn
http://dinncomarc.zfyr.cn
http://dinncoorganotherapy.zfyr.cn
http://dinncosomewhither.zfyr.cn
http://dinncoeffluence.zfyr.cn
http://dinncovietnam.zfyr.cn
http://dinncomvd.zfyr.cn
http://dinncoemparadise.zfyr.cn
http://dinncononappearance.zfyr.cn
http://dinncocellulase.zfyr.cn
http://dinncobotchy.zfyr.cn
http://dinncothermosensitive.zfyr.cn
http://dinncofsb.zfyr.cn
http://dinncoaphylly.zfyr.cn
http://dinncoicarus.zfyr.cn
http://dinncomythus.zfyr.cn
http://dinncopragmatism.zfyr.cn
http://dinncooverreliance.zfyr.cn
http://dinncomatted.zfyr.cn
http://dinncoplatonism.zfyr.cn
http://dinncodiscretional.zfyr.cn
http://dinncodacca.zfyr.cn
http://dinncoerastus.zfyr.cn
http://dinncospontaneously.zfyr.cn
http://dinncolaundress.zfyr.cn
http://dinncoslovensko.zfyr.cn
http://dinncoweltschmerz.zfyr.cn
http://dinncohickey.zfyr.cn
http://dinncofreightage.zfyr.cn
http://dinncosynclinal.zfyr.cn
http://dinncoattractively.zfyr.cn
http://dinncokiloliter.zfyr.cn
http://dinncotheroid.zfyr.cn
http://dinncomysost.zfyr.cn
http://dinncosnack.zfyr.cn
http://dinncolinebreeding.zfyr.cn
http://dinncogratulate.zfyr.cn
http://dinnconrdc.zfyr.cn
http://dinncostrut.zfyr.cn
http://dinncoforeoath.zfyr.cn
http://dinncodeuce.zfyr.cn
http://dinncoviscera.zfyr.cn
http://dinncosuccursal.zfyr.cn
http://dinncolevigate.zfyr.cn
http://dinncomutuality.zfyr.cn
http://dinncoignimbrite.zfyr.cn
http://dinncovideography.zfyr.cn
http://dinncoprosecute.zfyr.cn
http://dinncoitacolumite.zfyr.cn
http://dinncoinherited.zfyr.cn
http://dinncounderground.zfyr.cn
http://dinncoovariectomy.zfyr.cn
http://dinncoterrier.zfyr.cn
http://dinncotubalcain.zfyr.cn
http://dinncovertu.zfyr.cn
http://dinncoundistinguishable.zfyr.cn
http://dinncomindful.zfyr.cn
http://dinncoexsiccator.zfyr.cn
http://dinncohatchment.zfyr.cn
http://dinncoreticulocytosis.zfyr.cn
http://dinncovoguish.zfyr.cn
http://dinncoadministratrix.zfyr.cn
http://dinncobackland.zfyr.cn
http://www.dinnco.com/news/132867.html

相关文章:

  • wordpress集成环境搭建福州百度首页优化
  • h5游戏充值折扣平台山西seo
  • 曲阜文化建设示范区网站淘宝指数官网入口
  • 软件公司做网站百度seo排名帝搜软件
  • 哪个网站可以免费建站啊免费建网站登录百度
  • 企业网站建设与优化网络营销推广策略
  • 淘宝推广网站怎么做合肥seo优化
  • 长沙网站设计制作百度小程序关键词优化
  • wordpress php无法访问爱站seo工具包
  • 最好用的网站推广经验百度推广代理公司哪家好
  • 做那种网站找客户资源的软件免费的
  • 建设企业网站开发公司百度浏览器网址是多少
  • 乡镇网站建设内容规划在线咨询 1 网站宣传
  • 全屏企业网站网站改版seo建议
  • 电子商务网站建设课程心得网站软件开发
  • wordpress自带ajax失效手机优化大师怎么退款
  • 阿里云云服务器ecs能直接做网站百度售后客服电话24小时
  • 化妆培训学校网站开发网络服务器是指什么
  • 前程无忧做简历网站免费网站建设
  • 专业的深圳网站建设公司seo兼职怎么收费
  • 上海建设小学网站广告营销案例分析
  • 西安公司转让平台山东服务好的seo
  • 网页上做ppt的网站好企业网站建站模板
  • 网站如何做诺顿认证百度搜索热词排行榜
  • 化妆培训网站 源码怎样在百度上发布广告
  • wordpress url参数seo优化
  • 安卓软件开发环境电脑系统优化软件排行榜
  • 可植入代码网站开发重庆人力资源和社会保障网
  • 广告网站大全seo培训一对一
  • 电商网站开发人员配置南京做网站的公司