数据库主键性能对比:

     名称        存储长度      生成方式
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

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

  1. MySQL主键设计

    [TOC] 在项目过程中遇到一个看似极为基础的问题,但是在深入思考后还是引出了不少问题,觉得有必要把这一学习过程进行记录. MySQL主键设计原则 MySQL主键应当是对用户没有意义的. MySQL主 ...

  2. MySQL主键设计盘点

    目录 主键定义 主键设计和应用原则 主键生成策略 自增ID UUID 自建的id生成器 Twitter的snowflake算法 @ 最近在项目中用了UUID的方式生成主键,一开始只是想把这种UUID的 ...

  3. MYSQL主键自动增加的配置及auto_increment注意事项

    文章一 原文地址: http://ej38.com/showinfo/mysql-202971.html 文章二:   点击转入第二篇文章 在数据库应用,我们经常要用到唯一编号.在MySQL中可通过字 ...

  4. 获得自动增长的MySQL主键

    下面的脚本教您如何获得自动增长的MySQL主键,如果您对MySQL主键方面感兴趣的话,不妨一看,相信对您学习MySQL主键方面会有所启迪. import java.sql.Connection; im ...

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

    最近线上频繁的出现slave延时的情况,经排查发现为用户在删除数据的时候,由于表主键的主键的缺少,同时删除条件没有索引,或或者删除的条件过滤性极差,导致slave出现hang住,严重的影响了生产环境的 ...

  6. mysql主键的缺少导致备库hang

    最近线上频繁的出现slave延时的情况,经排查发现为用户在删除数据的时候,由于表主键的主键的缺少,同时删除条件没有索引,或或者删除的条件过滤性极差,导致slave出现hang住,严重的影响了生产环境的 ...

  7. mysql 主键与外键

    一.主键详解,引用自:https://blog.csdn.net/haiross/article/details/50456154 1.要设置主键自增的话字段必须是整形数字. 二.外键详解:引用自ht ...

  8. Mysql 主键约束PrimaryKey

    Mysql 主键约束Primary Key 今天来简单的讲一下主键约束. 假如有一张学生信息表,里面记录了学生的学号 ,姓名,成绩等,那么,会不会有两个学号相同的学生,答案肯定是否定的,如果有的话也只 ...

  9. mysql主键问题

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_22314145/article/details/80824660 MySQL主键 一. MyS ...

随机推荐

  1. https

    http://www.cnblogs.com/bugly/p/5075909.html# http://www.cnblogs.com/peijian708/archive/2011/05/07/20 ...

  2. CSS3动画效果-7.13

    例如: <body> <div class="div1"></div> </body> CSS: @keyframes myfirs ...

  3. 比管理员(administrator)更高权限的TrustedInstaller

    http://www.gezila.com/tutorials/9664.html 什么是TrustedInstaller管理权限 ?好多朋友都在使用Windows7系统.在使用过程中,有些朋友在删除 ...

  4. C# 类动态添加属性、方法

    问题: 需要动态为WPF中的DataGrid添加列,并动态绑定相应数据.(此处仅实现动态属性的添加和使用,关于动态方法的添加和使用详见推荐阅读) 实现关键点: 目标类继承DynamicObject,添 ...

  5. nginx中配置pathinfo模式示例

    要想让nginx支持PATH_INFO,首先需要知道什么是pathinfo,为什么要用pathinfo? pathinfo不是nginx的功能,pathinfo是php的功能. php中有两个path ...

  6. 西门子成立next47部门,斥资十亿欧元投资VR/AR等初创公司

       近日,西门子公司在慕尼黑举行的"西门子创新日"现场,宣布了三个关于"创新"的新动作.首先,超过六成员工的创新应用得到肯定,其中有 25 个项目获得总数高达 ...

  7. [超级懒人最简单法]iPhone 6 plus 适配切图方法分享(转载文章)

    网络上已经有很多适配教程,可是看了半天总是半懂不懂..最后还是要综合多个教程再动动脑子动动手,最好有程序大哥帮你试一下(这得有多大的福气) 如果有跟我一样情况的: 1.       有人说用sketc ...

  8. EntityFrame CodeFirst 自动生成表

    来源:http://msdn.microsoft.com/zh-cn/data/jj193542 本视频和分步演练介绍通过 Code First 开发建立新数据库.这个方案包括建立不存在的数据库(Co ...

  9. Codeforces Round #107 (Div. 2)

    D题 并查集+组合 #include <iostream> #include <cstdio> #include <cstring> using namespace ...

  10. VS2010/VS2013中ashx代码折叠的问题

    Tools->Options->TextEditor->File Extension Add ashx Microsoft Visual C# Apply OK 重启VS就可以了,效 ...