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 ...
随机推荐
- JavaScript老梗之this对象
Js中的this关键词貌似是初学者必经的坑 都不例外 我们经常听到其他人说 this对象谁调用指向谁 的确这是最容易理解的 但是我们可以更加深入的去探索下 加深印象以便更加灵活的适用它 这里不得不提下 ...
- MySQL - 问题集 - "Waiting for table metadata lock"(待完善)
待完善.show processlist; 可参考1:http://blog.csdn.net/huochuangchuang/article/details/49423893 可参考2:http:/ ...
- python 线程之 threading(三)
python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...
- VS2010 有关测试的一些使用
Visual Studio 2010 单元测试之一---普通单元测试:http://blog.csdn.net/tjvictor/archive/2011/02/09/6175362.aspx V ...
- 解决Spring的Component-scan和packagesToScan不支持Eclipse RCP问题
http://www.360doc.com/content/13/0401/13/10825198_275274565.shtml
- android--访问网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
- NOI 题库 6264
6264 走出迷宫 描述 当你站在一个迷宫里的时候,往往会被错综复杂的道路弄得失去方向感,如果你能得到迷宫地图,事情就会变得非常简单. 假设你已经得到了一个n*m的迷宫的图纸,请你找出从起点到出口的 ...
- 【JAVA】Runtime
1.内存管理:Java提供了无用单元自动收集机制.通过totalMemory()和freeMemory()方法可以知道对象的堆内存有多大,还剩多少.Java 会周期性的回收垃圾对象(未使用的对象),以 ...
- ZeroMQ接口函数之 :zmq_ctx_term - 终结一个ZMQ环境上下文
ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_ctx_term zmq_ctx_term(3) ØMQ Manual - ØMQ/4.1.0 Name zmq_ ...
- O(n)求1-n的逆元
转自:http://www.2cto.com/kf/201401/272375.html 新学的一个求逆元的方法: inv[i] = ( MOD - MOD / i ) * inv[MOD%i] % ...