数据库主键性能对比:名称        存储长度      生成方式

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

insert into id_int( name) VALUES ( concat('aaa',ct));

set ct=ct+1;

END WHILE;

-- 初始化uuid20

CREATE  PROCEDURE `uuid20_init`( in  s int)

BEGIN

DECLARE ct INT;

SET ct=1;

while ct

insert into id_uuid20(id ,name) VALUES (uuid_short(), concat('aaa',ct));

set ct=ct+1;

END WHILE;

-- 初始化uuid

CREATE PROCEDURE `uuid_init`(in  s int)

BEGIN

DECLARE ct INT;

SET ct=1;

while ct

insert into id_uuid (id, name) VALUES (uuid(), concat('aaa',ct));

set ct=ct+1;

END WHILE;

-- 分别调用

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)

数据库主键性能对比:名称    存储长度 生成方式1.  uuid       32+4     uuid()函数2.  uuid20      20   UUID_SHORT()函数3.bigint自增   20   auto_increment测试表:id_int()、--uuid测试表CREATETABLE`id_uuid`(`id`... 生成简洁,明确的,URL安全的 UUID 的Go库。 基于并与Python库兼容。 通常,人们需要在用户看到非顺序ID的地方使用它们,但是这些ID必须尽可能简明易用。 short uuid 通过使用生成 UUID ,然后使用小写和大写字母和数字将它们转换为base57,并删除外观相似的字符(例如l,1,I,O和0)来解决此问题。 package main import ( "fmt" "github.com/lithammer/ short uuid /v3" func main () { u := short uuid . New () // Cekw67uyMpBGZLRP2HFVbe 要使用 UUID v5(而不是默认v4),请使用NewWithNamespace(name string)代替New() 。 short uuid . NewWithNamespace ( "http://example.com" ) 也可以使用自定义字母,尽管它必须为57个字符长。 alphabet := "23456789ABCDEFGHJKLMNPQRSTUVW
MySQL -使用 UUID _ SHORT ( ) 的问题 文章目录 MySQL -使用 UUID _ SHORT ( ) 的问题问题说明简单分析解决分析解决认真查了一些详细的资料官方资料 表app_msg的 主键 id 设置的类型为:big int 20 使用插入语句:INSERT INT Oapp_msg(id,...) VALUES ( UUID _ SHORT (),...) 然而系统报错:[Err] 1264 -...
问题背景: 老项目从Oracle --> MySQL ,原来Oracle中有Sequence,而 MySQL 中没有。项目工期又很紧,得知 MySQL UUID _ SHORT ,就拿来用了;不过 UUID _ SHORT 返回的位数太多,值太大,就取后10位作为流水号。 问题爆发: 不过在使用了1、2年后,系统突然.. 没什么好讲的,肯定唯一 (2) UUID () 函数: MySQL UUID () 函数中,前三组数字从时间戳中生成,第四组数字暂时保持时间戳的唯一性,第五组数字是一个 IEEE 802 节点标点值,保证空间唯一。使用 UUID () 函数,可以生成时间、空间上都独一无二的值。 MySQL 5.1 之后的版本,提供 UUID _ SHORT () 函数,生成一个 64 位无符号整数 (3)程序自定义 Demo: select uuid (); select UU
在之前项目中,数据库生成 UUID 主键 一般会用Java调用函数,最近发现在 MySQL 也有自带的 uuid 函数,分别是 uuid ()和 uuid _ short (),现对这两个函数进行演示和说明。 在 MySQL 中,可以有如下几种途径实现唯一值: (1) 自增 序列 (2) UUID () 函数 (3)程序自定义 一、 uuid () UUID 基于 16 进制,由 32 位小写的 16 进制数字组成,如下: aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee 比如123e4567-e89b-12d3-a4
UUID 是通用唯一识别码(Universally Unique Identifier)的缩写,是一种软件建构的标准,亦为开放基金会组织在分布式计算机环境领域的一部分。 UUID 的标准型式包含32个16进制的数字,以连字号分为五段,形式为8-4-4-4-12的32个字符。 示例:550e8...
偶然的机会,得知 mysql 主键 的类型采用 varchar 存 UUID 的查询性能没有 int 型做 主键 好。网上查询大量资料,都是停留在理论上的,因此,自己写了代码进行实测,以下结果仅供参考,不具备权威性。 三个表的字段,除了 主键 ID 分别采用varchar,big int 和自动增长big int 不同外,其他三个字段都为 varchar 36位 数据库: mysql 5.5 表类型:InnoDB 数据...
MySQL 中,可以用 uuid ()函数来生成一个 UUID ,如下图: replace函数 默认生成的 uuid 含有’-’,我们可以使用replace函数替换掉’-’,SQL如下: select replace( uuid (),"-","") as uuid ; 结果如下: Insert语句中使用 UUID 如果一个表中id字段使用 uuid 来作为 主键 ,那我们可以使用下面的语句来插入数据: INSERT INT O t_inventive_principle (id,code_num,
可以使用 UUID 作为 主键 ,但是 UUID 是不可 自增 的。如果需要 自增 主键 ,可以使用 MySQL 提供的AUTO_INCREMENT属性。例如: CREATE TABLE `table_name` ( `id` INT (11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 其中,id为 自增 主键 ,每次插入数据时会自动增加。