You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generates a [UUID](../data-types/uuid.md) of [version 7](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04).
58
58
59
59
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.
Generates a [UUID](../data-types/uuid.md) of [version 7](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04).
120
126
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.
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.
137
128
138
129
**Syntax**
139
130
140
131
```sql
141
-
generateUUIDv7WithCounter([x])
132
+
generateUUIDv7WithFastCounter([x])
142
133
```
143
134
144
135
**Arguments**
145
136
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.
147
138
148
139
**Returned value**
149
140
@@ -156,72 +147,92 @@ First, create a table with a column of type UUID, then insert a generated UUIDv7
156
147
```sql
157
148
CREATETABLEtab (uuid UUID) ENGINE = Memory;
158
149
159
-
INSERT INTO tab SELECTgenerateUUIDv7WithCounter();
150
+
INSERT INTO tab SELECTgenerateUUIDv7WithFastCounter();
160
151
161
152
SELECT*FROM tab;
162
153
```
163
154
164
155
```response
165
156
┌─────────────────────────────────uuid─┐
166
-
│ 018f05c7-56e3-7ac3-93e9-1d93c4218e0e │
157
+
│ 018f05e2-e3b2-70cb-b8be-64b09b626d32 │
167
158
└──────────────────────────────────────┘
168
159
```
169
160
170
161
**Example where multiple UUIDs are generated per row**
Generates a [UUID](../data-types/uuid.md) of [version 7](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04).
183
175
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.
-`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.
195
206
196
207
**Returned value**
197
208
198
209
A value of type UUIDv7.
199
210
200
-
**Usage example**
211
+
**Example**
201
212
202
213
First, create a table with a column of type UUID, then insert a generated UUIDv7 into the table.
203
214
204
215
```sql
205
216
CREATETABLEtab (uuid UUID) ENGINE = Memory;
206
217
207
-
INSERT INTO tab SELECTgenerateUUIDv7WithFastCounter();
218
+
INSERT INTO tab SELECTgenerateUUIDv7NonMonotonic();
208
219
209
220
SELECT*FROM tab;
210
221
```
211
222
212
223
```response
213
224
┌─────────────────────────────────uuid─┐
214
-
│ 018f05e2-e3b2-70cb-b8be-64b09b626d32 │
225
+
│ 018f05af-f4a8-778f-beee-1bedbc95c93b │
215
226
└──────────────────────────────────────┘
216
227
```
217
228
218
229
**Example where multiple UUIDs are generated per row**
0 commit comments