Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice to generate uuid ? php OR MySql

What is the best practices to generate UUID (PRIMARY KEY) let it generate at Mysql end OR let it generate at PHP end

doing some performances test i find generating it at php end is a bit faster

but doing some Google i find many are practicing it to generate at MySQL end http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram

so what would be the right way to do so for an continuously growing data ?

like image 481
terry Avatar asked Mar 20 '15 05:03

terry


1 Answers

If you wish to insert rows from different applications then generating the UID via the DB is the safest method because the implementation is centralised and of course won't change between applications.

Let's say you decide to create an API in another language. You would then need to implement the same generator in that language. There might be differences in the way this other language implements functions you rely on to generate key data, which lead to duplicate keys.

If you are only going to be inserting rows from PHP on your own server, it doesn't matter if you generate UIDs in the application or the DB.

Safe isn't always right. Or practical. Weigh up the pros and cons of your intended use cases and go from there.

like image 186
David K-J Avatar answered Oct 22 '22 07:10

David K-J