[CockroachDB] Primary Key in CockroachDB
CockroachDB에서 권장하는 Primary Key는 UUID(Universally Unique Identifier)를 사용하는 것입니다.
UUID는 여러가지 포맷이 있는데 기본은 "Standard RFC4122-specified format" 형식입니다.
ex) acde070d-8c4c-4f0d-9d8a-162843c10333
클러스터 환경에서 각각의 노드에 데이터가 청크단위로 분배될때 (기본 64MB) 선형적으로 증가하는 값을 Primary Key로
사용하는 것 보다 UUID 값 처럼 랜덤값을 사용하면 데이터 분산이 효과적이며, 병목현상도 줄일 수 있습니다.
ex) create table t_order (
_id uuid primary key default gen_random_uuid(),
order_no string(20) not null,
qty int not null
);
전통적인 RDBMS의 Primary Key 대체용으로 Unique Index를 만들어 줍니다.
create unique index idx_t_order_01 on t_order(order_no);
insert into t_order (order_no, qty) values('201908130000001', 10);
Tip:
To autogenerate unique row IDs, we recommend using UUID with the gen_random_uuid() function
as the default value.