zoukankan      html  css  js  c++  java
  • Mysql 会导致锁表的语法

    最近再找一些Mysql锁表原因,整理出来一部分sql语句会锁表的,方便查阅,整理的不是很全,都是工作中碰到的,会持续更新

    笔者能力有限,如果有不正确的,或者不到位的地方,还请大家指出来,方便你我,方便大家。

    此测试环境

    Mysql 5.5 基于innodb 引擎

    1. insert into  table1 values select  … from table2 ….  


    此种方法,会锁table2

     
    1. delete table1  from table1 inner join table2  on table1.id=table2.id  …  


    此种方法,会锁table2

    1. update tabel1,table2 set table1.name=’feie’ where table1.id=table2.id  


    此种方法,会锁table2

    1. update tabel1,table2 set table1.name=’feie’ where table1.id=table2.id and table1.id=1;  


    此种方法,会锁table2.id=1的记录

    -----------------------------------------第二次编辑分割线--------------------------------------

    假设kid 是表table 的 一个索引字段 且值不唯一
    1.如果kid 有多个值为12的记录那么:
    update table  set name=’feie’ where kid=12;  
    会锁表
    2.如果kid有唯一的值为1的记录那么:
    update table  set name=’feie’ where kid=1;  
    不会锁
    总结:用索引字段做为条件进行修改时, 是否表锁的取决于这个索引字段能否确定记录唯一,当索引值对应记录不唯一,会进行锁表,相反则行锁。

    如果有两个delete

    kid1 与 kid2是索引字段
    语句1 delete from table where  kid1=1 and kid2=2;
    语句2 delete from table where  kid1=1 and kid2=3;
    这样的两个delete 是不会锁表的
    语句1 delete from table where  kid1=1 and kid2=2;
    语句2 delete from table where  kid1=1 ;
    这样的两个delete 会锁表
    总结:同一个表,如果进行删除操作时,尽量让删除条件统一,否则会相互影响造成锁表

  • 相关阅读:
    Eclipse 远程调试
    大数据处理方法bloom filter
    sicily 1259 Sum of Consecutive Primes
    sicily 1240. Faulty Odometer
    sicily 1152 简单马周游 深度优先搜索及回溯算法
    sicily 1050 深度优先搜索解题
    sicily 1024 邻接矩阵与深度优先搜索解题
    sicily 1156 二叉树的遍历 前序遍历,递归,集合操作
    sicily 1443 队列基本操作
    sicily 1006 team rankings 枚举解题
  • 原文地址:https://www.cnblogs.com/mafeng/p/9171750.html
Copyright © 2011-2022 走看看