在我们在数据库系统中实行update句子时,是锁住表或是行?使我们用MySQL上的事例立即测试一下。

一是自然环境提前准备。

1.建立新表。

create table test_update( id BIGINT not null primary key COMMENT '外键约束ID,雪花算法转化成', name VARCHAR(100) COMMENT '名字', user_no VARCHAR(20) COMMENT '客户序号');

2.插进两根数据信息。

insert into test_update(id,name,user_no)values(1,'张三','001');insert into test_update(id,name,user_no)values(2,'李四','002');

第二,逐渐检测。

情景1:沒有数据库索引。

开启事务管理sql1: updatetest _ updateset name = '张三1 '在其中user _ no在(' 001 ');不必递交事务管理。

开启事务管理SQL 2:updatetest _ updateset name = ' Li si 1 ',在其中user _ no在(' 002 ');递交事务管理

大家发觉,当sql1不递交事务时,sql2被堵塞,直至sql1的事务管理被递交,sql2才会取得成功实行。

引言:update句子锁住表而不创建数据库索引。

情景2:数据库索引。

加上数据库索引alter table test _ update将数据库索引index _ name (user_no)加上到user _ no;

开启事务管理sql1: updatetest _ updateset name = '张三1 '在其中user _ no在(' 001 ');不必递交事务管理。

开启事务管理SQL 2:updatetest _ updateset name = ' Li si 1 ',在其中user _ no在(' 002 ');递交事务管理

大家看到在sql1不递交事务管理的情形下,sql2是取得成功实行的,即sql2不依赖于sql1的事务管理递交。

概述:在检索的情形下,update句子锁住行。

情景3:数据库索引,可是in是一个错综复杂的查看。

在上面的事例中,它有确认的值,可是在它被查看,如下边的事例(user_no早已被数据库索引)。

开启事务管理sql1: update test _ update set name = '张三1 '在其中user _ no在(从other _ table中挑选user _ no,在其中id = 1);不必递交事务管理。

开启事务管理SQL 2:update test _ update set name = ' Li si 1 '在其中user _ no在(从别的_ table中挑选user _ no,在其中id = 2);递交事务管理

大家发觉,当sql1不递交事务时,sql2被阻拦。仅有当sql1的事务管理被递交时,sql2才会取得成功实行,这与情景1的結果同样。

引言:在检索的情形下,中有一个不确定性的值,update句子锁住该表。

评论(0条)

刀客源码 游客评论