自增锁预分配ID

http://www.cnblogs.com/xpchild/p/3825309.html

mysql> show create table pp;

CREATE TABLE `pp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk 

mysql>  insert into pp(name) values(‘xx‘);
Query OK, 1 row affected (0.18 sec)

mysql> show create table pp;

CREATE TABLE `pp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk 

mysql> insert into pp(name) values(‘xx‘),(‘xx‘),(‘xx‘),(‘xx‘);
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> show create table pp;
CREATE TABLE `pp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=gbk

mysql> insert into pp(name) select name from pp;   预分配ID值
Query OK, 5 rows affected (0.20 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> show create table pp;
CREATE TABLE `pp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=gbk 

mysql> select * from pp;
+----+------+
| id | name |
+----+------+
|  1 | xx   |
|  2 | xx   |
|  3 | xx   |
|  4 | xx   |
|  5 | xx   |
|  6 | xx   |
|  7 | xx   |
|  8 | xx   |
|  9 | xx   |
| 10 | xx   |
+----+------+
10 rows in set (0.00 sec)

mysql>  insert into pp(name) values(‘xx‘);
Query OK, 1 row affected (0.18 sec)

mysql> select * from pp;
+----+------+
| id | name |
+----+------+
|  1 | xx   |
|  2 | xx   |
|  3 | xx   |
|  4 | xx   |
|  5 | xx   |
|  6 | xx   |
|  7 | xx   |
|  8 | xx   |
|  9 | xx   |
| 10 | xx   |
| 13 | xx   |
+----+------+
11 rows in set (0.00 sec)
mysql> insert into pp(name) select name from pp;
Query OK, 11 rows affected (0.16 sec)
Records: 11  Duplicates: 0  Warnings: 0

mysql> show create table pp;
CREATE TABLE `pp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=gbk |
+

mysql> select * from pp;
+----+------+
| id | name |
+----+------+
|  1 | xx   |
|  2 | xx   |
|  3 | xx   |
|  4 | xx   |
|  5 | xx   |
|  6 | xx   |
|  7 | xx   |
|  8 | xx   |
|  9 | xx   |
| 10 | xx   |
| 13 | xx   |
| 14 | xx   |
| 15 | xx   |
| 16 | xx   |
| 17 | xx   |
| 18 | xx   |
| 19 | xx   |
| 20 | xx   |
| 21 | xx   |
| 22 | xx   |
| 23 | xx   |
| 24 | xx   |
+----+------+
22 rows in set (0.01 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)
模拟崩溃

kill -9 `pidof mysqld`

MYSQLD再起动,再测ID

mysql> show create table pp;

CREATE TABLE `pp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=gbk

并没有持久化29,

select max(id) from pp   取初值 25    回退了
时间: 2024-08-27 16:29:57

自增锁预分配ID的相关文章

自增锁引发的悲剧

背景 先描述下故障吧 step0: 环境介绍 1. MySQL5.6.27 2. InnoDB 3. Centos 基本介绍完毕,应该跟大部分公司的实例一样 CREATETABLE`new_table`( `id` int(11) NOT NULL AUTO_INCREMENT, `x` varchar(200) DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5908151 DEFAULT CHARSET=utf8 C

自增锁

自增锁,在提交前释放,并发插入高 s,x等锁,在COMMIT扣释放,并发插入需要等待 不能回滚到前面的值 insert -like: simple-insert:插入前就能确定插入行数语句 bulk insert :插入前不确定插入行数的语句 replace ... select mixed-mode inserts:insert into t1(c1,c2) values(1,"a"),(null,"b"),(4,"c"),(null,&quo

MySQL AutoIncrement--自增锁模式

自增锁模式 在MYSQL 5.1.22版本前,自增列使用AUTO_INC Locking方式来实现,即采用一种特殊的表锁机制来保证并发插入下自增操作依然是串行操作,为提高插入效率,该锁会在插入语句完成后立即释放,而不是插入语句所在事务提交时释放.该设计并发性能太差,尤其在大批量数据在一条语句中插入时(INSERT SELECT ), 会导致该语句长时间持有这个“表锁”,从而阻塞其他事务的插入操作. 在MYSQL 5.1.22版本开始,InnoDB存储引使用一种轻量级互斥锁(Mutex)来控制自增

自增锁ID复用问题

mysql> select * from pp; +----+------+ | id | name | +----+------+ | 1 | xx | | 2 | xx | | 3 | xx | | 4 | xx | | 5 | xx | | 6 | xx | | 7 | xx | | 8 | xx | | 9 | xx | | 10 | xx | | 13 | xx | | 14 | xx | | 15 | xx | | 16 | xx | | 17 | xx | | 18 | xx |

使用mybatis插入自增主键ID的数据后返回自增的ID

在开发中碰到用户注册的功能需要用到用户ID,但是用户ID是数据库自增生成的,这种情况上网查询后使用下面的方式配置mybatis的insert语句可以解决: 1 <insert id="insert" keyProperty="id" useGeneratedKeys="true"? parameterType="com.demo.domain.User">? 2 insert into User_t(name,ag

mybatis插入操作时,返回自增主键id

mapper.xml 代码 <insert id="insert" parameterType="com.Student" > <selectKey keyProperty="id" resultType="long" order="AFTER"> select last_insert_id(); </selectKey> insert into student(id,,

Mybatis+Mysql插入数据库返回自增主键id值的三种方法

一.场景: 插入数据库的值需要立即得到返回的主键id进行下一步程序操作 二.解决方法: 第一种:使用通用mapper的插入方法 Mapper.insertSelective(record): 此方法:插入一条数据,只插入不为null的字段,不会影响有默认值的字段支持Oracle序列,UUID,类似Mysql的INDENTITY自动增长(自动回写)优先使用传入的参数值,参数值空时,才会使用序列.UUID,自动增长 controller的实际应用:使用方法id会直接将映射到参数的实体上使用时直接使用

无锁唯一id生成

首先关于唯一id生成,个人比较推崇美团的Leaf,具体介绍可见链接: https://tech.meituan.com/2017/04/21/mt-leaf.html 但这个框架未免有些太重了,笔者之前看到项目中生成方式是时间戳(精确到秒)+四位随机(数字+字母)的方式,看起来简单轻巧,但在高并发场景遇到了重复的情况,为了不改变原有的唯一id的结构,笔者把心思动在了多台机器对同一种业务,如何保证后四位随机数不重复上,毕竟34(0到9,大写字母排除I和O)^4大概有100W个数字,一秒并发超过10

生成数据库自增不重复ID的方法

namespace ConsoleApp1 { class Program { static void Main(string[] args) { var list = new HashSet<string>(101); for (int i = 0; i < 100; i++) { var task = Task.Factory.StartNew((m => { var id = McidGenerator.NewMcid(); list.Add(id); Console.Wri