Mysql stores UUID to remove the horizontal line, and mysqluuid removes the horizontal line

Source: Internet
Author: User

Mysql stores UUID to remove the horizontal line, and mysqluuid removes the horizontal line

Refer:

Http://stackoverflow.com/questions/412341/how-should-i-store-guid-in-mysql-tables

UUID is usually used as a unique identifier and needs to be stored in the database.

UUID format
Copy codeThe Code is as follows:
String string = UUID. randomUUID (). toString ();
System. out. println ("uuid:" + string );
Uuid: 05ba463f-1dab-471f-81c7-58e0b06f35f0

Disadvantages of direct UUID storage in databases:

A string that is completely 'random', for example, generated by MD5 (), SHA1 (), and UUID. Each new value generated by them is stored in a large space range, which slows down INSERT and some SELECT queries. 1) They will slow down the INSERT query, because the inserted values will be randomly placed into the index. This results in paging, random disk access, and clustered index fragmentation on the clustered storage engine. 2) They will slow down SELECT queries because logically adjacent rows are distributed across disks and memory. 3) random values cause the cache to have poor performance on all types of queries, because they will invalidate the access locality on which the cache depends. If the entire dataset becomes "hot", caching specific data into the memory will have no advantage. In addition, if the working set cannot be loaded into the memory, the cache will perform a lot of write operations and cause many cache misses.

If you save the UUID value, you should remove the short horizontal line. A better way is to use UHEX () to convert the UUID value to a 16-byte number and save it in BINARY (16) column.

Copy codeThe Code is as follows:
DELIMITER $
Create function 'guidtobinary '(
$ Data VARCHAR (36)
) RETURNS binary (16)
BEGIN
DECLARE $ Result BINARY (16) default null;
IF $ Data IS NOT NULL THEN
SET $ Data = REPLACE ($ Data ,'-',");
SET $ Result = CONCAT (UNHEX (SUBSTRING ($ Data )), UNHEX (SUBSTRING ($ Data, 1, 2 )),
UNHEX (SUBSTRING ($ Data, 11, 2), UNHEX (SUBSTRING ($ Data, 9, 2), UNHEX (SUBSTRING ($ Data, 15, 2), UNHEX (SUBSTRING ($ Data, 13, 2 )),
UNHEX (SUBSTRING ($ Data, 17,16 )));
End if;
RETURN $ Result;
END
$
Create function 'togu '(
$ Data BINARY (16)
) RETURNS char (36) CHARSET utf8
BEGIN
DECLARE $ Result CHAR (36) default null;
IF $ Data IS NOT NULL THEN
SET $ Result = CONCAT (HEX (SUBSTRING ($ Data )), HEX (SUBSTRING ($ Data, 1, 1 )),'-',
HEX (SUBSTRING ($ Data, 6, 1), HEX (SUBSTRING ($ Data, 5, 1 )),'-',
HEX (SUBSTRING ($ Data, 8, 8), HEX (SUBSTRING ($ Data, 7, 1 )),'-',
HEX (SUBSTRING ($ Data, 9, 2), '-', HEX (SUBSTRING ($ Data, 11, 6 )));
End if;
RETURN $ Result;
END

Copy codeThe Code is as follows:
Create function 'uidtobin' () RETURNS binary (16)
BEGIN
DECLARE my_uuid char (36 );
SET my_uuid = UUID ();
Return concat (UNHEX (LEFT (my_uuid, 8), UNHEX (MID (my_uuid, 10, 4), UNHEX (MID (my_uuid, 15, 4), UNHEX (MID (my_uuid, 20, 4), UNHEX (RIGHT (my_uuid, 12 )));
END
Create function 'bintouuid' (uuid binary (16) RETURNS char (36)
BEGIN
Return concat (HEX (LEFT (uuid, 4), '-', HEX (MID (uuid, 5, 2), '-', HEX (MID (uuid, 7, 2), '-', HEX (MID (uuid, 9, 2), '-', HEX (RIGHT (uuid, 6 )));
END

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.