[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>前言
以前只知道UUID不适合作为主键,但是不知道具体原因,最近做了了解,在此做一个记录
<>索引
我们先了解一下mysql索引的数据结构,mysql在innodb的存储引擎下是使用B+树来做为索引的数据结构的,而一个表最终必定会创建一个主键索引
,即是没有设置主键也会有生成规则(先查找有没有非空的唯一索引,没有则可以使用默认的rowid字段创建索引)
mysql中存储数据的最小单元称之为page(页),大小为16KB,也就是在索引的结构中一个page存储了很多个数据,可以看上图第一个块存储了1、28、66三个数据
我们知道索引采用B+树的结构后,数据必须是有序的
,如果此时page1已经写满,page2写了一半,当下一个数据是有序的时候,则数据将直接追加到page2的末尾。
如果数据是无序的,那么数据可能被插入到page1的某个位置,但是此时page1已经写满了,那么page1中插入数据后面的数据就必须往后移动,存不下的数据就只能放到其他的page,此时就发生了
page的分裂和合并,如果此时page2也是写满的,那么将依次影响其他的page,那么将大大影响数据的插入和删除,这就是主键索引不适合使用UUID的原因
主键不适合使用无序字段的原因:
1、无序字段作为索引会导致page的分裂与合并
2、UUID等数据占用字节数太多,而自增的int类型只需要占用4个字节,所有索引最终都会在叶子节点存储主键的id,会导致占用更多的磁盘空间以及降低io性能