复制代码

为懒人提供无限可能,生命不息,code不止

人类感性的情绪,让我们知难行难
我思故我在
日拱一卒,功不唐捐
  • 首页
  • 前端
  • 后台
  • 数据库
  • 运维
  • 资源下载
  • 实用工具
  • 接口文档工具
  • 登录
  • 注册

其它

【原创】database's transaction

作者: whooyun发表于: 2025-03-28 11:08

Method 1 (Early Transaction):
procedures:
start transaction
 select * from tempa;
 insert into tempb(id,name,create_time)values(1,"tom","2025-03-27");
commit transaction
Lock Duration: From INSERT to COMMIT

Method 2 (Late Transaction):
procedures:
  select * from tempa;
start transaction
  insert into tempb(id,name,create_time)values(1,"tom","2025-03-27");
commit transaction

Lock Duration: Also covers INSERT to COMMIT。  But Transaction Lifetime is  shorter than Method 1

Critical Insight:

Both methods hold locks for the same duration in the example, because:

Regular SELECT (without explicit locking clauses) doesn't acquire locks in MySQL's default(unless SERIALIZABLE  ) isolation level
True lock contention occurs only during the INSERT operation
============knowledge derivation====================
Practical Advice
Use SELECT ... FOR UPDATE if you need to lock rows early:
START TRANSACTION;
SELECT * FROM tempa FOR UPDATE; -- Explicit lock acquired here
INSERT INTO tempb ... ; 
COMMIT; 

→ In this scenario, Method 1 would indeed extend lock duration, making Method 2 more optimal."