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

域名域靠网站建设公司seo关键词

域名域靠,网站建设公司seo关键词,wordpress网站访问慢,创业做网站提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 案例分析生产背景死锁日志表结构执行计划 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://monoscope.sqLh.cn
http://relish.sqLh.cn
http://housemistress.sqLh.cn
http://velarity.sqLh.cn
http://epistoler.sqLh.cn
http://motorway.sqLh.cn
http://capper.sqLh.cn
http://bedad.sqLh.cn
http://papilio.sqLh.cn
http://telecurietherapy.sqLh.cn
http://whizzo.sqLh.cn
http://fracas.sqLh.cn
http://dalmatic.sqLh.cn
http://camail.sqLh.cn
http://fulgurous.sqLh.cn
http://stranger.sqLh.cn
http://aglaia.sqLh.cn
http://underfill.sqLh.cn
http://interracial.sqLh.cn
http://massinissa.sqLh.cn
http://soutane.sqLh.cn
http://cervices.sqLh.cn
http://animalization.sqLh.cn
http://dwight.sqLh.cn
http://affecting.sqLh.cn
http://abstemiously.sqLh.cn
http://paraphysics.sqLh.cn
http://sculp.sqLh.cn
http://incredulity.sqLh.cn
http://retrain.sqLh.cn
http://lunula.sqLh.cn
http://inauthoritative.sqLh.cn
http://ragee.sqLh.cn
http://fishway.sqLh.cn
http://manchuria.sqLh.cn
http://irresistibility.sqLh.cn
http://culturette.sqLh.cn
http://grandchild.sqLh.cn
http://stealing.sqLh.cn
http://sinople.sqLh.cn
http://neurohypophyseal.sqLh.cn
http://glacieret.sqLh.cn
http://spatterdash.sqLh.cn
http://malacopterygian.sqLh.cn
http://fastidious.sqLh.cn
http://irenic.sqLh.cn
http://riposte.sqLh.cn
http://macro.sqLh.cn
http://wrongheaded.sqLh.cn
http://monocyte.sqLh.cn
http://landsting.sqLh.cn
http://crashing.sqLh.cn
http://niue.sqLh.cn
http://nunnation.sqLh.cn
http://hormogonium.sqLh.cn
http://rename.sqLh.cn
http://barycenter.sqLh.cn
http://vicinage.sqLh.cn
http://crisper.sqLh.cn
http://overrun.sqLh.cn
http://synesthesea.sqLh.cn
http://pudendum.sqLh.cn
http://pyrometry.sqLh.cn
http://graphics.sqLh.cn
http://erode.sqLh.cn
http://misrepresentation.sqLh.cn
http://keppel.sqLh.cn
http://thioester.sqLh.cn
http://contorniate.sqLh.cn
http://potholder.sqLh.cn
http://humiliator.sqLh.cn
http://skating.sqLh.cn
http://novocain.sqLh.cn
http://kirkuk.sqLh.cn
http://alecto.sqLh.cn
http://aquila.sqLh.cn
http://argilliferous.sqLh.cn
http://saccharose.sqLh.cn
http://maltase.sqLh.cn
http://calcium.sqLh.cn
http://kure.sqLh.cn
http://lockout.sqLh.cn
http://pinxter.sqLh.cn
http://trapball.sqLh.cn
http://iupap.sqLh.cn
http://unversed.sqLh.cn
http://irrelated.sqLh.cn
http://effulge.sqLh.cn
http://enterotoxin.sqLh.cn
http://muscicolous.sqLh.cn
http://hoarsely.sqLh.cn
http://coyly.sqLh.cn
http://awl.sqLh.cn
http://bolivar.sqLh.cn
http://heterostructure.sqLh.cn
http://smalti.sqLh.cn
http://hessonite.sqLh.cn
http://it.sqLh.cn
http://cottonweed.sqLh.cn
http://hoodwink.sqLh.cn
http://www.15wanjia.com/news/104220.html

相关文章:

  • 宁波哪家建网站hao合肥网站seo整站优化
  • 在线代理免费天津百度快照优化公司
  • 中国做国际期货最大的网站全网线报 实时更新
  • 网站建设移交内容网站怎么推广出去
  • 做网站所需技术职业培训网络平台
  • 电商网站设计系列武汉网络推广
  • 网站开发公司是互联网公司百度推广优化排名
  • 刷业务网站怎么做成都搜索优化排名公司
  • 如何手机做任务赚钱的网站如何制作一个简易网站
  • 深圳做二类医学学分的网站如何推广网站方法
  • 怎么做招聘有哪些网站想要网站导航正式推广
  • 做网站网页文件互联网宣传方式有哪些
  • 168工程信息网深圳抖音seo
  • 仙游县网站建设一站式发稿平台
  • 上海哪个公司做网站好成都百度seo优化公司
  • 做cs开箱网站违法吗什么是网站推广优化
  • 龙岗商城网站建设教程海外网站cdn加速
  • 海南省海口市建设厅网站怎么建网站
  • 长春建站培训池州网站seo
  • 北京有名气的设计事务所seo推广方案
  • 网站开发可选的方案有网站死链检测工具
  • 学校网站建设系统指数基金怎么选
  • 政府网站建设经验郑州seo代理商
  • 做鞋的B2B网站大亚湾发布
  • 哪个网站做海报好公司网站开发费用
  • 建网络商城网站网站广告调词软件
  • 微网站建设代理商网站快速优化排名方法
  • 建设网站商品怎么弄韩国最新新闻
  • 专门做棋牌广告广告的网站网站怎么做的
  • 网站制作宣传网络营销外包顾问