![]() Using UUID as the primary key will take up more space, as it will have to store a string instead of a number.This takes longer as it's a string and not an int that is already ordered Using UUID as the primary key will make your inserts slower, as MySQL will have to update the index to be "ordered" for better performance.Using UUID as the primary key will make your database slower, as it will have to search for a string instead of a number.When talking about the usage of UUID as a primary key or separate column, there are a few things to consider: Which will give us the following results:Īs you can see, the ULID was generated in an order, while the UUID was not. ![]() If we want to take a look at what UUID and ULID look like, we can generate a few of them: But it is still unique enough for most cases. This also means that ULID is not as unique as UUID, as it uses a timestamp. This means that ULID is sortable, while UUID is not. UUID is generated using a random number generator, while ULID is generated using a timestamp and a random number generator. The key difference between UUID and ULID is the way they are generated. If you have a small database - you won't notice any difference. But this is only noticeable if you have a large database. This is because UUID is stored as a string, while bigInt is stored as a number. If we compare the speed of the UUID field to a bigInt field, we can see that UUID is 2x slower. If we compare the size of the UUID field to a bigInt field, we can see that UUID takes 2x the space. The Str helper is a wrapper around the Ramsey library, so you can use either one. Illuminate\Support\ Str :: uuid () -> toString () To generate UUID V4 in Laravel, you can use: It's also especially useful if you have a multi-tenancy application. ![]() ![]() It also gives you a bit more security, as you can't just change 1 number in a URL and attempt to see someone else's order. Now, your users will have no idea how many orders you have, and they won't have a chance to guess the next or previous order correctly (unless they are extremely lucky!). But if you use UUIDs, you can hide that information:ĭ/shop/orders/f47ac10b-58cc-4372-a567-0e02b2c3d479 And people can attempt to change the number to see if there are more orders. This tells you that there are at least 1,596,115 orders in the database. How many times you have seen a URL like this? Another benefit of doing so is that you can hide details in your URL. The chance for a collision is less than 1 in 2.71 quintillion (really low!), so you can safely use them for your models. If we take a look at UUID V4 (which looks like this: f47ac10b-58cc-4372-a567-0e02b2c3d479) - we can be pretty sure that the values generated will always be unique. Generating unique IDs for your models and not having to worry about exposing the order of your records in the database.Generating unique IDs for your models and not having to worry about exposing the number of records you have in your database.Generating unique IDs for your models and not having to worry about collisions (duplicates).We will try to answer the most common ones here. If you just started with UUID, you might have quite a few questions about them.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |