abstract:数据库主键性能对比: 名称 存储长度 生成方式 1. uuid 32+4 uuid()函数 2. uuid20 20 UUID_SHORT
数据库主键性能对比:
名称 存储长度 生成方式 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
数据插入过程能发现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)