Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.
Original topic: RANDOM 生成的ID是否是有序递增的?
[TiDB Usage Environment] Production Environment / Testing / PoC
[TiDB Version] 6.5.5
[Reproduction Path] Whether the ID generated by setting RANDOM on the primary key in a single table is sequentially incremented
It is auto-increment, not sequentially increasing.
Yes, when inserting multiple rows at once
The specific structure of the AUTO_RANDOM
column value is as follows:
Total Bits |
Sign Bit |
Reserved Bits |
Shard Bits |
Incremental Bits |
64 bits |
0/1 bit |
(64-R) bits |
S bits |
(R-1-S) bits |
- The length of the sign bit is determined by whether the column has the
UNSIGNED
attribute: if it exists, it is 0
; otherwise, it is 1
.
- The length of the reserved bits is
64-R
, and the content of the reserved bits is always 0
.
- The content of the shard bits is obtained by calculating the hash value of the start time of the current transaction. To use a different number of shard bits (e.g., 10), you can specify
AUTO_RANDOM(10)
when creating the table.
- The value of the incremental bits is stored in the storage engine and allocated sequentially, incrementing by 1 each time a value is allocated. The incremental bits ensure the global uniqueness of the
AUTO_RANDOM
column value.
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.