정의
- Universally Unique IDentifier. UUID는 공간과 시간에 대해 전역적으로 unique하게 디자인 됨.
- A UUID is a 128-bit number represented by a utf8 string of five hexadecimal numbers in
aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
format: - The first three numbers are generated from a timestamp.
- The fourth number preserves temporal uniqueness in case the timestamp value loses monotonicity (for example, due to daylight saving time).
- The fifth number is an IEEE 802 node number that provides spatial uniqueness. A random number is substituted if the latter is not available (for example, because the host computer has no Ethernet card, or we do not know how to find the hardware address of an interface on your operating system). In this case, spatial uniqueness cannot be guaranteed. Nevertheless, a collision should have very low probability.
UUID를 Primary Key로??
장점
- UUID 값은 테이블, 데이터베이스 서버 간의 모든 곳에서 유니크함 → 다른 데이터베이스나 분산형 db에서 데이터 병합이 쉬움!
- UUID 값은 데이터에 대한 정보를 드러내지 않아 URL에 사용되더라도 안전함
- 만약 customer id가 10일 때 URL이 다음과 같다면
http://www.example.com/customers/10/
Customer가 10, 11, 12 이렇게 관리된다는 것을 알고 공격의 타겟이 될 수 있음
- UUID 값은 어디에서나 생성될 수 있기 때문에 db server에 가서 primary key를 만들고 하는 번거로운 작업이 필요가 없음 → application의 로직 또한 단순화 시킴
- parent table과 child table에 데이터를 넣고자 할 때, pk가 int형이면 parent table에 데이터를 먼저 넣고 나서 해당 id를 가지고 child table에 데이터를 넣어주어야 함. 그러나 UUID를 사용하면 uuid 두개 생성한 다음, parent와 child에 한번에 데이터 삽입이 가능함
단점
- 용량이 크다. UUID는 16 Bytes임. int(4-bytes), big integers(8-bytes)
- debuggin이 어려울 수 있음.
WHERE id = 'df3b7cb7-6a95-11e7-8846-b05adad3f0ae'
instead ofWHERE id = 10
- UUID의 사이즈와 정렬이 되지 않았기 때문에 → performance issue를 야기시킬 수 있음
UUID를 저장할 시 Mysql에서 BINARY(16)으로 저장하는 이유
- VARCHAR(36) 이런 필드에 저장할 수도 있지만, 대쉬 4개가 불필요함
- 또, UUID의 각각의 character 쌍은 하나의 hexadecimal(16진수) number 임 → 16 개의 16진수 숫자로 이루어지게 됨
- BINARY(16) 의 size < VARCHAR(36)
- looks more logical to index it