mysql分布式思维(七)- 锁机制优化

一、锁机制简介
  1.行级锁(row-level)
     操作时,锁记录,颗粒度最小,并发好,锁资源消耗大。
     主要针对innodb存储引擎
  2.表级锁 (table-level)
     锁表,并发差,锁资源消耗小。
     表级锁主要针对myisam存储引擎
  3.页级锁 (page-level)
     介于两者之间

二、锁的分析
1.表级锁分析
    读锁定,写锁定
    四个队列  Current read-lock queue(lock --->read)
                     Pending read-lock queue(lock --->read_wait)
       current write-lock queue(lock--->write)
       pending write-lock queue(lock --->write_wait)
   锁定类型还有很多
         参见图

  案例分析
           read锁/read local锁等
    write锁/write_allow_read等
2. 行级锁分析
     主要是innodb存储引擎支持。可以直接通过事务来支持。

     注意:无索引行锁升级为表锁的情况
                间隙锁带来的插入问题。
  使用共同索引不同数据也会阻塞
  也会有死锁的情况:
     session a:
      update t1 set id= 110 where id = 11;
    此时
      session b
        update t2 set id = 210 where id = 21

     然后:
         session a --->update t2 set id = 2100 where id = 21
         session b--->update t1 set id = 1000 where id = 11;
    

   

猜你喜欢

转载自394498036.iteye.com/blog/2291649
今日推荐