Best practice for creating uuid? php OR MySql

What are the best methods for generating a UUID (PRIMARY KEY) let it be generated at the end of Mysql OR let it be generated in PHP end

Running some performance tests that I find generates it in php-end, a little faster

but by doing some google, I find that many practice it for generation at the end of MySQL http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram

so how will this be the right way for ever-growing data?

+8
php mysql uuid primary-key
source share
3 answers

If you want to insert rows from different applications, then creating a UID through the database is the safest method, since the implementation is centralized and, of course, will not change between applications.

Suppose you decide to create an API in another language. Then you need to implement the same generator in this language. There may be differences in how this other language implements the functions you rely on to generate key data that leads to duplicate keys.

If you're just about to embed lines from PHP on your own server, it doesn't matter if you generate UIDs in the application or in the database.

The safe is not always right. Or practical. Weigh the pros and cons of your intended use cases from there.

+2
source share

you can try the very nice PHP class Pure PHP UUID Generator

  • Repository for cloning (or copy it to the php file of the UUID.php .. file)
  • Use a class with a static method directly

Example:

UUID::v5('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString');

0
source share

Using native database UUID functions is likely to cause problems if the database is replicated. At the same time, I would confirm that all pre-written UUID generators were tested to confirm that the least significant byte of the UUID changes faster than the most significant byte (to accommodate the database index clusters).

0
source share

All Articles