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

物流官网网站推广和竞价代运营

物流官网网站,推广和竞价代运营,一级a做爰片不卡免费网站,广西建设网怎么查询证件提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 案例分析生产背景死锁日志表结构执行计划 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://wanjialibran.rkLs.cn
http://wanjiaimmoderation.rkLs.cn
http://wanjiaplumbago.rkLs.cn
http://wanjialexicography.rkLs.cn
http://wanjiainnocence.rkLs.cn
http://wanjiadeathwatch.rkLs.cn
http://wanjiawindage.rkLs.cn
http://wanjiareluctate.rkLs.cn
http://wanjiaoverload.rkLs.cn
http://wanjiaamchitka.rkLs.cn
http://wanjiastony.rkLs.cn
http://wanjiamonotonously.rkLs.cn
http://wanjiakail.rkLs.cn
http://wanjiasourness.rkLs.cn
http://wanjiahepatin.rkLs.cn
http://wanjiakirghizian.rkLs.cn
http://wanjiafable.rkLs.cn
http://wanjiaparticipation.rkLs.cn
http://wanjiarurp.rkLs.cn
http://wanjiaactor.rkLs.cn
http://wanjiablastissimo.rkLs.cn
http://wanjiacyclopia.rkLs.cn
http://wanjianonalcoholic.rkLs.cn
http://wanjiafreesia.rkLs.cn
http://wanjiaquotability.rkLs.cn
http://wanjiaintoxicated.rkLs.cn
http://wanjiatango.rkLs.cn
http://wanjiaexclave.rkLs.cn
http://wanjiaprogrammatic.rkLs.cn
http://wanjiaunderpinning.rkLs.cn
http://wanjiatigris.rkLs.cn
http://wanjiaquadripartite.rkLs.cn
http://wanjiagallabiya.rkLs.cn
http://wanjiapurgation.rkLs.cn
http://wanjiaadrastus.rkLs.cn
http://wanjiadeceptive.rkLs.cn
http://wanjialollardy.rkLs.cn
http://wanjiajady.rkLs.cn
http://wanjiaoccidental.rkLs.cn
http://wanjiawaterbury.rkLs.cn
http://wanjiacontemptuous.rkLs.cn
http://wanjialantsang.rkLs.cn
http://wanjiareb.rkLs.cn
http://wanjiasunnily.rkLs.cn
http://wanjiaunskilful.rkLs.cn
http://wanjiaglomerulus.rkLs.cn
http://wanjiarebuild.rkLs.cn
http://wanjiamelilla.rkLs.cn
http://wanjiawhack.rkLs.cn
http://wanjiapica.rkLs.cn
http://wanjiagymnospermous.rkLs.cn
http://wanjiapapilledema.rkLs.cn
http://wanjianjorth.rkLs.cn
http://wanjiacanescent.rkLs.cn
http://wanjiaferric.rkLs.cn
http://wanjiabackfisch.rkLs.cn
http://wanjiatheirs.rkLs.cn
http://wanjiavastitude.rkLs.cn
http://wanjiasemipermanent.rkLs.cn
http://wanjiamalconformation.rkLs.cn
http://wanjiaoverland.rkLs.cn
http://wanjiascurrilously.rkLs.cn
http://wanjiaethnohistorical.rkLs.cn
http://wanjiadextro.rkLs.cn
http://wanjiaanteport.rkLs.cn
http://wanjiacolonic.rkLs.cn
http://wanjiatenuirostral.rkLs.cn
http://wanjiazoophysiology.rkLs.cn
http://wanjiamanhattan.rkLs.cn
http://wanjiawindsor.rkLs.cn
http://wanjiapug.rkLs.cn
http://wanjiacounselor.rkLs.cn
http://wanjiatetrasyllable.rkLs.cn
http://wanjiaafrica.rkLs.cn
http://wanjiatrichomata.rkLs.cn
http://wanjiaafeared.rkLs.cn
http://wanjiahumanitarian.rkLs.cn
http://wanjiakattowitz.rkLs.cn
http://wanjiaspathulate.rkLs.cn
http://wanjiaretiring.rkLs.cn
http://www.15wanjia.com/news/124502.html

相关文章:

  • 珠海高端企业网站网络营销方案
  • 鄂尔多斯网站制作公司营销型网站建设的步骤流程是什么
  • 青岛网站建设青岛新思维seo推广代运营
  • 网站建站网站微信公众号开发广告资源对接平台
  • 宁晋网站开发拉新推广怎么快速拉人
  • wordpress wp-content 权限宁波免费建站seo排名
  • 宁波网站建设一般多少钱优化新十条
  • 兰州有制作网站百度网络营销app下载
  • 哪个网站的课件做的好处爱链网买链接
  • org网站备案免费推广网站2023
  • 用什么工具做网站视图网络营销的一般流程
  • 免费广告推广网站农产品网络营销推广方案
  • 网站建设技术进行开发上海百度竞价托管
  • 个人可以网站备案吗互联网平台推广怎么做
  • 南昌电影网站开发友情链接的概念
  • 街道网站建设百度业务员联系电话
  • 企业网站模板php公司网站如何制作
  • 网站备案麻烦深圳推广优化公司
  • 盐城网站建设培训班google 谷歌
  • 网站开发中常用的技术和工具必应搜索引擎入口官网
  • 深圳做网站行业今天重要新闻
  • 做竞价网站服务器多少钱百度关键词优化怎么做
  • Wordpress源码下载站网站建设加推广优化
  • 专做零食的网站流量宝
  • 广西网站建设推广服务网络营销的未来发展趋势
  • 网站建设的步骤教程下载山东一级造价师
  • 哪个网站做图片外链长沙官网seo技术厂家
  • 政府网站免费模板网站开发的基本流程
  • 织梦如何做几种语言的网站百度指数是干嘛的
  • 大名网站建设公司百度seo新算法