首页 > 数据库 >MySQL index and dead lock

MySQL index and dead lock

时间:2024-03-04 13:58:35浏览次数:33  
标签:index use Thread lock dead select

MySQL 

 

 

 

 

 

 

 

Analyze:

step 1. Thread(test1) use "select for update" lock common age index in range (10,30]

step 2. Thread(test2) use "select for update" lock common age index in range (40,60]

step 3. Thread(test2) try to insert a row which age-index = 25, but the range has been locked by Thread(test1), so wait

step 4. Thread(test1) try to insert a row which age-index = 55, but the range has been locked by Thread(test2), so wait

step 5. mysql find dead lock, rollback Thread(test2), then Thread(test1) can insert and commit.

 

Usually, most dead locks happen when using non-unique-index and using gap lock and next-key lock, which default enable in REPEATABLE-READ.

 

When execute DML(like "select for update", "insert", "update", "delete"):

  if hit unique-lock or primary lock

      mysql will use record/row lock.

  if hit non-unique-lock and transaction_isolation=REPEATABLE-READ

      mysql will use gap and next-key lock.

  if doesn't hit any index

      mysql will use  table lock.

 

 

Solution for deadlock

1. make transaction short

2. for those DML which will add lock, add lock in same order

3. replace "select ... for update" to "select" if possible, read by MVCC, it won't add lock

4. use READ-COMMITTED if possible  

5. open dead-lock checking, which will auto rollback some transaction when find deadlock

 

标签:index,use,Thread,lock,dead,select
From: https://www.cnblogs.com/huainanyin/p/18051642

相关文章

  • windows 磁盘锁 bitlocker关闭
      要关闭BitLocker等待激活的状态,可以按照以下步骤操作:打开命令提示符(CMD),以管理员身份运行。使用`manage-bde-off`命令关闭BitLocker加密。例如,关闭E盘的BitLocker加密命令为`manage-bde-offE:`。运行命令后,系统会对指定的磁盘进行解密工作,可能需要等......
  • 多线程系列(十一) -浅析并发读写锁StampedLock
    一、摘要在上一篇文章中,我们讲到了使用ReadWriteLock可以解决多线程同时读,但只有一个线程能写的问题。如果继续深入的分析ReadWriteLock,从锁的角度分析,会发现它有一个潜在的问题:如果有线程正在读数据,写线程准备修改数据的时候,需要等待读线程释放锁后才能获取写锁,简单的说就是,读......
  • App Lock on iPhone
    Withtheincreasingseverityofcybersecuritythreatsonmobiledevices,awarenessofpersonalprivacyprotectionisalsogrowing.Considerthisscenario:ifyoutemporarilylendyourphonetosomeoneelseanditremainsunlocked,wouldyoualsoworryabo......
  • SpringBoot定时任务:使用shedlock解决SpringBoot分布式定时任务
    第一步:引入shedlock包maven中pom文件添加如下配置:<dependency><groupId>net.javacrumbs.shedlock</groupId><artifactId>shedlock-spring</artifactId><version>4.33.0</version>使用其他版本</dependency>第二步:添加shedlock-p......
  • 为什么会有幽灵空白元素,display:inline/inline-block会出现空隙的原因
    display:inline-block样式会在一些情况下产生间隙,这通常是由于元素之间的空白符或换行符所致。具体来说,以下情况可能导致display:inline-block元素之间出现间隙:HTML源代码中的换行符和空格:如果display:inline-block元素之间有换行符或空格,浏览器会将这些空白符解释为......
  • JUC系列之(七)Lock同步锁
    Lock同步锁用于解决多线程安全问题的方式:同步代码块,synchronized实现,隐式锁同步方法,synchronized实现,隐式锁同步锁Lock:jdk1.5以后注:是一个显示锁,需要通过lock()方法上锁,必须通过unlock()方法进行释放锁(一定要将unlock()放到finally中,保证一定会释放锁),更加灵活示例......
  • 【每周一读】Automating Hyperparameter Tuning with LlamaIndex
    原文......
  • CF1209G2 Into Blocks (hard version) 题解
    Description给你\(n\),\(q\),\(n\)表示序列长度,\(q\)表示操作次数。我们需要达成这么一个目标状态:如果存在\(x\)这个元素,那么必须满足所有\(x\)元素都必须在序列中连续。然后你可以进行这么一种操作,将所有的\(x\)元素的变为任意你指定的\(y\)元素,并且花费\(cnt[x......
  • 解决uniapp项目中使用vant Weapp图标组件报错问题(Module build failed from ./node_mo
    解决uniapp项目中使用vantWeapp图标组件报错问题(Modulebuildfailedfrom./node_modules/postcss-loader/src/index):https://blog.csdn.net/it_cgq/article/details/111991644?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170909210216800225582870%2522%252C%252......
  • 面试官:说一下红锁RedLock的实现原理?
    RedLock是一种分布式锁的实现算法,由Redis的作者SalvatoreSanfilippo(也称为Antirez)提出,主要用于解决在分布式系统中实现可靠锁的问题。在Redis单独节点的基础上,RedLock使用了多个独立的Redis实例(通常建议是奇数个,比如5个),共同协作来提供更强健的分布式锁服务。RedLo......