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
mysql主键uuid、uuid_short和int自增对比的更多相关文章
- MySQL主键设计
[TOC] 在项目过程中遇到一个看似极为基础的问题,但是在深入思考后还是引出了不少问题,觉得有必要把这一学习过程进行记录. MySQL主键设计原则 MySQL主键应当是对用户没有意义的. MySQL主 ...
- MySQL主键设计盘点
目录 主键定义 主键设计和应用原则 主键生成策略 自增ID UUID 自建的id生成器 Twitter的snowflake算法 @ 最近在项目中用了UUID的方式生成主键,一开始只是想把这种UUID的 ...
- MYSQL主键自动增加的配置及auto_increment注意事项
文章一 原文地址: http://ej38.com/showinfo/mysql-202971.html 文章二: 点击转入第二篇文章 在数据库应用,我们经常要用到唯一编号.在MySQL中可通过字 ...
- 获得自动增长的MySQL主键
下面的脚本教您如何获得自动增长的MySQL主键,如果您对MySQL主键方面感兴趣的话,不妨一看,相信对您学习MySQL主键方面会有所启迪. import java.sql.Connection; im ...
- 【转载】mysql主键的缺少导致备库hang
最近线上频繁的出现slave延时的情况,经排查发现为用户在删除数据的时候,由于表主键的主键的缺少,同时删除条件没有索引,或或者删除的条件过滤性极差,导致slave出现hang住,严重的影响了生产环境的 ...
- mysql主键的缺少导致备库hang
最近线上频繁的出现slave延时的情况,经排查发现为用户在删除数据的时候,由于表主键的主键的缺少,同时删除条件没有索引,或或者删除的条件过滤性极差,导致slave出现hang住,严重的影响了生产环境的 ...
- mysql 主键与外键
一.主键详解,引用自:https://blog.csdn.net/haiross/article/details/50456154 1.要设置主键自增的话字段必须是整形数字. 二.外键详解:引用自ht ...
- Mysql 主键约束PrimaryKey
Mysql 主键约束Primary Key 今天来简单的讲一下主键约束. 假如有一张学生信息表,里面记录了学生的学号 ,姓名,成绩等,那么,会不会有两个学号相同的学生,答案肯定是否定的,如果有的话也只 ...
- mysql主键问题
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_22314145/article/details/80824660 MySQL主键 一. MyS ...
随机推荐
- Fiddler
Fiddler教程: 原文:http://kb.cnblogs.com/page/130367/
- IP地址的分类
IPv4 地址的分类: 一,组成 1. 使用32位地址 2. 以点分十进制表示,如172.16.0.0,每一个数字对应于8个二进制的比特串,称为一个位组(octets).如某一台主机的IP地址 ...
- C# 执行文件的根目录 (转)
1.取得控制台应用程序的根目录方法 方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径方法2.AppDomain.CurrentDomain.Bas ...
- ASP.NET4.5Web API及非同步程序开发系列(3)
接着上一篇博客的内容做一个补充,正好是一个大哥提出来的,我们看看一个有趣的现象. 请求相关问题的补充: 我们先在Controller中的定义一个我们在前一篇博客中已经测试过的方法如下: public ...
- jQuery插件(拖拽)
拖曳插件draggable的功能是拖动被绑定的元素,当这个jQuery UI插件与元素绑定后,可以通过调用draggable()方法,实现各种拖曳元素的效果,调用格式如下: $(selector). ...
- inner join on 和 where = 的区别!
请看下面两条语句:select * from table1 inner join table2 on table1.id = table2.idselect * from table1,table2 ...
- android 多媒体数据库详解(转)
转自:http://www.2cto.com/kf/201309/242876.html 主要分为几节: 1. Android的媒体文件内部是如何存储的? 2. Andoid的媒体文件如何获取? 3. ...
- NOIP2009潜伏者【B003】
[B003]潜伏者[难度B]—————————————————————————————————————————————————————————————————— [题目要求] R国和S国正陷入战火之中 ...
- CodeForces 544A
You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concat ...
- 【原】PHP初体验
1. 相关内容介绍 1>互联网开发 互联网:传统互联网.移动互联网 互联网开发:前端开发(前台).后台开发(后端.服务端) 前端开发:视觉展示(用户界面).用户交互.采集输入信息 后台开发:管理 ...