So, as an answer to the comments. The correct way to store a 36-character UUID in binary (16) is to insert as follows:
INSERT INTO sometable (UUID) VALUES (UNHEX(REPLACE("3f06af63-a93c-11e4-9797-00505690773f", "-","")))
UNHEX because the UUID is already a hexadecimal value. We trim the REPLACE dash in the statement to reduce the length to 32 ASCII characters (our 16 bytes are represented as HEX ). You can do this at any time before saving, obviously, so this should not be handled by the database.
You can get the UUID as follows:
SELECT HEX(UUID) FROM sometable;
Just in case, if someone comes across this topic and is not sure how it works.
And remember: if you select a row using a UUID, use UNHEX() provided :
SELECT * FROM sometable WHERE UUID = UNHEX('3f06af63a93c11e4979700505690773f');
And not HEX() in the column:
SELECT * FROM sometable WHERE HEX(UUID) = '3f06af63a93c11e4979700505690773f';
The second solution, although it works, requires MySQL HEX evaluate all the UUIDs before it can determine which rows match. It is very inefficient.
Change: If you are using MySQL 8, you should take a look at the UUID functions as indicated in the SlyDave answer. This answer is still correct, but it does not optimize the UUIDs that can be made using these functions.
nickdnk
source share