MySql 主键一般用自增ID还是UUID

Posted 书香水墨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySql 主键一般用自增ID还是UUID相关的知识,希望对你有一定的参考价值。

一、主键一般用自增ID还是UUID?

(1)自增ID:

使用自增ID的好处:

  • 字段长度较 UUID 会小很多。
  • 数据库自动编号,按顺序存放,利于检索
  • 无需担心主键重复问题

使用自增ID的缺点:

  • 因为是自增,在某些业务场景下,容易被其他人查到业务量。
  • 发生数据迁移时,或者表合并时会非常麻烦
  • 在高并发的场景下,竞争自增锁会降低数据库的吞吐能力

(2)UUID:通用唯一标识码,UUID是基于当前时间、计数器和硬件标识等数据计算生成的。

使用UUID的优点:

  • 唯一标识,不用考虑重复问题,在数据拆分、合并时也能达到全局的唯一性。
  • 可以在应用层生成,提高数据库的吞吐能力。
  • 无需担心业务量泄露的问题。

使用UUID的缺点:

  • 因为UUID是随机生成的,所以会发生随机IO,影响插入速度,并且会造成硬盘的使用率较低。
  • UUID占用空间较大,建立的索引越多,造成的影响越大。
  • UUID之间比较大小较自增ID慢不少,影响查询速度。

一般情况下,mysql推荐使用自增ID,因为在MySQL的 InnoDB 存储引擎中,主键索引是聚簇索引,主键索引的B+树的叶子节点按照顺序存储了主键值及数据,如果主键索引是自增ID,只需要按顺序往后排列即可,如果是UUID,ID是随机生成的,在数据插入时会造成大量的数据移动,产生大量的内存碎片,造成插入性能的下降。

如何获取自增id

参考技术A 去"O"的业务支撑的时候,遇到一个开发询问如何获取到MySQL 的自增id,这里做一个总结,我们可以通过四种方式来获取MySQL 的自增id。
一 last_insert_id()
官方介绍如下:
“The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions. ”

last_insert_id() 单个会话中最近一次执行的insert语句时表的自增id的值。不受其他的会话插入影响。

如果是新建的表,它的值为0
root@rac3 [yangyi]> create table t2(id int not null auto_increment primary key ,col varchar(10));
Query OK, 0 rows affected (0.11 sec)
root@rac3 [yangyi]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 0 |
+------------------+
1 row in set (0.01 sec)

如果向表中插入的记录,id自增,它的值为max(id),如果指定插入的id ,它的值为上一次插入的最大值。
root@rac3 [yangyi]> insert into t3 values(null,'sss');
Query OK, 1 row affected (0.00 sec)

root@rac3 [yangyi]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
root@rac3 [yangyi]> insert into t3 values(2,'sss');
Query OK, 1 row affected (0.00 sec)
root@rac3 [yangyi]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)

使用select last_insert_id()时要注意,当一次插入多条记录时,只是获得第一次插入的id值,务必注意!
root@rac3 [yangyi]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 3 |
+------------------+
1 row in set (0.01 sec)
root@rac3 [yangyi]> insert into t1 values(null,'sss'),(null,'dd'),(null,'aaa');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
root@rac3 [yangyi]> select * from t1;
+----+------+
| id | col |
+----+------+
| 1 | sss |
| 2 | sss |
| 3 | sss |
| 4 | sss |
| 5 | dd |
| 6 | aaa |
+----+------+
6 rows in set (0.00 sec)

root@rac3 [yangyi]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 4 |
+------------------+
1 row in set (0.00 sec)

last_insert_id 是与表无关的,如果向表a插入数据后,再向表b插入数据,last_insert_id 返回表b的Id值!
root@rac3 [yangyi]> insert into t2 values(null,'sss');
Query OK, 1 row affected (0.00 sec)
root@rac3 [yangyi]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 11 |
+------------------+
1 row in set (0.00 sec)
root@rac3 [yangyi]> insert into t1 values(null,'sss');
Query OK, 1 row affected (0.00 sec)
root@rac3 [yangyi]> select @@identity;
+------------+
| @@identity |
+------------+
| 3 |
+------------+
1 row in set (0.00 sec)

二 使用@@identity

@@identity是表示的是最近一次向具有identity属性(即自增列)的表插入数据时对应的自增列的值,是系统定义的全局变量。
其特性last_insert_id 一样.

三 使用max(id)
select max(id) from tab; 返回的是tab 的最大id值。
使用max(id) 只能针对单个表获取最大的id,而且高并发情况下,必须加上'X' 锁才能获取会话当前的最大id,显然加锁会严重影响并发性能。

四 查看表结构

root@rac3 [yangyi]> show table status like 't3' \G
*************************** 1. row ***************************
Name: t3
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 2
Avg_row_length: 8192
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 3
Create_time: 2014-05-06 20:36:00
Update_time: NULL
Check_time: NULL
Collation: utf8_bin
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)

root@rac3 [yangyi]> show create table t3 \G
*************************** 1. row ***************************
Table: t3
Create Table: CREATE TABLE `t3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`col` varchar(10) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
1 row in set (0.00 sec)

五 总结
我们知道MySQL 使用auto_increment 属性来实现自增id,这点与oracle 的sequence 不同,auto_increment 是基于表的而非全局。从oracle 到mysql 的dba 要注意改变对sequence 使用方式的转变。本回答被提问者和网友采纳

以上是关于MySql 主键一般用自增ID还是UUID的主要内容,如果未能解决你的问题,请参考以下文章

如何获取自增id

阿里二面差点败在这道题:MySQL自增主键为何不是连续的呢?

Python操作MySQL获取插入数据的主键id

MySQL 使用自增ID主键和UUID 作为主键的优劣比较详细过程(从百万到千万表记录测试)

MySQL 使用自增ID主键和UUID 作为主键的优劣比較具体过程(从百万到千万表记录測试)

mysql表设计的一些面试题