在一个应用中,有一条mysql function 中使用了多条SQL文实现 自增 如
update sequence set current_value=current_value+increment where name=item_name;
select current_value from sequence where name=item_name.
不去分析为什么不使用自增而使用这样的设计,单从这样的实现本身不是原子,如要保证唯一性还需要加锁。
简单的做法是:
UPDATE sequence SET current_value = @value:=current_value+increment WHERE name = item_name;
RETURN @value;
探讨在应用中使用多条SQL语句实现自增操作的非原子性,并提出简单有效的原子性优化方法,确保数据唯一性和一致性。
1350





