mysql主键uuiduuid_short和int自增对比
Posted Mr.Liu08
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql主键uuiduuid_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主键uuiduuid_short和int自增对比的主要内容,如果未能解决你的问题,请参考以下文章
MySql中测试GUID 与Int自增主键 性能对比 总结适用场景