Mysql中儲存UUID去除橫線的方法

2022-09-29 13:09:08 字數 2514 閱讀 1277

參考:

通常用uuid做唯一標識,需要在資料庫中進行儲存。

uuid的格式

複製** **如下:

string string = uuid.randomuuid().tostring(); 

system.out.println(「uuid:」 + string);

uuid:05ba463f-1dab-471f-81c7-58e0b06f35f0

資料庫中直接儲存uuid的壞處:

完全『隨機'的字串,例如由md5()、sha1()、uuid()產生的。它們產生的每乙個新值都會被任意地儲存在很大的空間範圍內,這會減慢insert及一些select查詢。1)它們會減慢insert查詢,因為插入的值會被隨機地放入索引中。這會導致分頁、隨機磁碟訪問及聚集儲存引擎上的聚集索引碎片。2)它們會減慢select查詢,因為邏輯上程式設計客棧相鄰的行會分布在磁碟和記憶體中的各個地方。3)隨機值導致快取對所有型別的查詢效能都很差,因為它們會使快取賴以工作的訪問區域性性失效。如果整個資料集都變得同樣「熱」的時候,那麼把特定部分的資料快取到記憶體中就沒有任何的優勢了。並且如果工作集不能被裝入記憶體中,快取就會進行很多刷寫的工作,並且會導致很多快取未命中。

如果儲存uuid值,就應該移除其中的短橫線,更好的辦法是使用uhex()把uuid值轉化為16位元組的數字,並把它儲存在binary(16)列中。

複製** **如下:

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,7,2)),unhex(substring($data,5,2)),unhex(substring($data,3,2)), 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 `toguid`( 

$data binary(16) 

) returns char(36) charset gyaqhqmmgputf8 

begin

declare $result char(36) default null; 

if $data is not null then

set $result = concat(hex(substring($data,4,1)),hex(substring($data,3,1)),hex(substring($data,2,1)), hex(substring($data,1,1)) , 『-',  

hex(substring($data,6,1)),hex(substring($data,5,1)),'-', 

hex(程式設計客棧substring($data,8,1)) , hex(substring($data,7,1)),'-', 

hex(substring($data,9,2)),'-',hex(substring($data,11,6))); 

end if; 

return $result; 

end複製** **如下:

create function `uuidtobin`() 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)));  

endcreate 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(u程式設計客棧uid,9,2)),'-',hex(right(uuid,6)));  

end

本文標題: mysql中儲存uuid去除橫線的方法

本文位址:

mysql中儲存過程

delimiter,簡單解釋下這個命令的用途,在mysql中每行命令都是用 結尾,回車後自動執行,在儲存過程中 往往不代表指令結束,馬上執行,而delimiter原本就是 的意思,因此用這個命令轉換一下 為 這樣只有收到 才認為指令結束可以執行 檢視myql中已經存在的儲存過程 show proce...

mysql中儲存過程

儲存過程,其本質還是函式 但其規定 不能有返回值 說明 1,in 用於設定該變數是用來 接收實參資料 的,即 傳入 預設不寫,就是in 2,out 用於設定該變數是用來 儲存儲存過程中的資料 的,即 傳出 即函式中必須對它賦值 3,inout 是in和out的結合,具有雙向作用 4,對於,out和i...

mysql中儲存過程學習

例項 獲取登入 登出 操作日誌 命令日誌 從系統日誌表查詢,命令日誌表查詢結過儲存到臨時表中,得到所有的日誌,然後按條件進行篩選 create function getlog uname varcharacter 200 starttime datetime,endtime datetime,log...