在很多小项目中,我们往往直接使用数据库自增特性来生成主键ID,这样确实比较简单。而在分库分表的环境下,数据分布在不同的数据表中,不能再借助数据库自增特性直接生成,否则会造成不同数据表主键重复。下面介绍集中ID生成算法。
UUID
UUID是通用唯一标识码(Universally Unique Identifier)的缩写,长度是16个字节,被表示为32个十六进制数字,以‘-’分隔的五组来显示,格式为8-4-4-4-12,共36个字符,例如:be277cf0-9fed-433e-b958-eb82e8c0f142。UUID在在生成时使用到了以太网地址、纳秒级时间、芯片ID和随机数等信息,目的是让分布式系统中的所有元素都能有唯一的标示信息。
使用UUID作主键,可以在本地生成,没有网络消耗,所以性能非常高。但是UUID比较长,没有规律性,消耗存储空间。
All indexes other thantheclustered index are known as secondary indexes.In innoDB,each record in secondary index contains the primary key columns for the row, as the columns specified for the secondary index.InnoDB uses this primary key value to search for the row in the clustered index.If the primary key is long,the secondary indexes use more space,so it is advantageous to have a short primary key.
除聚集索引意外的