C
C#11mo ago
Core

Is there a standard option to generate UUID version 7? (Guid)

Hello, .NET provides a way to generate UUID v4 with Guid.NewGuid(), but this cannot be used as a PK for tables. In Postgres newer UUID versions can be used as primary keys, as they are sortable and because of that there is no index fragmentation. Is there a standard library that is used for generating UUID 7? It is a bit odd there is no built in way to do this.
4 Replies
jcotton42
jcotton4211mo ago
are you looking specifically for a database scenario? b/c usually you'd want the database to do that work but to answer your question directly, afaik no
Kiel
Kiel11mo ago
I use https://github.com/medo64/Medo.Uuid7 - it has efcore integration to take care of all the conversion stuff i believe
GitHub
GitHub - medo64/Medo.Uuid7: This is a C# implementation of UUIDv7 g...
This is a C# implementation of UUIDv7 generation algorithm. - GitHub - medo64/Medo.Uuid7: This is a C# implementation of UUIDv7 generation algorithm.
Kiel
Kiel11mo ago
it's not available in the BCL yet and probably won't be for awhile if ever just to avoid confusion over multiple potentially conflicting UUID types
Core
CoreOP11mo ago
Thanks, after doing a bit of research, it turns out that the UUID 7 is still in development: https://uuid.ramsey.dev/en/stable/rfc4122/version7.html It will take time until this is standardized
Method | Mean | Error | StdDev | Ratio | RatioSD | Rank | Gen0 | Allocated | Alloc Ratio |
|---------- |---------:|---------:|---------:|------:|--------:|-----:|-------:|----------:|------------:|
| NewGuid | 35.43 ns | 0.177 ns | 0.148 ns | 1.00 | 0.00 | 1 | - | - | NA |
| MedoUuid7 | 47.74 ns | 0.474 ns | 0.582 ns | 1.35 | 0.02 | 2 | 0.0032 | 40 B | NA |
| UUIDNext | 93.51 ns | 0.859 ns | 0.804 ns | 2.64 | 0.02 | 3 | - | - | NA |
Method | Mean | Error | StdDev | Ratio | RatioSD | Rank | Gen0 | Allocated | Alloc Ratio |
|---------- |---------:|---------:|---------:|------:|--------:|-----:|-------:|----------:|------------:|
| NewGuid | 35.43 ns | 0.177 ns | 0.148 ns | 1.00 | 0.00 | 1 | - | - | NA |
| MedoUuid7 | 47.74 ns | 0.474 ns | 0.582 ns | 1.35 | 0.02 | 2 | 0.0032 | 40 B | NA |
| UUIDNext | 93.51 ns | 0.859 ns | 0.804 ns | 2.64 | 0.02 | 3 | - | - | NA |
The speed of the generations is not much worse compared to the Guid.NewGuid() I will go with the Medo.Uuid7, thank you for the suggestion Isn't it a bit concerning that it allocates 40B memory?
Want results from more Discord servers?
Add your server