温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL中使用if not exists需要注意什么

发布时间:2021-11-06 11:04:17 来源:亿速云 阅读:1560 作者:小新 栏目:MySQL数据库

小编给大家分享一下MySQL中使用if not exists需要注意什么,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!



环境MySQL 5.6.14
事务隔离级别 读提交
事务的开启和结束由JAVA程序控制.

上次报死锁的过程,抽象如下

  1. delimitr $$

  2. CREATE PROCEDURE `test_proc`(

  3.     pid int

  4. )

  5. begin

  6.     if not exists (select * from t where id=pid) then

  7.         insert into t(id) values(pid);

  8.     end if;

  9.     

  10.     update t set total=total+1 where id=pid;

  11. end $$

  12. delimiter ; 


死锁原因已经明白了,就是并发情况下,Insert遇到排它锁,则尝试加共享锁。
在最后Update的时候,两个持有共享锁的连接,都尝试申请排它锁,则导致了死锁.

但是问题是...怎么会走到了最后一行的Update语句?
另外两个连接,不是应该在Insert语句时报 Duplicate entry 'xx' for key 'PRIMARY'错误吗?

问题应该出在这种结构里
if not exists (select * from t where id=pid) then
    xxx
end if;

使用 if not exists 模式,真心要注意啊.在这种结构里出现的异常,不会报错,而是直接跳出IF判断,继续执行!!

实验准备


  1. CREATE TABLE `t` (

  2.   `id` int(11) NOT NULL,

  3.   `total` int(11) NOT NULL DEFAULT '0',

  4.   PRIMARY KEY (`id`)

  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


  6. truncate table t;

  7. drop procedure if exists test_proc;

  8. delimiter $$

  9. CREATE PROCEDURE `test_proc`(

  10.     pid int,

  11.     ptotal int

  12. )

  13. begin

  14.     if not exists (select * from t where id=pid) then

  15.         insert into t(id,total) value(pid,ptotal);

  16.         update t set total=ptotal+1 where id=pid;

  17.     end if;

  18.     select ptotal+1;    

  19. end $$

  20. delimiter ;


打开三个客户端,分别执行过程
MySQL中使用if not exists需要注意什么

第一个客户端执行,并没有提交.
第二,第三客户端处于阻塞状态.
等第一个客户端提交,

第二个客户端返回 201
第三个客户端返回 301
且没有任何的报错信息.


三个客户端都提交之后,查看T表信息
只有一个记录,id为1,total为101

也就是说,第二个,第三个客户端,在得到主键冲突的异常后,没有报错,没有继续执行IF块内剩下的语句,而是直接跳出了IF块,继续执行IF块外的语句!!

该报错的地方不报错,在大段的存储过程中,导致死锁还是小问题,就怕引起数据的错乱,而不自知.

针对这种情况,如果有主键或者唯一约束,我觉得干脆改为如下的方式.
delimiter $$
CREATE PROCEDURE `test_proc`(
    pid int,
    ptotal int
)
begin
    insert into t(id,total) value(pid,ptotal);
    update t set total=ptotal+1 where id=pid;
    select ptotal+1;
end $$
delimiter ;

看完了这篇文章,相信你对“MySQL中使用if not exists需要注意什么”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI