mysql主键uuid、uuid_short和int自增对比

数据库主键性能对比:

     名称        存储长度      生成方式
1.  uuid       32+4     uuid()函数

2.  uuid20      20        UUID_SHORT()函数

3.    bigint自增    20        auto_increment

测试表:id_int()、

-- uuid测试表
CREATE TABLE `id_uuid` (
  `id` varchar(50) NOT NULL,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- uuid20测试表 【注:id是bigint unsigned 】
CREATE TABLE `id_uuid20` (
  `id` bigint(20) unsigned NOT NULL,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--int自增测试表(我本地使用的是int 可以改成bigint)
CREATE TABLE `id_int` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

初始化100w条数据的存储过程:

-- 初始化int自增
CREATE  PROCEDURE `int_init`(in  s int)
BEGIN
    DECLARE ct INT;
    SET ct=1;
    while ct<s DO
        insert into id_int( name) VALUES ( concat(‘aaa‘,ct));
        set ct=ct+1;
    END WHILE;
END

-- 初始化uuid20
CREATE  PROCEDURE `uuid20_init`( in  s int)
BEGIN
    DECLARE ct INT;
    SET ct=1;
    while ct<s DO
        insert into id_uuid20(id ,name) VALUES (uuid_short(), concat(‘aaa‘,ct));
        set ct=ct+1;
    END WHILE;
END

-- 初始化uuid

CREATE PROCEDURE `uuid_init`(in  s int)
BEGIN
    DECLARE ct INT;
    SET ct=1;
    while ct<s DO
        insert into id_uuid (id, name) VALUES (uuid(), concat(‘aaa‘,ct));
        set ct=ct+1;
    END WHILE;
END

-- 分别调用
call int_init(1000000);

call uuid20_init(1000000);

call uuid_init(1000000);

数据插入过程能发现int自增的插入速度明显高出另外两个,uuid()函数调用肯定没有自增快。不过相较于插入,我更关注查询的性能对比

count:  长整形的效率明显高于字符型的

mysql> select count(*) from id_int;
+----------+
| count(*) |
+----------+
|  2412382 |
+----------+
1 row in set (0.65 sec)

mysql> select count(*) from id_uuid;
+----------+
| count(*) |
+----------+
|  1005356 |
+----------+
1 row in set (6.11 sec)

mysql> select count(*) from id_uuid20;
+----------+
| count(*) |
+----------+
|  1000003 |
+----------+
1 row in set (0.82 sec)

基于主键查询:差别不大

mysql> select * from id_int where id = 555555;
+--------+-----------+
| id     | name      |
+--------+-----------+
| 555555 | aaa555538 |
+--------+-----------+
1 row in set (0.08 sec)

mysql> select * from id_uuid where id =‘e27267ba-a897-11e6-b4a0-742f68b93fab‘;
+--------------------------------------+-----------+
| id                                   | name      |
+--------------------------------------+-----------+
| e27267ba-a897-11e6-b4a0-742f68b93fab | aaa550010 |
+--------------------------------------+-----------+
1 row in set (0.02 sec)

mysql> select * from id_uuid where id =‘e27267ba-a897-11e6-b4a0-742f68b93fab‘;
+--------------------------------------+-----------+
| id                                   | name      |
+--------------------------------------+-----------+
| e27267ba-a897-11e6-b4a0-742f68b93fab | aaa550010 |
+--------------------------------------+-----------+
1 row in set (0.00 sec)

mysql> select * from id_uuid20 where id = 24811291965678717;
+-------------------+-----------+
| id                | name      |
+-------------------+-----------+
| 24811291965678717 | aaa550010 |
+-------------------+-----------+
1 row in set (0.04 sec)

基于name查询(无索引):

mysql> select * from id_int where name = ‘aaa550010‘;
+--------+-----------+
| id     | name      |
+--------+-----------+
| 550027 | aaa550010 |
+--------+-----------+
1 row in set (0.93 sec)

mysql> select * from id_uuid where name = ‘aaa550010‘;
+--------------------------------------+-----------+
| id                                   | name      |
+--------------------------------------+-----------+
| e27267ba-a897-11e6-b4a0-742f68b93fab | aaa550010 |
+--------------------------------------+-----------+
1 row in set (6.36 sec)

mysql> select * from id_uuid20 where name = ‘aaa550010‘;
+-------------------+-----------+
| id                | name      |
+-------------------+-----------+
| 24811291965678717 | aaa550010 |
+-------------------+-----------+
1 row in set (0.82 sec)

加入索引后

mysql> select * from id_int where name = ‘aaa550010‘;
+--------+-----------+
| id     | name      |
+--------+-----------+
| 550027 | aaa550010 |
+--------+-----------+
1 row in set (0.06 sec)

mysql> select * from id_uuid where name = ‘aaa550010‘;
+--------------------------------------+-----------+
| id                                   | name      |
+--------------------------------------+-----------+
| e27267ba-a897-11e6-b4a0-742f68b93fab | aaa550010 |
+--------------------------------------+-----------+
1 row in set (0.00 sec)

mysql> select * from id_int where name = ‘aaa550010‘;
+--------+-----------+
| id     | name      |
+--------+-----------+
| 550027 | aaa550010 |
+--------+-----------+
1 row in set (0.00 sec)

插入操作:

mysql> insert into id_int (name) values(‘ccccd‘);
Query OK, 1 row affected (0.02 sec)

mysql> insert into id_uuid20 (id,name) values(uuid_short(),‘ccccc‘);
Query OK, 1 row affected (0.13 sec)

mysql> insert into id_uuid20 (id,name) values(uuid_short(),‘cccc1‘);
Query OK, 1 row affected (0.06 sec)

mysql> insert into id_uuid20 (id,name) values(uuid_short(),‘cccc3‘);
Query OK, 1 row affected (0.07 sec)

mysql> insert into id_int (name) values(‘cccc1‘);
Query OK, 1 row affected (0.02 sec)

mysql> insert into id_int (name) values(‘cccc2‘);
Query OK, 1 row affected (0.07 sec)

mysql> insert into id_int (name) values(‘cccc3‘);
Query OK, 1 row affected (0.07 sec)

mysql> insert into id_uuid20 (id,name) values(5231231231111111,‘cccc4‘);
Query OK, 1 row affected (0.04 sec)

mysql> insert into id_uuid20 (id,name) values(5231231231111112,‘cccc5‘);
Query OK, 1 row affected (0.02 sec)

mysql> insert into id_uuid20 (id,name) values(5531231231111112,‘cccc5‘);
Query OK, 1 row affected (0.07 sec)

mysql> insert into id_uuid (id,name) values(uuid(),‘cccc1‘);
Query OK, 1 row affected (0.07 sec)

mysql> insert into id_uuid (id,name) values(uuid(),‘cccc2‘);
Query OK, 1 row affected (0.05 sec)

mysql> insert into id_uuid (id,name) values(uuid(),‘cccc3‘);
Query OK, 1 row affected (0.03 sec)

mysql> insert into id_uuid (id,name) values(‘11cccasdqwdeqweqw‘,‘cccc4‘);
Query OK, 1 row affected (0.07 sec)

mysql> insert into id_uuid (id,name) values(‘12cccasdqwdeqweqw‘,‘cccc5‘);
Query OK, 1 row affected (0.07 sec)

参考:http://j2ees.iteye.com/blog/1554423

时间: 2024-08-29 19:01:06

mysql主键uuid、uuid_short和int自增对比的相关文章

MySQL主键设计

原文:MySQL主键设计 [TOC] 在项目过程中遇到一个看似极为基础的问题,但是在深入思考后还是引出了不少问题,觉得有必要把这一学习过程进行记录. MySQL主键设计原则 MySQL主键应当是对用户没有意义的. MySQL主键应该是单列的,以便提高连接和筛选操作的效率 永远也不要更新MySQL主键 MySQL主键不应包含动态变化的数据,如时间戳.创建时间列.修改时间列等 MySQL主键应当有计算机自动生成. 主键设计的常用方案 自增ID 优点: 1.数据库自动编号,速度快,而且是增量增长,聚集

MySQL主键设计盘点

目录 主键定义 主键设计和应用原则 主键生成策略 自增ID UUID 自建的id生成器 Twitter的snowflake算法 @ 最近在项目中用了UUID的方式生成主键,一开始只是想把这种UUID的方式生成主键记录下来,在查阅资料的过程中,又有了一些新的认识和思考. 主键定义 唯一标识表中每行的一个列(或一组列)称为主键.主键用来表示一个特定的行. 主键设计和应用原则 除了满足MySQL强制实施的规则(主键不可重复:一行中主键不可为空)之外,主键的设计和应用应当还遵守以下公认的原则: 不更新主

mysql主键的缺少导致备库hang

最近线上频繁的出现slave延时的情况,经排查发现为用户在删除数据的时候,由于表主键的主键的缺少,同时删除条件没有索引,或或者删除的条件过滤性极差,导致slave出现hang住,严重的影响了生产环境的稳定性,也希望通过这篇博客,来加深主键在innodb引擎中的重要性,希望用户在使用RDS,设计自己的表的时候,一定要为表加上主键,主键可以认为是innodb存储引擎的生命,下面我们就来分析一下这个案例(本案例的生产环境的binlog为row模式,对于myisam存储引擎也有同样的问题):(1).现象

MYSQL主键自动增加的配置

文章一 原文地址: http://ej38.com/showinfo/mysql-202971.html 文章二:   点击转入第二篇文章 在数据库应用,我们经常要用到唯一编号.在MySQL中可通过字段的AUTO_INCREMENT属性来自动生成.MySQL支持多种数据表,每种数据表的自增属性都有所不同. ISAM表 如果把一个NULL插入到一个AUTO_INCREMENT数据列里去,MySQL将自动生成下一个序列编号.编号从1开始,并1为基数递增. 当插入记录时,没有为AUTO_INCREME

【转载】mysql主键的缺少导致备库hang

最近线上频繁的出现slave延时的情况,经排查发现为用户在删除数据的时候,由于表主键的主键的缺少,同时删除条件没有索引,或或者删除的条件过滤性极差,导致slave出现hang住,严重的影响了生产环境的稳定性,也希望通过这篇博客,来加深主键在innodb引擎中的重要性,希望用户在使用RDS,设计自己的表的时候,一定要为表加上主键,主键可以认为是innodb存储引擎的生命,下面我们就来分析一下这个案例(本案例的生产环境的binlog为row模式,对于myisam存储引擎也有同样的问题):(1).现象

主键设置,总的来说int&gt;Guid/string

主键设置,总的来说int>Guid/string 一般设置int为主键可以有更高的效率 mysql: http://venublog.com/2010/04/19/choosing-the-right-data-type-makes-a-big-difference/http://venublog.com/2010/04/20/int-and-string-data-comparison-difference-in-performance-because-of-quotes/http://www

MySQL主键自动生成和生成器表以及JPA主键映射

MySQL主键自动生成 表设计 MySQL有许多主键生成策略,其中很常见的一种是自动生成.一般情况下,主键类型是BIGINT UNSIGNED,自动生成主键的关键词是AUTO_INCREMENT. CREATE TABLE Stock ( id BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, NO VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, price DECIMAL(6,2) N

mysql——主键自动增长&amp;唯一索引

首先说一下主键和唯一索引的区别 主键:一个数据库的一张表有且仅有一个主键,而且主键不能重复 唯一索引:一个数据库的一张表上唯一索引可以有多个,只是所在唯一索引上的值不能重复,这一点和主键一样 下面我们创建一个有主键有唯一索引的,并且主键是自动增长 create table demo (id int primary key auto_increment, name char(10) unique key, age int); 接下来我们插入数据看看 mysql--主键自动增长&唯一索引 原文地址:

Oracle与Mysql主键、索引及分页的区别小结

Oracle与Mysql主键.索引及分页的区别,学习oracle的朋友可以参考下 区别: 1.主键,Oracle不可以实现自增,mysql可以实现自增. oracle新建序列,SEQ_USER_Id.nextval 2.索引: mysql索引从0开始,Oracle从1开始. 3.分页, mysql: select * from user order by desc limit n ,m. 表示,从第n条数据开始查找,一共查找m条数据. Oracle:select * from user sele