

I've created following test scenario: Insert random UUID v4 as primary key for a table until 40.000.000 rows are created. NOTE: Unfortunately MariaDB doesn't support RANDOM_BYTES() (See ) Test RETURN '-', '-4', '-', '-', should generate UUID V4 random enough to don't care about collisions. 4th block first nibble can only be 8, 9 A or B, remaining is random

SET = SUBSTR(HEX(RANDOM_BYTES(2)), 2, 3) 3th block will start with a 4 indicating the version, remaining is random 1th and 2nd block are made of 6 random bytes So we can update the function to: CREATE FUNCTION uuid_v4s()
Mysql uuid not unique generator#
This function returns a binary string of len random bytes generated using the random number generator of the SSL library. It's possible to generate safe UUID V4 on MySQL side using random_bytes() function: In the practice, this mean that the generated UUID using this function might (and will) be biased, and collisions can occur more frequently then expected. It is a fast way to generate random numbers on demand that is portable between platforms for the same MySQL version. RAND() is not meant to be a perfect random generator. Note: The pseudo-random number generation used (MySQL's RAND) is notĬryptographically secure and thus has some bias which can increase the collisionīoth existing answers relies on MySQL RAND() function: RETURN '-', '-', '-', '-', Switch back the delimiter 5th section first half-byte can only be 8, 9 A or B 4th section will start with a 4 indicating the version Generate 8 2-byte strings that we will combine into a UUIDv4 Change delimiter so that the function body doesn't end the function declaration I'm answering my own question to share that in the hope that it'll be Mysql function that generates a random UUID (i.e. I've spent quite some time looking for a solution and came up with the following
