RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
MySQL为什么慎用ifnotexists写法

这篇文章将为大家详细讲解有关MySQL为什么慎用if not exists写法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、成都做网站、西区网络推广、重庆小程序开发、西区网络营销、西区企业策划、西区品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供西区建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com



版本 5.6.14

环境
CREATE TABLE `t1` (
  `id` int(11) NOT NULL,
  `batchid` int(11) DEFAULT NULL,
  `state` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

INSERT INTO `t1` (`id`,`batchid`,`state`) VALUES (1,1,1);
INSERT INTO `t1` (`id`,`batchid`,`state`) VALUES (2,1,1);


drop procedure if exists p;
delimiter $$
CREATE PROCEDURE `p`()
begin
    if not exists (select * from t1 where id=1) then
        select 'id=1 not exists';
    end if;
end $$
delimiter ; 

按照如下时间轴执行 ,窗口二居然被阻塞了.


MySQL为什么慎用if not exists写法

查看 show engine innodb status;
发现如下信息:

---TRANSACTION 15252974, ACTIVE 2 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 376, 1 row lock(s)
MySQL thread id 38, OS thread handle 0x1d88, query id 4663 localhost 127.0.0.1 root statistics
select * from t1 where id=1 
LIMIT 0, 1000
for update
------- TRX HAS BEEN WAITING 2 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 2344 page no 3 n bits 72 index `PRIMARY` of table `mvbox`.`t1` trx id 15252974 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 4; hex 80000001; asc     ;;
 1: len 6; hex 000000e8bdc4; asc       ;;
 2: len 7; hex 030003c0100437; asc       7;;
 3: len 4; hex 80000001; asc     ;;
 4: len 4; hex 80000001; asc     ;;


------------------
---TRANSACTION 15252973, ACTIVE 15 sec
2 lock struct(s), heap size 376, 1 row lock(s)
MySQL thread id 36, OS thread handle 0x35c, query id 4615 localhost 127.0.0.1 root cleaning up

过程中 if not exists 的查询居然上了锁..

关于“MySQL为什么慎用if not exists写法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


当前名称:MySQL为什么慎用ifnotexists写法
文章位置:http://scyingshan.cn/article/jspdes.html