2018년 2월 4일 일요일

Implementation of Hash partiton in postgresql

PostgreSQL의 경우 따로 테이블 파티셔닝 기능은 없음.

대용량 데이타베이스를 핸들링 해야되는 경우 아래와 같이 trigger를 이용한 파티셔닝이 가능.

ps. 프로젝트 초반부에는 오픈소스 데이타베이스인 PostgreSQL에서 과연 대용량 처리가 가능할까 의심이 들었는데 결과는 아주 만족스럽. Mysql에 비해 상당히 안정적으로 돌아감.

 1. 파티셔닝키에 사용될 Hash 함수를 생성

create or replace function hash_cust_idfy_id(cust_idfy_id text)
returns int
as
$$ select ascii(substring($1,length($1)))%4 $$
language sql immutable strict;

2. 파티셔닝 테이블 생성

CREATE TABLE tablename (cust_idfy_id text, mdate date);
CREATE TABLE tablename_partition_0 ( CHECK ( hash_cust_idfy_id(cust_idfy_id) = 0 ) ) INHERITS (tablename);
CREATE TABLE tablename_partition_1 ( CHECK ( hash_cust_idfy_id(cust_idfy_id) = 1 ) ) INHERITS (tablename);
CREATE TABLE tablename_partition_2 ( CHECK ( hash_cust_idfy_id(cust_idfy_id) = 2 ) ) INHERITS (tablename);
CREATE TABLE tablename_partition_3 ( CHECK ( hash_cust_idfy_id(cust_idfy_id) = 3 ) ) INHERITS (tablename);

3. 테스트 및 Explain Plan
explain
select * from tablename where cust_idfy_id = '1111' and hash_cust_idfy_id(cust_idfy_id) = hash_cust_idfy_id('1111')

Result  (cost=0.00..40.75 rows=2 width=36)
  ->  Append  (cost=0.00..40.75 rows=2 width=36)
        ->  Seq Scan on tablename  (cost=0.00..0.00 rows=1 width=36)
              Filter: ((cust_idfy_id = '1111'::text) AND ((ascii("substring"(cust_idfy_id, length(cust_idfy_id))) % 4) = 1))
        ->  Seq Scan on tablename_partition_1 tablename  (cost=0.00..40.75 rows=1 width=36)
              Filter: ((cust_idfy_id = '1111'::text) AND ((ascii("substring"(cust_idfy_id, length(cust_idfy_id))) % 4) = 1))



댓글 없음:

댓글 쓰기

Creating CRC32 Hex string

public String getCRC32HexaString(String paramString) throws Exception  {   byte bytes[] = paramString.getBytes(DEFAULT_CHARSET);   Che...