"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > 성능 향상을 위해 UUID를 MySQL의 숫자로 저장하는 이유는 무엇입니까?

성능 향상을 위해 UUID를 MySQL의 숫자로 저장하는 이유는 무엇입니까?

2025-02-06에 게시되었습니다
검색:719

Why Store UUIDs as Numbers in MySQL for Improved Performance?

대시를 제거하고 바이너리로 변환

첫 번째 단계는 UUID에서 대시를 제거하는 것입니다. 예를 들어, "110E8400-E29B-11D4-A716-44665440000"과 같은 UUID는 "110E8400E29B11D4A7164665440000"이됩니다. MySQL의 이진 데이터 유형은 문자열보다 처리하는 데 더 효율적인 이진 표현을 사용합니다.

예제 SQL 쿼리

이진 필드에 삽입하십시오. 다음 쿼리 : table_name (field_binary) 값에 삽입하십시오 (field_binary) 값 (unhex ( "110e8400e29b11d4a7164665440000")

INSERT INTO table_name (field_binary) VALUES (UNHEX("110E8400E29B11D4A716446655440000"))

INSERT INTO table_name (field_binary) VALUES (UNHEX("110E8400E29B11D4A716446655440000"))

. field_binary) AS field_binary FROM table_name

Ruby Code Integration
require 'uuidtools'

# Generate a UUID as a string
uuid = UUID.new

# Remove dashes and convert to binary
binary_uuid = uuid.to_s.gsub("-", "").hex_to_bin

# Store the binary UUID in MySQL
# ...

# Retrieve the binary UUID from MySQL
# ...

# Reconstruct the original UUID with dashes
new_uuid = binary_uuid.bin_to_hex.gsub(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '\1-\2-\3-\4-\5')

In Ruby, you can use the UUIDtools gem to generate UUIDs and convert them to binary. 보석은 uuid.new 및 uuid.hex_to_bin과 같은 방법을 제공합니다. 예를 들면 :

'uuidtools'가 필요합니다. # uuid를 문자열로 생성합니다 uuid = uuid.new # 대시를 제거하고 바이너리로 변환합니다 binary_uuid = uuid.to_s.gsub ( "-", "".hex_to_bin # 이진 UUID를 MySQL에 보관하십시오 # ... # MySQL에서 이진 UUID를 검색하십시오 # ... # 원래 UUID를 대시로 재구성하십시오 new_uuid = binary_uuid.bin_to_hex.gsub (/(. {8}) (. {4}) (. {4}) (. {4}) (. {12})/, '\ 1- \ 2- \ 3 -\ 4- \ 5 ') Why Store UUIDs as Numbers in MySQL for Improved Performance? 
[&&)

최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3