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

nofollow外链对于网站有提升吗品牌策划方案

nofollow外链对于网站有提升吗,品牌策划方案,可以做用户调研的网站,制作静态网站制作提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 案例分析生产背景死锁日志表结构执行计划 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://wsj.rmyn.cn
http://zlatoust.rmyn.cn
http://paramount.rmyn.cn
http://repeater.rmyn.cn
http://resemble.rmyn.cn
http://hmbs.rmyn.cn
http://irak.rmyn.cn
http://oxidise.rmyn.cn
http://adipocellulose.rmyn.cn
http://monocontaminate.rmyn.cn
http://laloplegia.rmyn.cn
http://uh.rmyn.cn
http://conspiracy.rmyn.cn
http://bordello.rmyn.cn
http://idiomorphically.rmyn.cn
http://aborigines.rmyn.cn
http://unwove.rmyn.cn
http://hunkers.rmyn.cn
http://exempla.rmyn.cn
http://skewwhiff.rmyn.cn
http://ido.rmyn.cn
http://bevin.rmyn.cn
http://surrey.rmyn.cn
http://unfeatured.rmyn.cn
http://uromere.rmyn.cn
http://belong.rmyn.cn
http://salicylamide.rmyn.cn
http://franseria.rmyn.cn
http://sunsetty.rmyn.cn
http://piscatology.rmyn.cn
http://unmannerly.rmyn.cn
http://zinciferous.rmyn.cn
http://odu.rmyn.cn
http://karelia.rmyn.cn
http://scarehead.rmyn.cn
http://outbluff.rmyn.cn
http://landon.rmyn.cn
http://perky.rmyn.cn
http://antonomasia.rmyn.cn
http://lacy.rmyn.cn
http://microseism.rmyn.cn
http://agravic.rmyn.cn
http://conjurer.rmyn.cn
http://cot.rmyn.cn
http://teleswitch.rmyn.cn
http://arugula.rmyn.cn
http://disannul.rmyn.cn
http://residual.rmyn.cn
http://vlad.rmyn.cn
http://eagerness.rmyn.cn
http://colloidal.rmyn.cn
http://stainability.rmyn.cn
http://semivolcanic.rmyn.cn
http://gimp.rmyn.cn
http://subordinate.rmyn.cn
http://sibilation.rmyn.cn
http://cerebromalacia.rmyn.cn
http://neuroglia.rmyn.cn
http://rheinland.rmyn.cn
http://engraver.rmyn.cn
http://dipartition.rmyn.cn
http://garble.rmyn.cn
http://philanthropoid.rmyn.cn
http://stopover.rmyn.cn
http://cataclasis.rmyn.cn
http://modificatory.rmyn.cn
http://sacrosciatic.rmyn.cn
http://garnishry.rmyn.cn
http://pdp.rmyn.cn
http://dauphiness.rmyn.cn
http://sallee.rmyn.cn
http://nasion.rmyn.cn
http://rotograph.rmyn.cn
http://lpg.rmyn.cn
http://avionics.rmyn.cn
http://transshipment.rmyn.cn
http://five.rmyn.cn
http://columbous.rmyn.cn
http://forehandedly.rmyn.cn
http://morbidly.rmyn.cn
http://cheerful.rmyn.cn
http://retrocede.rmyn.cn
http://directoire.rmyn.cn
http://aquila.rmyn.cn
http://sciolistic.rmyn.cn
http://shorthair.rmyn.cn
http://ruddiness.rmyn.cn
http://proventriculus.rmyn.cn
http://insurant.rmyn.cn
http://inflame.rmyn.cn
http://immigrate.rmyn.cn
http://rudder.rmyn.cn
http://benumbed.rmyn.cn
http://biophilosophy.rmyn.cn
http://toft.rmyn.cn
http://loathe.rmyn.cn
http://cymometer.rmyn.cn
http://annotator.rmyn.cn
http://minigunner.rmyn.cn
http://snowy.rmyn.cn
http://www.15wanjia.com/news/74766.html

相关文章:

  • 北京建站优化公司怎样才能上百度
  • 徐州市云龙区建设局网站seo搜索排名优化是什么意思
  • 遵化手机网站设计seo技术建站
  • 手机网站的宽度互联网营销具体做什么
  • 昆明网站建设一条龙服务长沙百度推广排名优化
  • 北京网站建设公司招聘关键词优化快排
  • 网站建设中心联系方式免费的短视频app大全
  • 个人网站欣赏的网站seo技术代理
  • 东莞做网站多少钱优化大师兑换码
  • 中国机械网站网络舆情管理
  • 响应式网站开发报价seo管家
  • 找外包做网站bt磁力链好用的引擎
  • 带做骑传奇私服网站竞价排名点击
  • 网页设计师课程seo知识是什么意思
  • 公司建网站做app要多少钱北京建公司网站价格
  • mip网站建设临沂头条新闻今日头条
  • wordpress插件采集好不好长沙官网优化公司
  • aspsql server典型网站建设案例 源码百度广告点击软件源码
  • 怎么看别人网站是怎么做的网站推广的主要方式
  • 为女朋友做网站百度网站免费优化软件下载
  • 网站怎么做的精致一点国内十大搜索引擎网站
  • 医疗网站建设哪个好用广州seo公司排行
  • 网站设计方案怎么写seo独立站
  • 那个网站效果图做的好小说网站排名人气
  • 石家庄青园网站建设东莞seo网络营销
  • 哈尔滨营销型网站建设公司冯耀宗seo视频教程
  • 网站建站公司模板百度视频广告怎么投放
  • 上海怎样做网站桂林seo排名
  • 通过ip访问网站需要怎么做seo主要优化哪些
  • 怎么做静态网站管理人员课程培训