数据库主键性能对比:

     名称        存储长度      生成方式
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. 【Java EE 学习 17 上】【dbutils和回调函数】

    一.dbutils的核心就是回调函数,可以说如果没有回调函数的思想,dbutils是不可能被开发出来的. 对于dbutils中的QuryRunner类,向该类的query方法提供不同的参数,可以得到不 ...

  2. POJ 1743 Musical Theme 二分+后缀数组

    Musical Theme   Description A musical melody is represented as a sequence of N (1<=N<=20000)no ...

  3. 已解决:ECSHOP安装出现date_default_timezone_get()问题

    今天在安装ECSHOP时遇到警告如下: Warning: date_default_timezone_get(): It is not safe to rely on the system's tim ...

  4. Guava学习笔记(3):复写的Object常用方法

    转自:http://www.cnblogs.com/peida/p/Guava_Objects.html 在Java中Object类是所有类的父类,其中有几个需要override的方法比如equals ...

  5. eclipse运行速度优化(解决狂读盘、发布慢、CPU100%等问题)

    转自:http://blog.csdn.net/wyp_810618/article/details/8953921 我的ECLIPSE运行时速度奇慢,具体表现为: 1.只要ECLIPSE启动后,硬盘 ...

  6. iOS 启动画面 代码自定义

    先来看一个可能会遇到的问题: 如果你已经删除了xcode为你的项目自动生成的LaunchScreen.storyboard, 然后你在测试你的app的时候发现,屏幕里出现了黑色的区域,如上图(画红线的 ...

  7. jquery取值

    1. 如何用jquery获取<input id="test" name="test" type="text"/>中输入的值?$( ...

  8. 从零开始山寨Caffe·肆:线程系统

    不精通多线程优化的程序员,不是好程序员,连码农都不是. ——并行计算时代掌握多线程的重要性 线程与操作系统 用户线程与内核线程 广义上线程分为用户线程和内核线程. 前者已经绝迹,它一般只存在于早期不支 ...

  9. Web服务器基础学习

    1)Socket通信相当于两个人通过电话联系,Http协议相当于电话联系时所使用的中文2)Http1.1前均为短连接,1.1版本为长连接,即服务器接收一次请求并发送响应后会等待一段时间看浏览器是否在这 ...

  10. python 获取网页图片

    re为正则表达式模组 re.findall在字符串中查找所有匹配的模式,返回一个list urllib2提供了使用简单的url工具 urllib2.urlopen发送url请求,返回一个文件流 imp ...