自增锁预分配ID

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

mysql> show create table pp;

CREATE TABLE `pp` (
`id` int() NOT NULL AUTO_INCREMENT,
`name` varchar() DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk mysql> insert into pp(name) values('xx');
Query OK, row affected (0.18 sec) mysql> show create table pp; CREATE TABLE `pp` (
`id` int() NOT NULL AUTO_INCREMENT,
`name` varchar() 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, rows affected (0.01 sec)
Records: Duplicates: Warnings: mysql> show create table pp;
CREATE TABLE `pp` (
`id` int() NOT NULL AUTO_INCREMENT,
`name` varchar() 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, rows affected (0.20 sec)
Records: Duplicates: Warnings: mysql> show create table pp;
CREATE TABLE `pp` (
`id` int() NOT NULL AUTO_INCREMENT,
`name` varchar() DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=gbk mysql> select * from pp;
+----+------+
| id | name |
+----+------+
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
+----+------+
rows in set (0.00 sec) mysql> insert into pp(name) values('xx');
Query OK, row affected (0.18 sec) mysql> select * from pp;
+----+------+
| id | name |
+----+------+
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
+----+------+
rows in set (0.00 sec)
mysql> insert into pp(name) select name from pp;
Query OK, rows affected (0.16 sec)
Records: Duplicates: Warnings: mysql> show create table pp;
CREATE TABLE `pp` (
`id` int() NOT NULL AUTO_INCREMENT,
`name` varchar() DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=gbk |
+ mysql> select * from pp;
+----+------+
| id | name |
+----+------+
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
| | xx |
+----+------+
rows in set (0.01 sec) mysql> commit;
Query OK, rows affected (0.00 sec)
模拟崩溃

kill - `pidof mysqld`

MYSQLD再起动,再测ID

mysql> show create table pp;

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

并没有持久化29,

select max(id) from pp   取初值 25    回退了
上一篇:2016BUAA校赛决赛


下一篇:拖图UI和纯代码UI