MySQL 表的自增字段 预判功能innodb_autoinc_lock_mode

在 5.1.22之后,innodb对于可以 预判行数的insert语句,innodb使用一个轻量级的互斥量,而不是表级锁。
但有可能会造成自增字段的值不连续。


注:如果使用新的自增互斥方式,对于replication应该避免使用INSERT ... ON DUPLICATE KEY UPDATE语句。

设置新自增互斥方式:通过配置选项:innodb_autoinc_lock_mode:调整锁策略:

innodb_autoinc_lock_mode = 0 (“traditional” lock mode:全部使用表锁)
innodb_autoinc_lock_mode = 1  (默认)(“consecutive” lock mode:可预判行数时使用新方式,不可时使用表锁) 
innodb_autoinc_lock_mode = 2 (“interleaved” lock mode:全部使用新方式,不安全,不适合replication)


##创建自增字段

方法1、创建:
mysql> create table c(id int auto_increment,name varchar(20),primary key(id));
Query OK, 0 rows affected (0.52 sec)

mysql> desc c;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.03 sec)

方法2、修改:
mysql> create table cc (id int,name varchar(20));
Query OK, 0 rows affected (0.42 sec)

mysql> alter table cc change id id int primary key auto_increment;
Query OK, 0 rows affected (1.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc cc;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.11 sec)

mysql> insert into cc(id,name) values(1,'a'),(NULL,'b'),(NULL,'c'),(5,'d');

mysql> select * from cc;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|   2 | b    |
|   3 | c    |
|  5 | d    |
+----+------+
4 rows in set (0.00 sec)

注:只有int类型且为primary key 才可以使用auto_increment.


##对存在记录的表的列修改为自增列
mysql> create table ccc (id int,name varchar(20));
Query OK, 0 rows affected (0.27 sec)

mysql> insert into ccc(id,name) values(1,'a'),(NULL,'b'),(NULL,'c'),(5,'d');
Query OK, 4 rows affected (0.53 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from ccc;
+------+------+
| id   | name |
+------+------+
|    1 | a    |
| NULL | b    |
| NULL | c    |
|    5 | d    |
+------+------+
4 rows in set (0.00 sec)

mysql>  alter table ccc change id id int primary key auto_increment;
Query OK, 4 rows affected (1.04 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> desc ccc;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

mysql> insert into ccc(id,name) values(1,'a'),(NULL,'b'),(NULL,'c'),(5,'d');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> insert into ccc(id,name) values(6,'aa'),(NULL,'ab'),(NULL,'ac'),(10,'ad')
;
Query OK, 4 rows affected (0.07 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> select * from ccc;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  5 | d    |
|  6 | aa   |
|  7 | ab   |
|  8 | ac   |
| 10 | ad   |
+----+------+
8 rows in set (0.00 sec)

mysql>



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27126919/viewspace-2125137/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/27126919/viewspace-2125137/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值