33

This is the best way I could come up with to convert a MySQL GUID/UUID generated by UUID() to a binary(16):

UNHEX(REPLACE(UUID(),'-',''))

And then storing it in a BINARY(16)

Are there any implications of doing it this way that I should know of?

thr
  • 19,160
  • 23
  • 93
  • 130
  • Yes that is, but I could get marginal performance improvements when I relied on application's own guid generation and unhexing and replacing (in my case, .NET) – nawfal Jun 28 '12 at 00:08
  • @nawfal, may be an oblique answer to OP, but would really like to see your comment fleshed out with examples. – Marc L. Mar 06 '17 at 19:27

2 Answers2

9

From MySQL 8.0 and above you could use UUID_TO_BIN:

UUID_TO_BIN(string_uuid), UUID_TO_BIN(string_uuid, swap_flag)

Converts a string UUID to a binary UUID and returns the result. (The IS_UUID() function description lists the permitted string UUID formats.) The return binary UUID is a VARBINARY(16) value.

CREATE TABLE t (id binary(16) PRIMARY KEY);
 
INSERT INTO t VALUES(UUID_TO_BIN(UUID(), true));
INSERT INTO t VALUES(UUID_TO_BIN(UUID(), true));
INSERT INTO t VALUES(UUID_TO_BIN(UUID(), true));

SELECT *, BIN_TO_UUID(id) FROM t;

DB-Fiddle.com Demo

Community
  • 1
  • 1
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
8

Not many implications. It will slow down the queries a little, but you will hardly notice it.

UNIQUEIDENTIFIER is stored as 16-byte binary internally anyway.

If you are going to load the binary into a client and parse it there, note the bit order, it may have other string representation than the initial NEWID().

Oracle's SYS_GUID() function is prone to this issue, converting it to a string gives different results on client and on server.

Ross McFarlane
  • 4,054
  • 4
  • 36
  • 52
Quassnoi
  • 413,100
  • 91
  • 616
  • 614
  • I would add to the comment some material I dug up. Considerations around UUID in MySQL should think about performance as well as uniqueness. While slightly older an interesting performance test was here: http://kccoder.com/mysql/uuid-vs-int-insert-performance/ - This shows a point of sensitivity around MySQL and ensuring "UNIQUE" values in MySQL. I am sure there have been improvements but the size of the field, the structure of the contained index, etc. should be contemplated if you have the opportunity. BINARY(16) / CHAR(16) does seem to be the way to go. – Zack Jannsen Feb 04 '16 at 10:47
  • Doing some more in depth research I also came to a good link on "handling BINARY indexes". Here is the link: http://mysqlserverteam.com/storing-uuid-values-in-mysql-tables/ - I'd encourage anyone looking at a conversion to MySQL to review this blog as well. Some great points are brought up to consider around how to 'optimally' store the BINARY id. Your use case may vary based on how you implement UUID but great points on ordering bits and using calculated columns for any 'human readable' needs. – Zack Jannsen Feb 04 '16 at 10:53
  • 2
    I'm confused why this is accepted, when the terminology and assumptions of the answer seem to be exclusive to MS SQL, not MySQL. – Marc L. Mar 06 '17 at 19:14