Blog Friends RSS About

猜想: 如果拿UUID做数据库主键...



1. Branching Factor

什么是 Branching Factor?

In computing, tree data structures, and game theory, the branching factor is the number of children at each node, the outdegree.


假如 InnoDB 的 page 为16 KB,如果放 integer(4 Byte) 可以容纳 16*1024/4=4096 个 children;

如果放32位 UUID 可以容纳 16*1024/(32*4) = 128 children.

对于一个 1,000,000 条记录的表:

  • Integer B+ 树的高度为 log4096(1,000,000) = 1.66

  • UUID B+ 树的高度为 log129(1,000,000) 2.84

猜测:对 Read 的性能不会有太大影响。

2. 对 cluster index 的影响

What is cluster index?

Clustered Indexes: In clustered index, the non-leaf level points to the actual data.


  1. UUID 是无序的,每一次 Insert 都是磁盘的 Random write。写入效率降低。
  2. 可能会导致 B+ 的节点分裂,引起连锁反应。写入效率变低,或更糟。

3. 对 secondary index 的影响

What’s secondary index?

Non-Clustered Indexes: In Non-Clustered index the leaf nodes point to primary key, which then point to actual data.

All indexes other than the clustered index are known as secondary indexes. In InnoDB, each record in a secondary index contains the primary key columns for the row, as well as the columns specified for the secondary index. InnoDB uses this primary key value to search for the row in the clustered index.

猜测:secondary index 的 leaf 默认会指向主键。如果主键是 32 byte 的UUID,那么所有的 secondary key 的大小就会翻8倍。


22 November 2016