Skip to content

Commit 9235845

Browse files
authored
Performance optimizations + docs and tests changes (#4)
1 parent c68a96e commit 9235845

8 files changed

Lines changed: 350 additions & 365 deletions

File tree

docs/en/sql-reference/functions/uuid-functions.md

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,27 @@ SELECT generateUUIDv4(1), generateUUIDv4(2);
5252
└──────────────────────────────────────┴──────────────────────────────────────┘
5353
```
5454

55-
## generateUUIDv7
55+
## generateUUIDv7 {#uuidv7-function-generate}
5656

5757
Generates a [UUID](../data-types/uuid.md) of [version 7](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04).
5858

5959
The generated UUID contains the current Unix timestamp in milliseconds (48 bits), followed by version "7" (4 bits), a counter (42 bit) to distinguish UUIDs within a millisecond (including a variant field "2", 2 bit), and a random field (32 bits).
60+
At any given new timestamp in unix_ts_ms, the counter starts from some random value and then it's being increased by 1 on each new UUID v7 with counter generation until current timestamp changes.
61+
The counter overflow causes unix_ts_ms field increment by 1 and the counter restart from a random value. Counter increment monotony at one timestamp is guaranteed across all `generateUUIDv7` functions running simultaneously.
62+
63+
:::note
64+
As of April 2024 UUIDv7 is only a draft and the layout may change in future.
65+
:::
6066

6167
```
6268
0 1 2 3
6369
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
6470
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
6571
| unix_ts_ms |
6672
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
67-
| unix_ts_ms | ver | rand_a |
73+
| unix_ts_ms | ver | counter_high_bits |
6874
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
69-
|var| rand_b |
75+
|var| counter_low_bits |
7076
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
7177
| rand_b |
7278
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
@@ -110,40 +116,25 @@ SELECT * FROM tab;
110116
SELECT generateUUIDv7(1), generateUUIDv7(2);
111117

112118
┌─generateUUIDv7(1)────────────────────┬─generateUUIDv7(2)────────────────────┐
113-
018f05b1-8c2e-7567-a988-48d09606ae8c018f05b1-8c2e-7946-895b-fcd7635da9a0
119+
018f05c9-4ab8-7b86-b64e-c9f03fbd45d1018f05c9-4ab8-7b86-b64e-c9f12efb7e16
114120
└──────────────────────────────────────┴──────────────────────────────────────┘
115121
```
116122

117-
## generateUUIDv7WithCounter
123+
## generateUUIDv7WithFastCounter
118124

119125
Generates a [UUID](../data-types/uuid.md) of [version 7](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04).
120126

121-
The generated UUID contains the current Unix timestamp in milliseconds (48 bits), followed by version "7" (4 bits), a counter (42 bit) to distinguish UUIDs within a millisecond (including a variant field "2", 2 bit), and a random field (32 bits).
122-
At any given new timestamp in unix_ts_ms, the counter starts from some random value and then it's being increased by 1 on each new UUID v7 with counter generation until current timestamp changes.
123-
The counter overflow causes unix_ts_ms field increment by 1 and the counter restart from a random value. Counter increment monotony at one timestamp is guaranteed across all `generateUUIDv7WithCounter` functions running simultaneously.
124-
```
125-
0 1 2 3
126-
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
127-
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
128-
| unix_ts_ms |
129-
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
130-
| unix_ts_ms | ver | counter_high_bits |
131-
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
132-
|var| counter_low_bits |
133-
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
134-
| rand_b |
135-
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
136-
```
127+
This function behaves like [generateUUIDv7](#uuidv7-function-generate) but gives no guarantee on counter monotony across different requests running simultaneously. Counter increment monotony at one timestamp is guaranteed only within one thread calling this function to generate many UUIDs.
137128

138129
**Syntax**
139130

140131
``` sql
141-
generateUUIDv7WithCounter([x])
132+
generateUUIDv7WithFastCounter([x])
142133
```
143134

144135
**Arguments**
145136

146-
- `x`[Expression](../../sql-reference/syntax.md#syntax-expressions) resulting in any of the [supported data types](../../sql-reference/data-types/index.md#data_types). The expression is used to bypass [common subexpression elimination](../../sql-reference/functions/index.md#common-subexpression-elimination) if the function is called multiple times in a query but otherwise ignored. Optional.
137+
- `x`[Expression](../../sql-reference/syntax.md#syntax-expressions) resulting in any of the [supported data types](../../sql-reference/data-types/index.md#data_types). The resulting value is discarded, but the expression itself if used for bypassing [common subexpression elimination](../../sql-reference/functions/index.md#common-subexpression-elimination) if the function is called multiple times in one query. Optional parameter.
147138

148139
**Returned value**
149140

@@ -156,72 +147,92 @@ First, create a table with a column of type UUID, then insert a generated UUIDv7
156147
``` sql
157148
CREATE TABLE tab (uuid UUID) ENGINE = Memory;
158149

159-
INSERT INTO tab SELECT generateUUIDv7WithCounter();
150+
INSERT INTO tab SELECT generateUUIDv7WithFastCounter();
160151

161152
SELECT * FROM tab;
162153
```
163154

164155
```response
165156
┌─────────────────────────────────uuid─┐
166-
018f05c7-56e3-7ac3-93e9-1d93c4218e0e
157+
018f05e2-e3b2-70cb-b8be-64b09b626d32
167158
└──────────────────────────────────────┘
168159
```
169160

170161
**Example where multiple UUIDs are generated per row**
171162

172163
```sql
173-
SELECT generateUUIDv7WithCounter(1), generateUUIDv7WithCounter(2);
164+
SELECT generateUUIDv7WithFastCounter(1), generateUUIDv7WithFastCounter(2);
174165

175-
┌─generateUUIDv7WithCounter(1)─────────┬─generateUUIDv7WithCounter(2)─────────┐
176-
018f05c9-4ab8-7b86-b64e-c9f03fbd45d1018f05c9-4ab8-7b86-b64e-c9f12efb7e16
166+
┌─generateUUIDv7WithFastCounter(1)─────┬─generateUUIDv7WithFastCounter(2)─────┐
167+
018f05e1-14ee-7bc5-9906-207153b400b1018f05e1-14ee-7bc5-9906-2072b8e96758
177168
└──────────────────────────────────────┴──────────────────────────────────────┘
178169
```
179170

180-
## generateUUIDv7WithFastCounter
171+
172+
## generateUUIDv7NonMonotonic
181173

182174
Generates a [UUID](../data-types/uuid.md) of [version 7](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04).
183175

184-
This function behaves like `generateUUIDv7WithCounter` but gives no guarantee on counter monotony across different requests running simultaneously. Counter increment monotony at one timestamp is guaranteed only within one thread calling this function to generate many UUIDs.
176+
The generated UUID contains the current Unix timestamp in milliseconds (48 bits), followed by version "7" (4 bits) and a random field (76 bits) (including a variant field "2", 2 bit).
177+
The monotonicity within one timestamp is not guaranteed at all. This is the fastest version of `generateUUIDv7*` functions family.
178+
179+
:::note
180+
As of April 2024 UUIDv7 is only a draft and the layout may change in future.
181+
:::
182+
183+
```
184+
0 1 2 3
185+
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
186+
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
187+
| unix_ts_ms |
188+
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
189+
| unix_ts_ms | ver | rand_a |
190+
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
191+
|var| rand_b |
192+
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
193+
| rand_b |
194+
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
195+
```
185196

186197
**Syntax**
187198

188199
``` sql
189-
generateUUIDv7WithFastCounter([x])
200+
generateUUIDv7NonMonotonic([x])
190201
```
191202

192203
**Arguments**
193204

194-
- `x`[Expression](../../sql-reference/syntax.md#syntax-expressions) resulting in any of the [supported data types](../../sql-reference/data-types/index.md#data_types). The resulting value is discarded, but the expression itself if used for bypassing [common subexpression elimination](../../sql-reference/functions/index.md#common-subexpression-elimination) if the function is called multiple times in one query. Optional parameter.
205+
- `x`[Expression](../../sql-reference/syntax.md#syntax-expressions) resulting in any of the [supported data types](../../sql-reference/data-types/index.md#data_types). The expression is used to bypass [common subexpression elimination](../../sql-reference/functions/index.md#common-subexpression-elimination) if the function is called multiple times in a query but otherwise ignored. Optional.
195206

196207
**Returned value**
197208

198209
A value of type UUIDv7.
199210

200-
**Usage example**
211+
**Example**
201212

202213
First, create a table with a column of type UUID, then insert a generated UUIDv7 into the table.
203214

204215
``` sql
205216
CREATE TABLE tab (uuid UUID) ENGINE = Memory;
206217

207-
INSERT INTO tab SELECT generateUUIDv7WithFastCounter();
218+
INSERT INTO tab SELECT generateUUIDv7NonMonotonic();
208219

209220
SELECT * FROM tab;
210221
```
211222

212223
```response
213224
┌─────────────────────────────────uuid─┐
214-
018f05e2-e3b2-70cb-b8be-64b09b626d32
225+
018f05af-f4a8-778f-beee-1bedbc95c93b
215226
└──────────────────────────────────────┘
216227
```
217228

218229
**Example where multiple UUIDs are generated per row**
219230

220231
```sql
221-
SELECT generateUUIDv7WithFastCounter(1), generateUUIDv7WithFastCounter(2);
232+
SELECT generateUUIDv7NonMonotonic(1), generateUUIDv7NonMonotonic(2);
222233

223-
┌─generateUUIDv7WithFastCounter(1)─────┬─generateUUIDv7WithFastCounter(2)─────┐
224-
018f05e1-14ee-7bc5-9906-207153b400b1018f05e1-14ee-7bc5-9906-2072b8e96758
234+
┌─generateUUIDv7NonMonotonic(1) ───────┬─generateUUIDv7(2)NonMonotonic────────┐
235+
018f05b1-8c2e-7567-a988-48d09606ae8c018f05b1-8c2e-7946-895b-fcd7635da9a0
225236
└──────────────────────────────────────┴──────────────────────────────────────┘
226237
```
227238

0 commit comments

Comments
 (0)